Mel*_*oun 97 c++ static initialization const map
我只需要字典或关联数组string=> int.
这种情况有类型映射C++.
但我只需要一个map forall实例( - > static),这个地图不能改变( - > const);
我用boost库找到了这种方式
std::map<int, char> example =
boost::assign::map_list_of(1, 'a') (2, 'b') (3, 'c');
Run Code Online (Sandbox Code Playgroud)
没有这个lib还有其他解决方案吗?我尝试过类似的东西,但是地图初始化总是存在一些问题.
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();
};
Run Code Online (Sandbox Code Playgroud)
小智 102
#include <map>
using namespace std;
struct A{
static map<int,int> create_map()
{
map<int,int> m;
m[1] = 2;
m[3] = 4;
m[5] = 6;
return m;
}
static const map<int,int> myMap;
};
const map<int,int> A:: myMap = A::create_map();
int main() {
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*hop 93
C++ 11标准引入了统一初始化,如果您的编译器支持它,这将使这更加简单:
//myClass.hpp
class myClass {
private:
static map<int,int> myMap;
};
Run Code Online (Sandbox Code Playgroud)
//myClass.cpp
map<int,int> myClass::myMap = {
{1, 2},
{3, 4},
{5, 6}
};
Run Code Online (Sandbox Code Playgroud)
另请参阅Professional C++中有关unordered_maps的此部分.
use*_*030 12
我做的!:)
没有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);
Run Code Online (Sandbox Code Playgroud)
小智 11
如果你觉得boost::assign::map_list_of有用,但由于某些原因不能使用它,你可以写自己的:
template<class K, class V>
struct map_list_of_type {
typedef std::map<K, V> Map;
Map data;
map_list_of_type(K k, V v) { data[k] = v; }
map_list_of_type& operator()(K k, V v) { data[k] = v; return *this; }
operator Map const&() const { return data; }
};
template<class K, class V>
map_list_of_type<K, V> my_map_list_of(K k, V v) {
return map_list_of_type<K, V>(k, v);
}
int main() {
std::map<int, char> example =
my_map_list_of(1, 'a') (2, 'b') (3, 'c');
cout << example << '\n';
}
Run Code Online (Sandbox Code Playgroud)
知道这些东西是如何工作的很有用,特别是当它们如此短暂时,但在这种情况下我会使用一个函数:
struct A {
static map<int, int> const m;
};
Run Code Online (Sandbox Code Playgroud)
namespace {
map<int,int> create_map() {
map<int, int> m;
m[1] = 2; // etc.
return m;
}
}
map<int, int> const A::m = create_map();
Run Code Online (Sandbox Code Playgroud)
解决该问题的另一种方法:
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
}
Run Code Online (Sandbox Code Playgroud)
这是更有效的,因为从堆栈到堆之间没有一种类型的副本(包括所有元素上的构造函数,析构函数)。这是否重要取决于您的用例。字符串无关紧要!(但您可能会或可能不会找到此版本的“清洁器”)
如果地图仅包含在编译时已知的条目,并且地图的键是整数,则根本不需要使用地图.
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
}
}
Run Code Online (Sandbox Code Playgroud)