C++ 如何在C++;?
我只需要字典或关联数组C++ 如何在C++;?,c++,static,map,initialization,constants,C++,Static,Map,Initialization,Constants,我只需要字典或关联数组string=>int 这种情况下有类型图C++。 但是我只需要所有实例的一个映射(->static),并且这个映射不能更改(->const) 我在boost库中找到了这种方法 std::map<int, char> example = boost::assign::map_list_of(1, 'a') (2, 'b') (3, 'c'); std::映射示例= boost::assign::map_list_of(1,'a')(2,'b')
string
=>int
这种情况下有类型图C++。 但是我只需要所有实例的一个映射(->static),并且这个映射不能更改(->const)
我在boost库中找到了这种方法 std::map<int, char> example =
boost::assign::map_list_of(1, 'a') (2, 'b') (3, 'c');
std::映射示例=
boost::assign::map_list_of(1,'a')(2,'b')(3,'c');
没有这个库还有其他解决方案吗?
我已经尝试过类似的方法,但是地图初始化总是存在一些问题
class myClass{
private:
static map<int,int> create_map()
{
map<int,int> m;
m[1] = 2;
m[3] = 4;
m[5] = 6;
return m;
}
static map<int,int> myMap = create_map();
};
class-myClass{
私人:
静态映射创建_映射()
{
地图m;
m[1]=2;
m[3]=4;
m[5]=6;
返回m;
}
静态映射myMap=create_map();
};
#包括
使用名称空间std;
结构A{
静态映射创建_映射()
{
地图m;
m[1]=2;
m[3]=4;
m[5]=6;
返回m;
}
静态常量映射myMap;
};
常量映射A::myMap=A::create_map();
int main(){
}
函数调用不能出现在常量表达式中
试试这个:(只是一个例子)
#包括
#包括
使用std::map;
使用std::cout;
类myClass{
公众:
静态映射创建_映射()
{
地图m;
m[1]=2;
m[3]=4;
m[5]=6;
返回m;
}
常量静态映射myMap;
};
常量mapmyClass::myMap=create_map();
int main(){
map t=myClass::create_map();
std::cout如果您发现
的boost::assign::map\u list\u有用,但由于某种原因无法使用,您可以:
模板
类型的结构映射列表{
typedef std::map;
地图数据;
映射类型(kk,vv){data[K]=V;}
映射类型和运算符()的映射列表{data[K]=V;返回*this;}
运算符映射常量&()常量{返回数据;}
};
模板
映射列表的类型我的映射列表的类型(K,V){
返回类型为(k,v)的映射列表;
}
int main(){
std::映射示例=
(1,'a')(2,'b')(3,'c');
不能用不同的方法解决这个问题:
struct A {
static const map<int, string> * singleton_map() {
static map<int, string>* m = NULL;
if (!m) {
m = new map<int, string>;
m[42] = "42"
// ... other initializations
}
return m;
}
// rest of the class
}
结构A{
静态常量映射*单例映射(){
静态映射*m=NULL;
如果(!m){
m=新地图;
m[42]=“42”
//…其他初始化
}
返回m;
}
//班上其他同学
}
这更有效,因为从堆栈到堆(包括所有元素上的构造函数和析构函数)没有单一类型的副本。这是否重要取决于您的用例。与字符串无关!(但您可能会或可能不会发现此版本“更干净”)如果映射只包含编译时已知的条目,并且映射的键是整数,则根本不需要使用映射
char get_value(int key)
{
switch (key)
{
case 1:
return 'a';
case 2:
return 'b';
case 3:
return 'c';
default:
// Do whatever is appropriate when the key is not valid
}
}
我经常使用这种模式,并建议您也使用它:
class MyMap : public std::map<int, int>
{
public:
MyMap()
{
//either
insert(make_pair(1, 2));
insert(make_pair(3, 4));
insert(make_pair(5, 6));
//or
(*this)[1] = 2;
(*this)[3] = 4;
(*this)[5] = 6;
}
} const static my_map;
使用以下命令:
void bar()
{
struct MyMap : Map
{
MyMap()
{
...
}
} static mymap;
}
不仅您不再需要在这里处理布尔变量,而且您也不再需要检查隐藏的全局变量(如果函数中的静态变量的初始值设定项已被调用)。C++11标准引入了统一的初始化,如果您的编译器支持它,这将使初始化变得更加简单:
//myClass.hpp
class myClass {
private:
static map<int,int> myMap;
};
//myClass.hpp
类myClass{
私人:
静态地图myMap;
};
//myClass.cpp
映射myClass::myMap={
{1, 2},
{3, 4},
{5, 6}
};
另请参见无序映射。如果您使用的编译器仍然不支持通用初始化,或者您对使用Boost有所保留,则另一种可能的替代方法如下
std::map<int, int> m = [] () {
std::pair<int,int> _m[] = {
std::make_pair(1 , sizeof(2)),
std::make_pair(3 , sizeof(4)),
std::make_pair(5 , sizeof(6))};
std::map<int, int> m;
for (auto data: _m)
{
m[data.first] = data.second;
}
return m;
}();
std::map m=[](){
标准::配对_m[]={
标准::制造双(1,尺寸为(2)),
标准::制造双(3,尺寸为(4)),
std::make_对(5,sizeof(6))};
std::map m;
对于(自动数据:_m)
{
m[data.first]=data.second;
}
返回m;
}();
在没有C++11的情况下可以正常工作
class MyClass {
typedef std::map<std::string, int> MyMap;
struct T {
const char* Name;
int Num;
operator MyMap::value_type() const {
return std::pair<std::string, int>(Name, Num);
}
};
static const T MapPairs[];
static const MyMap TheMap;
};
const MyClass::T MyClass::MapPairs[] = {
{ "Jan", 1 }, { "Feb", 2 }, { "Mar", 3 }
};
const MyClass::MyMap MyClass::TheMap(MapPairs, MapPairs + 3);
class-MyClass{
typedef std::map MyMap;
结构T{
常量字符*名称;
int-Num;
运算符MyMap::value\u type()常量{
返回std::pair(名称,Num);
}
};
静态常量映射对[];
静态常量MyMap TheMap;
};
常量MyClass::T MyClass::MapPairs[]={
{“一月”,1},{“二月”,2},{“三月”,3}
};
常量MyClass::MyMap MyClass::TheMap(映射对,映射对+3);
您可以尝试以下方法:
MyClass.h
class MyClass {
private:
static const std::map<key, value> m_myMap;
static const std::map<key, value> createMyStaticConstantMap();
public:
static std::map<key, value> getMyConstantStaticMap( return m_myMap );
}; //MyClass
class-MyClass{
私人:
静态常量std::map m_myMap;
静态常量std::map createMyStaticConstantMap();
公众:
static std::map getMyConstantStaticMap(返回m_myMap);
};//MyClass
MyClass.cpp
#include "MyClass.h"
const std::map<key, value> MyClass::m_myMap = MyClass::createMyStaticConstantMap();
const std::map<key, value> MyClass::createMyStaticConstantMap() {
std::map<key, value> mMap;
mMap.insert( std::make_pair( key1, value1 ) );
mMap.insert( std::make_pair( key2, value2 ) );
// ....
mMap.insert( std::make_pair( lastKey, lastValue ) );
return mMap;
} // createMyStaticConstantMap
#包括“MyClass.h”
常量std::map MyClass::m_myMap=MyClass::createMyStaticConstantMap();
常量std::map MyClass::createMyStaticConstantMap(){
std::map-mMap;
mMap.insert(标准::make_pair(键1,值1));
mMap.insert(标准::make_pair(键2,值2));
// ....
mMap.insert(std::make_pair(lastKey,lastValue));
返回mMap;
}//createMyStaticConstantMap
在这个实现中,类常量静态映射是私有成员,其他类可以使用公共get方法访问它
由于它是常量且不能更改,因此可以删除public get方法
然后将map变量移到classes public部分。但是,如果需要继承和/或多态性,我会将createMap方法保留为private或protected
std::map<key,value> m1 = MyClass::getMyMap();
// then do work on m1 or
unsigned index = some predetermined value
MyClass::getMyMap().at( index ); // As long as index is valid this will
// retun map.second or map->second value so if in this case key is an
// unsigned and value is a std::string then you could do
std::cout << std::string( MyClass::getMyMap().at( some index that exists in map ) );
// and it will print out to the console the string locted in the map at this index.
//You can do this before any class object is instantiated or declared.
//If you are using a pointer to your class such as:
std::shared_ptr<MyClass> || std::unique_ptr<MyClass>
// Then it would look like this:
pMyClass->getMyMap().at( index ); // And Will do the same as above
// Even if you have not yet called the std pointer's reset method on
// this class object.
// This will only work on static methods only, and all data in static methods must be available first.
std::map m1=MyClass::getMyMap();
//然后对m1或m1进行操作
无符号索引=某个预定值
MyClass::getMyMap().at(index);//只要索引有效,这将
//重新运行map.second或map->second值,因此在本例中,如果键是
//unsigned and value是一个std::字符串
std::cout getMyMap().at(index);//并将执行与上面相同的操作
//即使您尚未在上调用std指针的重置方法
//这类对象。
//这只适用于静态方法,并且所有数据都在
//myClass.hpp
class myClass {
private:
static map<int,int> myMap;
};
//myClass.cpp
map<int,int> myClass::myMap = {
{1, 2},
{3, 4},
{5, 6}
};
std::map<int, int> m = [] () {
std::pair<int,int> _m[] = {
std::make_pair(1 , sizeof(2)),
std::make_pair(3 , sizeof(4)),
std::make_pair(5 , sizeof(6))};
std::map<int, int> m;
for (auto data: _m)
{
m[data.first] = data.second;
}
return m;
}();
class MyClass {
typedef std::map<std::string, int> MyMap;
struct T {
const char* Name;
int Num;
operator MyMap::value_type() const {
return std::pair<std::string, int>(Name, Num);
}
};
static const T MapPairs[];
static const MyMap TheMap;
};
const MyClass::T MyClass::MapPairs[] = {
{ "Jan", 1 }, { "Feb", 2 }, { "Mar", 3 }
};
const MyClass::MyMap MyClass::TheMap(MapPairs, MapPairs + 3);
class MyClass {
private:
static const std::map<key, value> m_myMap;
static const std::map<key, value> createMyStaticConstantMap();
public:
static std::map<key, value> getMyConstantStaticMap( return m_myMap );
}; //MyClass
#include "MyClass.h"
const std::map<key, value> MyClass::m_myMap = MyClass::createMyStaticConstantMap();
const std::map<key, value> MyClass::createMyStaticConstantMap() {
std::map<key, value> mMap;
mMap.insert( std::make_pair( key1, value1 ) );
mMap.insert( std::make_pair( key2, value2 ) );
// ....
mMap.insert( std::make_pair( lastKey, lastValue ) );
return mMap;
} // createMyStaticConstantMap
std::map<key,value> m1 = MyClass::getMyMap();
// then do work on m1 or
unsigned index = some predetermined value
MyClass::getMyMap().at( index ); // As long as index is valid this will
// retun map.second or map->second value so if in this case key is an
// unsigned and value is a std::string then you could do
std::cout << std::string( MyClass::getMyMap().at( some index that exists in map ) );
// and it will print out to the console the string locted in the map at this index.
//You can do this before any class object is instantiated or declared.
//If you are using a pointer to your class such as:
std::shared_ptr<MyClass> || std::unique_ptr<MyClass>
// Then it would look like this:
pMyClass->getMyMap().at( index ); // And Will do the same as above
// Even if you have not yet called the std pointer's reset method on
// this class object.
// This will only work on static methods only, and all data in static methods must be available first.