与set map 比,unordered_set-unordered_map底层是哈希hash,也就是无序。
内建数据类型:比如int ,string这样。
本篇代码实例为unordered_set-unordered_map。
set,map有序参看前篇 C++-STL(6)-map-set -有序-增删改查
set -unordered_set:
1.insert :unordered_set 输入顺序: 1、2、10、100、4、8、9, 输出顺序:9、1、10、2、4、100、8
set 输入顺序: 1、2、10、100、4、8、9, 输出顺序:1、2、10、100、4、8、9
2. lower_boundh,upper_bound:
无论是unordered_set还是set 都是按输出顺序查找的。
unordered_set增删改查
void hashset_int()
{
//1.定义 unordered_set<int> myset;
//2.插入insert <key>
//3.得到大小 int nSize = myset.size();
//4.查找 iter=myset.find(key) 输入key 返回iter
//5.定位 iter=lower_bound(key) <=key 返回iter
// 定位 iter = upper_bound(key) >=key 返回iter
//6.删除 int n=myset.erase(key) 返1 成功;返0 不成功
// 删除全部 mymap.clear();
cout << "*********************" << endl;
cout << "1.定义 unordered_set<int> myset;" << endl;
unordered_set<int> myset;
myset.insert(1);
myset.insert(2);
myset.insert(10);
myset.insert(100);
myset.insert(4);
myset.insert(8);
myset.insert(9);
cout << "2.插入insert <key>" << endl;
unordered_set<int>::iterator iter;
for (iter = myset.begin(); iter != myset.end(); iter++)
{
cout << *iter << endl;
}
cout << "auto 类型遍历" << endl;
for (auto iter1 = myset.begin(); iter1 != myset.end(); iter1++)
{
cout << *iter1 << endl;
}
cout << "3.得到大小 int nSize = myset.size();" << myset.size() << endl;
cout << "4.查找 iter=myset.find(100) " << endl;
iter = myset.find(100);
cout << *iter << endl;
cout << "5.定位 iter=lower_bound(100) <=key set 返回10" << endl;
iter = myset.lower_bound(100);
cout << *iter << endl;
cout << "6 定位 iter = upper_bound(10)>=key set返回" << endl;
iter = myset.upper_bound(10);
cout << *iter << endl;
cout << "6.删除 int n=myset.erase(key) 返1 成功;返0 不成功" << endl;
cout << "int n=" << myset.erase(8) << endl;
for (auto iter1 = myset.begin(); iter1 != myset.end(); iter1++)
{
cout << *iter1 << endl;
}
cout << " 删除全部 myset.clear();" << endl;
myset.clear();
cout << "删除后大小 int nSize = myset.size();" << myset.size() << endl;
}
set -unordered_map:
1.insert :unordered_map 输入顺序: a、z、b、d、f, 输出顺序:a、b、z、d、f
map 输入顺序: a、z、b、d、f 输出顺序:a、b、d、f、z
void hashmap_int()
{
cout<<"hashmap_int**************"<<endl;
//1.定义 map<char, int> mymap;
//2.插入insert pair<,>
//3.得到大小 int nSize = mymap.size();
//4.查找 iter=mymap.find() 输入key 返回iter
//5.删除 int n=mymap.erase(key) 返1 成功;返0 不成功
//6.删除全部 mymap.erase(mymap.begin(), mymap.end());
//7.修改mymap['d']=4000;
cout << "unordered_map 增删改查 ******" << endl;
cout << "1定义 map<char, int> mymap;" << endl;
cout << " 2插入insert pair<,>" << endl;
unordered_map <char, int> mymap;
mymap.insert(pair<char, int>('a', 300));
mymap.insert(pair<char, int>('z', 200));
mymap.insert(pair<char, int>('b', 300));
mymap['d'] = 700;
mymap.insert(unordered_map<char, int>::value_type('f', 500));
unordered_map<char, int>::iterator iter;
for (iter = mymap.begin(); iter != mymap.end(); iter++)
{
cout << iter->first << " " << iter->second << endl;
}
cout << " 3得到大小 int nSize = mymap.size();" << endl;
int nSize = mymap.size();
cout << nSize << endl;
cout << "4.查找 iter=mymap.find(b) 输入key 返回iter" << endl;
iter = mymap.find('b');
cout << iter->first << " " << iter->second << endl;
cout << "5.删除 int n=mymap.erase(key) 返1 成功;返0 不成功" << endl;
int n = mymap.erase('b');
cout << "删除后的返回值=" << n << endl;
for (iter = mymap.begin(); iter != mymap.end(); iter++)
{
cout << iter->first << " " << iter->second << endl;
}
cout << "6.修改mymap['d']=4000;" << endl;
mymap['d'] = 4000;
cout << mymap['d'] << endl;
cout << "7.删除全部 mymap.erase(mymap.begin(), mymap.end());" << endl;
mymap.erase(mymap.begin(), mymap.end());
nSize = mymap.size();
cout << "删除全部后map的长度" << nSize << endl;
}
原文链接: https://www.cnblogs.com/jasmineTang/p/14369307.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/327451
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!