什么std :: map扩展初始化列表看起来像? [英] What would a std::map extended initializer list look like?
本文介绍了什么std :: map扩展初始化列表看起来像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果它甚至存在,std :: map扩展初始化列表会是什么样子?
If it even exists, what would a std::map extended initializer list look like?
我试过一些组合...好,一切我
I've tried some combinations of... well, everything I could think of with GCC 4.4, but found nothing that compiled.
推荐答案
它存在和工作良好:
std::map <int, std::string> x
{
std::make_pair (42, "foo"),
std::make_pair (3, "bar")
};
请记住,地图的值类型是 pair< const key_type,mapped_type> ;
,所以你基本上需要一个具有相同或可转换类型的对的列表。
Remember that value type of a map is pair <const key_type, mapped_type>
, so you basically need a list of pairs with of the same or convertible types.
通过使用std :: pair的统一初始化,代码变得更简单
With unified initialization with std::pair, the code becomes even simpler
std::map <int, std::string> x {
{ 42, "foo" },
{ 3, "bar" }
};
这篇关于什么std :: map扩展初始化列表看起来像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文