unordered_map::insert
添加元素。
std::pair<iterator, bool> insert(const value_type& val);
iterator insert(iterator where, const value_type& val);
template<class InIt>
void insert(InIt first, InIt last);
template<class ValTy>
pair<iterator, bool> insert(ValTy&& val);
template<class ValTy>
iterator insert(const_iterator where, ValTy&& val);
参数
Parameter |
说明 |
InIt |
迭代器类型。 |
ValTy |
就地构造函数参数类型。 |
first |
范围开头插入的。 |
last |
范围的末尾插入的。 |
val |
要插入的值。 |
where |
在插入 (仅提示的容器)。 |
备注
第一个成员函数确定元素 X 是否存在于键具有相同顺序对该 val的序列。 否则,它创建这样一个元素 X 并将它初始化与 val。 函数来确定指定 X的迭代器 where 。 如果插入发生的事件,函数返回 std::pair(where, true)。 否则,该调用将返回 std::pair(where, false)。
,在搜索的控件序列中的起始位置插入点,第二个成员函数返回 insert(val).first,使用 where 。 (插入某些可能更快,可能会发生,如果插入点紧邻或遵循 where。)
元素顺序值的第三个成员函数插入,每 where 的范围内 [first, last),通过调用 insert(*where)。
最后两个成员函数的行为与前两个相同,不同之处在于, val 用于构造该插入的值。
如果在单个元素的插入时引发,容器未更改,并且异常来重新引发。 如果在多个组件的插入时引发,容器在稳定左侧,但未指定的状态和异常来重新引发。
示例
// std_tr1__unordered_map__unordered_map_insert.cpp
// compile with: /EHsc
#include <unordered_map>
#include <iostream>
#include <string>
typedef std::unordered_map<char, int> Mymap;
int main()
{
Mymap c1;
c1.insert(Mymap::value_type('a', 1));
c1.insert(Mymap::value_type('b', 2));
c1.insert(Mymap::value_type('c', 3));
// display contents " [c 3] [b 2] [a 1]"
for (Mymap::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
// insert with hint and reinspect
Mymap::iterator it2 = c1.insert(c1.begin(), Mymap::value_type('d', 4));
for (Mymap::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
// insert range and inspect
Mymap c2;
c2.insert(c1.begin(), c1.end());
for (Mymap::const_iterator it = c2.begin();
it != c2.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
// insert with checking and reinspect
std::pair<Mymap::iterator, bool> pib =
c1.insert(Mymap::value_type('e', 5));
std::cout << "insert(['a', 5]) success == "
<< std::boolalpha << pib.second << std::endl;
pib = c1.insert(Mymap::value_type('a', 6));
std::cout << "insert(['a', 5]) success == "
<< std::boolalpha << pib.second << std::endl;
for (Mymap::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
// The templatized versions move constructing elements
unordered_map<int, string> c3, c4;
pair<int, string> is1(1, "a"), is2(2, "b");
c3.insert(std::move(is1));
std::cout << "After the move insertion, c3 contains:" << std::endl
<< " " << c3.begin()->first
<< " => " << c3.begin()->second
<< std::endl;
c4.insert(c4.begin(), std::move(is2));
std::cout << "After the move insertion, c4 contains:" << std::endl
<< " " << c4.begin()->first
<< " => " << c4.begin()->second
<< std::endl;
return (0);
}
要求
**标题:**unordered_map
命名空间: std