[英]std::map initializer list constructor
The C++ standard defines a std::map
constructor using a std::initializer_list
: C ++标准使用
std::initializer_list
定义了一个std::map
构造std::initializer_list
:
map( std::initializer_list<value_type> init, const Allocator& );
However, where is defined what happens if the initializer list contains duplicate keys? 但是,在哪里定义了初始化程序列表包含重复键时会发生什么? Is the first key choosen, or the last?
是第一个选择键还是最后一个? For example:
例如:
std::map<std::string, int> my_map {
{"a", 1},
{"a", 2}
};
In practice, it seems it behaves like insert()
, so that the map will now contain {a: 1}. 在实践中,它看起来像
insert()
,因此地图现在将包含{a:1}。
However, I was unable to find anything in the C++ standard regarding this. 但是,我无法在C ++标准中找到任何关于此的内容。
N4296 (~C++14) N4296(~C ++ 14)
Table 102 - Associative container requirements
表102 - 关联容器要求
X(il);
|| Same as
X(il.begin(), il.end())
.与
X(il.begin(), il.end())
相同X(il.begin(), il.end())
。
Then from above in the table, for the iterator ctor: 然后从表中的上方,对于迭代器ctor:
Effects: Constructs an empty container and inserts elements from the range
[i, j)
into it;效果:构造一个空容器并将范围
[i, j)
元素插入其中; usesc
as a comparison object.使用
c
作为比较对象。
and 和
i
andj
satisfy input iterator requirements and refer to elements implicitly convertible tovalue_type
,[i,j)
denotes a valid range,i
和j
满足输入迭代器要求并引用可隐式转换为value_type
元素,[i,j)
表示有效范围,
Note that "and inserts elements" here is not marked up to indicate the insert
function, but I suppose we may interpret it that way. 请注意,此处的“和插入元素” 未标记为表示
insert
函数,但我想我们可以这样解释它。 Also note that i
and j
are input iterators, so must be traversed in order. 另请注意,
i
和j
是输入迭代器,因此必须按顺序遍历。
. 。
(It is slightly harder to find this information, because the equivalent tables all have (找到这些信息稍微困难一些,因为相同的表格都有
il
designates an object of typeinitializer_list<value_type>
il
指定类型为initializer_list<value_type>
的对象
above them, so can be found by searching for initializer_list
, but for this table the word is split over two lines, with a hyphen at the break.) 在它们之上,所以可以通过搜索
initializer_list
找到,但是对于这个表,这个单词被分成两行,在断点处有一个连字符。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.