/*直接构造并插入元素*/
std::pair<std::unordered_map<int,int>::iterator,bool> pair1 = unordermap1.emplace(1,10);
std::cout <<"emplace result is "<<std::boolalpha<<pair1.second<<std::endl;
std::cout <<"pair1 iterator key is "<<pair1.first->first<<" pair1 iterator value is "<<pair1.first->second<<std::endl;/*直接构造并插入元素*/
std::pair<int,int>pair2(11,20);
std::pair<std::unordered_map<int,int>::iterator,bool> pair3 = unordermap1.emplace(std::move(pair2));
std::cout <<"emplace result is "<<std::boolalpha<<pair3.second<<std::endl;
std::cout <<"pair3 iterator key is "<<pair3.first->first<<" pair3 iterator value is "<<pair3.first->second<<std::endl;/*直接构造并在指定位置插入元素*/
std::unordered_map<int,int>::iterator itr1 = unordermap1.emplace_hint(++unordermap1.begin(),8,66);
std::cout <<"itr1 key is "<<itr1->first<<" itr1 value is "<<itr1->second<<std::endl;/*删除单个元素*/
std::unordered_map<int,int>::iterator itr2 = unordermap1.erase(unordermap1.begin());
std::cout <<"itr2 key is "<<itr2->first<<" itr2 value is "<<itr2->second<<std::endl;/*删除指定范围的元素*/
std::unordered_map<int,int>::iterator itr3 = unordermap1.erase(unordermap1.begin(),++unordermap1.begin());
std::cout <<"itr3 key is "<<itr3->first<<" itr3 value is "<<itr3->second<<std::endl;/*根据键值删除元素*/int iCount = unordermap1.erase(1);
std::cout <<"erace element count is "<<iCount<<std::endl;/*清空元素*/
unordermap1.clear();/*插入元素,返回键值对*/
std::pair<std::unordered_map<int,int>::iterator,int> pair4 = unordermap1.insert({1,0});
std::cout <<"insert result is "<<std::boolalpha<<pair4.second<<std::endl;
std::cout <<"pair4 iterator key is "<<pair4.first->first<<" pair4 iterator value is "<<pair4.first->second<<std::endl;/*使用移动语句插入元素,返回键值对*/
std::pair<int,int>pair5(2,0);
std::pair<std::unordered_map<int,int>::iterator,int> pair6 = unordermap1.insert(std::move(pair5));
std::cout <<"insert result is "<<std::boolalpha<<pair6.second<<std::endl;
std::cout <<"pair6 iterator key is "<<pair6.first->first<<" pair6 iterator value is "<<pair6.first->second<<std::endl;/*插入元素,返回迭代器*/
std::unordered_map<int,int>::iterator itr4 = unordermap1.insert(unordermap1.begin(),{3,3});
std::cout <<"insert key is "<< itr4->first <<",insert value is "<<itr4->second<<std::endl;/*使用移动语句插入元素,返回迭代器*/
std::pair<int,int>pair7(4,5);
std::unordered_map<int,int>::iterator itr5 = unordermap1.insert(unordermap1.begin(),std::move(pair7));
std::cout <<"insert key is "<< itr5->first <<",insert value is "<<itr5->second<<std::endl;/*初始化列表插入,无返回值*/
unordermap1.insert({{5,2},{7,3},{6,4}});/*使用迭代器指定范围插入元素,无返回值*/
std::unordered_map<int,int> unordermap8{{9,5},{10,6}};
unordermap1.insert(unordermap8.begin(),unordermap8.end());
元素查找
函数
返回值
功能
count()
std::size_t
返回给定键对应元素的数量
find()
迭代器
查找指定键对应元素的位置,未找到则返回end()
equal_range()
键值对
查找与指定键匹配的范围
/*判断某个键对应值的数量*/
std::cout<<unordermap1.count(3)<<std::endl;/*查找指定键的元素所在的位置*/
std::unordered_map<int,int>::iterator itr6 = unordermap1.find(4);
std::cout<<"find key is "<<itr6->first<<",find value is "<<itr6->second<<std::endl;/*返回给定键的范围*/auto ret7 = unordermap1.equal_range(1);if(unordermap1.end()!= ret7.first){
std::cout<<"first key is "<<ret7.first->first<<",first value is "<<ret7.first->second<<std::endl;}if(unordermap1.end()!= ret7.second){
std::cout<<"second key is "<<ret7.second->first<<",second value is "<<ret7.second->second<<std::endl;}