编程语言
首页 > 编程语言> > C++——STL(无序容器)

C++——STL(无序容器)

作者:互联网

无序容器

底层实现结构:哈希表 冲突解决方法为链地址法 快速查找(使用哈希函数)

名称备注
unordered_map键值不相等;不可重复;无序;
unordered_multimap可重复;
unordered_set键值相等;不可重复;有序;不可修改;
unordered_multiset可重复;

代码

(unordered_map)

#include<iostream>
#include<unordered_map>
#include<string>
using namespace std;

int main() {
    //声明
    unordered_map<string, string> umap;
    unordered_map<string, string> umap{
    {"Python教程","http://c.biancheng.net/python/"},
    {"Java教程","http://c.biancheng.net/java/"},
    {"Linux教程","http://c.biancheng.net/linux/"} };
    unordered_map<string, string> umap2(umap);
    unordered_map<string, string> umap2(++umap.begin(), umap.end());

    //遍历
    for (auto iter = umap.begin(); iter != umap.end(); iter++)
        cout << iter->first << " " << iter->second << endl;

    //查找
    auto iter = umap.find("Python教程");
    int count = umap.count("Python教程");

    //插入
    umap.insert(make_pair("STL教程", "http://c.biancheng.net/stl/"));//返回 pair<unordered_map<string,string>::iterator,bool>
    umap.insert(++umap2.begin(), umap2.end());
    umap.insert(++umap.begin(), { "Python教程","http://c.biancheng.net/python/" });
    umap.insert({ {"STL教程","http://c.biancheng.net/stl/"},
                  {"Python教程","http://c.biancheng.net/python/"},
                  {"Java教程","http://c.biancheng.net/java/"} });
    umap.emplace("STL教程", "http://c.biancheng.net/stl/");//返回 pair<unordered_map<string,string>::iterator,bool>
    umap.emplace_hint(umap.begin(),"STL教程", "http://c.biancheng.net/stl/");//返回 unordered_map<string,string>::iterator

    //删除
    umap.erase(umap.end());
    umap.erase("STL教程");
    umap.erase(++umap.begin(), umap.end());
    umap.clear();

    //交换
    umap.swap(umap2);

    return 0;
}

(unordred_multimap)

  1. 没有[]定位指定key
  2. 没有at() 成员函数
  3. 头文件仍是引用 unordered_map

(unordered_set)

  1. 不再以键对方式存储
  2. 没有[]定位值
  3. 没有at()成员函数
  4. 不可修改
#include<iostream>
#include<unordered_set>
#include<string>
using namespace std;

int main() {
    //声明
    unordered_set<string> uset;
    unordered_set<string> uset{
    "http://c.biancheng.net/c/",
    "http://c.biancheng.net/java/",
    "http://c.biancheng.net/linux/" };
    unordered_set<string> uset2(uset);
    unordered_set<string> umap2(++uset.begin(), uset.end());
    //删除
    uset.erase(uset.end());
    uset.erase("http://c.biancheng.net/c/");
    uset.erase(++uset.begin(), uset.end());

    return 0;
}

(unordered_multiset)

  1. 不再限定唯一值
  2. 头文件仍是引用 unordered_set

标签:http,STL,无序,C++,umap,uset,net,biancheng,unordered
来源: https://blog.csdn.net/Dedication_/article/details/116749946