Fer*_*cio 591
使用C++ 11:
#include <map>
using namespace std;
map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}};
Run Code Online (Sandbox Code Playgroud)
使用Boost.Assign:
#include <map>
#include "boost/assign.hpp"
using namespace std;
using namespace boost::assign;
map<int, char> m = map_list_of (1, 'a') (3, 'b') (5, 'c') (7, 'd');
Run Code Online (Sandbox Code Playgroud)
Pie*_*BdR 132
最好的方法是使用一个功能:
#include <map>
using namespace std;
map<int,int> create_map()
{
map<int,int> m;
m[1] = 2;
m[3] = 4;
m[5] = 6;
return m;
}
map<int,int> m = create_map();
Run Code Online (Sandbox Code Playgroud)
Vit*_*con 113
制作类似于提升的东西并不是一个复杂的问题.这是一个只有三个函数的类,包括构造函数,用于复制boost所做的(几乎).
template <typename T, typename U>
class create_map
{
private:
std::map<T, U> m_map;
public:
create_map(const T& key, const U& val)
{
m_map[key] = val;
}
create_map<T, U>& operator()(const T& key, const U& val)
{
m_map[key] = val;
return *this;
}
operator std::map<T, U>()
{
return m_map;
}
};
Run Code Online (Sandbox Code Playgroud)
用法:
std::map mymap = create_map<int, int >(1,2)(3,4)(5,6);
上面的代码最适合初始化全局变量或需要初始化的类的静态成员,并且您不知道何时首先使用它,但您想确保其中的值可用.
如果说,你必须在现有的std :: map中插入元素......这里是你的另一个类.
template <typename MapType>
class map_add_values {
private:
MapType mMap;
public:
typedef typename MapType::key_type KeyType;
typedef typename MapType::mapped_type MappedType;
map_add_values(const KeyType& key, const MappedType& val)
{
mMap[key] = val;
}
map_add_values& operator()(const KeyType& key, const MappedType& val) {
mMap[key] = val;
return *this;
}
void to (MapType& map) {
map.insert(mMap.begin(), mMap.end());
}
};
Run Code Online (Sandbox Code Playgroud)
用法:
typedef std::map<int, int> Int2IntMap;
Int2IntMap testMap;
map_add_values<Int2IntMap>(1,2)(3,4)(5,6).to(testMap);
Run Code Online (Sandbox Code Playgroud)
请在此处查看GCC 4.7.2:http://ideone.com/3uYJiH
###############以下一切已经过时#################
编辑:map_add_values下面的类,这是我建议的原始解决方案,在GCC 4.5+时会失败.请查看上面的代码,了解如何向现有地图添加值.
template<typename T, typename U>
class map_add_values
{
private:
std::map<T,U>& m_map;
public:
map_add_values(std::map<T, U>& _map):m_map(_map){}
map_add_values& operator()(const T& _key, const U& _val)
{
m_map[key] = val;
return *this;
}
};Run Code Online (Sandbox Code Playgroud)
用法:
std::map<int, int> my_map; // Later somewhere along the code map_add_values<int,int>(my_map)(1,2)(3,4)(5,6);
注意:以前我用a operator []来添加实际值.dalle评论说这是不可能的.
#####################结束部分的结束#####################
Bri*_*eal 42
这是使用2元素数据构造函数的另一种方法.初始化它不需要任何函数.没有第三方代码(Boost),没有静态函数或对象,没有技巧,只是简单的C++:
#include <map>
#include <string>
typedef std::map<std::string, int> MyMap;
const MyMap::value_type rawData[] = {
MyMap::value_type("hello", 42),
MyMap::value_type("world", 88),
};
const int numElems = sizeof rawData / sizeof rawData[0];
MyMap myMap(rawData, rawData + numElems);
Run Code Online (Sandbox Code Playgroud)
自从我写这个答案后,C++ 11就出来了.您现在可以使用新的初始化列表功能直接初始化STL容器:
const MyMap myMap = { {"hello", 42}, {"world", 88} };
Run Code Online (Sandbox Code Playgroud)
Dre*_*mer 24
我将地图包装在静态对象中,并将地图初始化代码放在此对象的构造函数中,这样您就可以确保在执行初始化代码之前创建地图.
isn*_*xbh 22
例如:
const std::map<LoggerLevel, const char*> g_logLevelsDescriptions =
{
{ LoggerLevel::llNothing, "Logging disabled" },
{ LoggerLevel::llInfo, "Base information" },
{ LoggerLevel::llWarn, "Warnings" },
{ LoggerLevel::llError, "Errors" },
{ LoggerLevel::llDebug, "All information: debug-mode" }
};
Run Code Online (Sandbox Code Playgroud)
小智 18
只是想分享纯C++ 98的工作原理:
#include <map>
std::map<std::string, std::string> aka;
struct akaInit
{
akaInit()
{
aka[ "George" ] = "John";
aka[ "Joe" ] = "Al";
aka[ "Phil" ] = "Sue";
aka[ "Smitty" ] = "Yando";
}
} AkaInit;
Run Code Online (Sandbox Code Playgroud)
小智 17
你可以试试:
std::map <int, int> mymap =
{
std::pair <int, int> (1, 1),
std::pair <int, int> (2, 2),
std::pair <int, int> (2, 2)
};
Run Code Online (Sandbox Code Playgroud)
这类似于PierreBdR,没有复制地图.
#include <map>
using namespace std;
bool create_map(map<int,int> &m)
{
m[1] = 2;
m[3] = 4;
m[5] = 6;
return true;
}
static map<int,int> m;
static bool _dummy = create_map (m);
Run Code Online (Sandbox Code Playgroud)
如果您不熟悉C++ 98并且不想使用boost,那么我需要初始化静态映射时使用的解决方案:
typedef std::pair< int, char > elemPair_t;
elemPair_t elemPairs[] =
{
elemPair_t( 1, 'a'),
elemPair_t( 3, 'b' ),
elemPair_t( 5, 'c' ),
elemPair_t( 7, 'd' )
};
const std::map< int, char > myMap( &elemPairs[ 0 ], &elemPairs[ sizeof( elemPairs ) / sizeof( elemPairs[ 0 ] ) ] );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
408340 次 |
| 最近记录: |