//以普通方式传递参数
pair<iterator,bool> insert ( const value_type& val );
//以右值引用的方式传递参数
template <class P>
pair<iterator,bool> insert ( P&& val );
以上 2 种格式中,参数 val 表示要添加到容器中的目标键值对元素;该方法的返回值为 pair类型值,内部包含一个 iterator 迭代器和 bool 变量:有关右值引用,可阅读《C++右值引用详解》一文,这里不再做具体解释。
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
//创建空 umap 容器
unordered_map<string, string> umap;
//构建要添加的键值对
std::pair<string, string>mypair("STL教程", "http://c.biancheng.net/stl/");
//创建接收 insert() 方法返回值的pair类型变量
std::pair<unordered_map<string, string>::iterator, bool> ret;
//调用 insert() 方法的第一种语法格式
ret = umap.insert(mypair);
cout << "bool = " << ret.second << endl;
cout << "iter -> " << ret.first->first <<" " << ret.first->second << endl;
//调用 insert() 方法的第二种语法格式
ret = umap.insert(std::make_pair("Python教程","http://c.biancheng.net/python/"));
cout << "bool = " << ret.second << endl;
cout << "iter -> " << ret.first->first << " " << ret.first->second << endl;
return 0;
}
程序执行结果为:
bool = 1
iter -> STL教程 http://c.biancheng.net/stl/
bool = 1
iter -> Python教程 http://c.biancheng.net/python/
//以普通方式传递 val 参数
iterator insert ( const_iterator hint, const value_type& val );
//以右值引用方法传递 val 参数
template <class P>
iterator insert ( const_iterator hint, P&& val );
注意,以上 2 种语法格式中,虽然通过 hint 参数指定了新键值对添加到容器中的位置,但该键值对真正存储的位置,并不是 hint 参数说了算,最终的存储位置仍取决于该键值对的键的值。
举个例子:
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
//创建空 umap 容器
unordered_map<string, string> umap;
//构建要添加的键值对
std::pair<string, string>mypair("STL教程", "http://c.biancheng.net/stl/");
//创建接收 insert() 方法返回值的迭代器类型变量
unordered_map<string, string>::iterator iter;
//调用第一种语法格式
iter = umap.insert(umap.begin(), mypair);
cout << "iter -> " << iter->first <<" " << iter->second << endl;
//调用第二种语法格式
iter = umap.insert(umap.begin(),std::make_pair("Python教程", "http://c.biancheng.net/python/"));
cout << "iter -> " << iter->first << " " << iter->second << endl;
return 0;
}
程序输出结果为:
iter -> STL教程 http://c.biancheng.net/stl/
iter -> Python教程 http://c.biancheng.net/python/
template <class InputIterator>
void insert ( InputIterator first, InputIterator last );
[first, last)表示复制其它 unordered_map 容器中键值对的区域。
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
//创建并初始化 umap 容器
unordered_map<string, string> umap{ {"STL教程","http://c.biancheng.net/stl/"},
{"Python教程","http://c.biancheng.net/python/"},
{"Java教程","http://c.biancheng.net/java/"} };
//创建一个空的 unordered_map 容器
unordered_map<string, string> otherumap;
//指定要拷贝 umap 容器中键值对的范围
unordered_map<string, string>::iterator first = ++umap.begin();
unordered_map<string, string>::iterator last = umap.end();
//将指定 umap 容器中 [first,last) 区域内的键值对复制给 otherumap 容器
otherumap.insert(first, last);
//遍历 otherumap 容器中存储的键值对
for (auto iter = otherumap.begin(); iter != otherumap.end(); ++iter){
cout << iter->first << " " << iter->second << endl;
}
return 0;
}
程序输出结果为:
Python教程 http://c.biancheng.net/python/
Java教程 http://c.biancheng.net/java/
void insert ( initializer_list<value_type> il );
其中,il 参数指的是可以用初始化列表的形式指定多个键值对元素。
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
//创建空的 umap 容器
unordered_map<string, string> umap;
//向 umap 容器同时添加多个键值对
umap.insert({ {"STL教程","http://c.biancheng.net/stl/"},
{"Python教程","http://c.biancheng.net/python/"},
{"Java教程","http://c.biancheng.net/java/"} });
//遍历输出 umap 容器中存储的键值对
for (auto iter = umap.begin(); iter != umap.end(); ++iter){
cout << iter->first << " " << iter->second << endl;
}
return 0;
}
程序输出结果为:
STL教程 http://c.biancheng.net/stl/
Python教程 http://c.biancheng.net/python/
Java教程 http://c.biancheng.net/java/
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有