STL

MAP

map 是有序不重复的键值对 容器

SET

set 是有序不重复的集合

unordered_map

无序、键不重复的键值对容器,键(key)唯一,值(value)可修改,适用于需要通过键快速查找对应值的场景(如缓存存储、字典映射等)。

 

  1. 初始化

    // 1. 空构造
    unordered_map<string, int> um1;
     
    // 2. 初始化列表构造
    unordered_map<string, int> um2 = {
        {"apple", 10},
        {"banana", 20},
        {"orange", 15}
    };
    unordered_map<string, int> um3{{"cat", 5}, {"dog", 8}};
     
    // 3. 范围构造
    unordered_map<string, int> um4(um2.begin(), um2.end());
     
    // 4. 拷贝构造
    unordered_map<string, int> um5(um2);
     
    // 5. 移动构造
    unordered_map<string, int> um6(move(um5))

unordered_set

无序、不重复的元素集合,专注于元素的快速查找、插入和删除,适用于需要去重且无序遍历的场景(如快速判断元素是否存在、统计不重复元素等)

  1. 初始化

    // 1. 空构造
    unordered_set<int> us1;
     
    // 2. 初始化列表构造
    unordered_set<int> us2 = {1, 2, 3, 4, 5};
    unordered_set<int> us3{10, 20, 30};
     
    // 3. 范围构造(从其他容器拷贝)
    vector<int> v = {1, 2, 3, 4, 5};
    unordered_set<int> us4(v.begin(), v.end());
     
    // 4. 拷贝构造
    unordered_set<int> us5(us2);
     
    // 5. 移动构造(效率更高,不拷贝数据)
    unordered_set<int> us6(move(us5));
  2. 基本操作

    //=====================================插入元素==================================
    unordered_set<int> us;
    // 插入单个元素
    auto ret1 = us.insert(10);  // ret1.first是迭代器,ret1.second=true(插入成功)
    auto ret2 = us.insert(10);  // ret2.second=false(重复插入失败)
     
    // 插入多个元素
    us.insert({20, 30, 40});
     
    // 范围插入
    vector<int> v = {50, 60};
    us.insert(v.begin(), v.end());
      
    
    
    unordered_map<string, int> um;
    // 方式1:插入pair
    um.insert(pair<string, int>("apple", 10));
    um.insert(make_pair("banana", 20));  // 更简洁
     
    // 方式2:插入初始化列表
    um.insert({{"orange", 15}, {"grape", 25}});
     
    // 方式3:emplace()直接构造(避免pair拷贝,效率更高)
    um.emplace("pear", 30);  // 直接在容器中构造键值对
     
    // 方式4:[]运算符(插入或修改)
    um["mango"] = 35;  // 键不存在则插入,存在则修改值
    
    
    //=====================================查找元素==================================
    
    // unordered_set查找
    //find()
    unordered_set<int> us = {10, 20, 30, 40};
    auto it1 = us.find(20);
    if (it1 != us.end()) {
        cout << "找到元素:" << *it1 << endl;  // 输出20
    }
     
    // unordered_map查找
    unordered_map<string, int> um = {{"apple", 10}, {"banana", 20}};
    auto it2 = um.find("apple");
    if (it2 != um.end()) {
        cout << "键:" << it2->first << ",值:" << it2->second << endl;  // 输出apple 10
    }
    
    //count()
    unordered_set<int> us = {10, 20, 30};
    cout << us.count(20) << endl;  // 输出1
    cout << us.count(50) << endl;  // 输出0
     
    unordered_multiset<int> ums = {10, 20, 20, 30};
    cout << ums.count(20) << endl;  // 输出2
    
    
    //=====================================删除元素==================================
    
    
    // unordered_set删除
    unordered_set<int> us = {10, 20, 30, 40, 50};
     
    // 1. 按键删除,返回删除的元素个数
    size_t n1 = us.erase(30);  // n1=1(删除成功)
     
    // 2. 按迭代器删除,无返回值
    auto it = us.find(40);
    if (it != us.end()) {
        us.erase(it);
    }
     
    // 3. 按范围删除
    us.erase(us.begin(), us.end());  // 删除所有元素,等同于clear()
     
    // unordered_map删除
    unordered_map<string, int> um = {{"apple", 10}, {"banana", 20}, {"orange", 15}};
    um.erase("banana");  // 按键删除
    auto it_um = um.find("orange");
    if (it_um != um.end()) {
        um.erase(it_um);  // 按迭代器删除
    }
    
    //=====================================其他==================================
    
    // 清空容器
    us.clear();
    um.clear();
     
    // 获取容器大小
    cout << "size: " << us.size() << endl;
     
    // 判断容器是否为空
    if (us.empty()) {
        cout << "容器为空" << endl;
    }
     
    // 交换两个容器的内容
    unordered_set<int> us1 = {1, 2, 3};
    unordered_set<int> us2 = {4, 5, 6};
    us1.swap(us2);  // us1变为{4,5,6},us2变为{1,2,3}
     
    // 预留哈希桶空间(优化插入性能)
    us.reserve(1000);  // 预留至少1000个哈希桶,避免频繁扩容
    
    
    //=====================================遍历==================================
    
    //1、迭代器遍历
    // unordered_set迭代器遍历
    unordered_set<int> us = {10, 20, 30, 40};
    for (auto it = us.begin(); it != us.end(); ++it) {
        cout << *it << " ";  // 输出顺序不确定,如20 10 40 30
    }
    cout << endl;
     
    // unordered_map迭代器遍历
    unordered_map<string, int> um = {{"apple", 10}, {"banana", 20}, {"orange", 15}};
    for (auto it = um.begin(); it != um.end(); ++it) {
        cout << it->first << ":" << it->second << " ";  // 输出顺序不确定
    }
    cout << endl;
    
    
    
    
    //2、范围 for 遍历(C++11  及以上)这是最简洁的遍历方式:
    
    // unordered_set范围for
    unordered_set<int> us = {10, 20, 30, 40};
    for (auto e : us) {
        cout << e << " ";
    }
    cout << endl;
     
    // unordered_map范围for
    unordered_map<string, int> um = {{"apple", 10}, {"banana", 20}, {"orange", 15}};
    for (auto& p : um) {  // 使用引用避免拷贝,效率更高
        cout << p.first << ":" << p.second << " ";
    }
    cout << endl;
    
    
    //3、结构化绑定遍历(C++17 及以上)unordered_map 遍历的更优雅方式,直接拆分键值对:
    
    unordered_map<string, int> um = {{"apple", 10}, {"banana", 20}, {"orange", 15}};
    for (auto [key, value] : um) {  // 结构化绑定,直接获取key和value
        cout << key << ":" << value << " ";
    }
    cout << endl;

     

posted @ 2026-07-14 15:15  直至成伤  阅读(3)  评论(0)    收藏  举报