C++ STL 关联型容器

16 篇文章 0 订阅
5 篇文章 0 订阅

关联型容器

map 的特性是,所有元素会根据元素的键值自动被排序,map 的所有元素都是 pair,
同时拥有实值(Value)和键值(Key)。pair 的第一元素被视为键值,第二个元素被视为
实值。map 不允许两个元素拥有相同的键值。

map

// map
#include <iostream>
#include <map>
using namespace std;

int main(){

    map<int ,string ,less<int> >mis={
            pair<int,string>(1,"apple"),
            pair<int,string>(2,"banana"),
            pair<int,string>(3,"orange"),
    };

    mis.insert(pair<int,string>(4,"grape"));
    mis.insert(pair<int,string>(4,"xxx"));//重复的key,会舍弃掉
    mis.insert(map<int,string>::value_type(5,"pear"));
    mis.insert(make_pair<int,string>(6,"pineapple"));

    mis[4]="66";//修改某个key的值

    map<int,string>::iterator it;
    for(it=mis.begin();it!=mis.end();it++){
        cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}

multimap

//todo  multimap
#include <iostream>
#include <map>
using namespace std;

int main(){

    multimap<int ,string ,less<int> >mis={
            pair<int,string>(1,"apple"),
            pair<int,string>(2,"banana"),
            pair<int,string>(3,"orange"),
    };

    mis.insert(pair<int,string>(4,"grape"));
    mis.insert(pair<int,string>(4,"xxx"));
    mis.insert(map<int,string>::value_type(5,"pear"));
    mis.insert(make_pair<int,string>(6,"pineapple"));

    //mis[4]="66";//修改某个key的值 multimap不支持

     //插入
    mis.insert(mis.find(100),pair<int,string>(100,"100"));
    mis.insert(mis.find(100),pair<int,string>(100,"100"));

    mis.erase(100); //删除某个key

    auto i= mis.find(1);
    if(i!=mis.end()){
        mis.erase(i);//删除某个元素
    }


    //mis.clear(); //清空

    map<int,string>::iterator it;
    for(it=mis.begin();it!=mis.end();it++){
        cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}

equal_range 与 lower_bound 与 upper_bound

//todo
#include <iostream>
#include <map>
using namespace std;

int main(){
    multimap<int,string > mis;
    mis = {
            pair<int,string>(1,"a"),
            pair<int,string>(2,"abc"),
            pair<int,string>(3,"abcd"),
            pair<int,string>(4,"abcde"),
            pair<int,string>(5,"abcdef"),
            pair<int,string>(5,"abcdef11"),
            pair<int,string>(5,"abcdef1111"),
            pair<int,string>(6,"abcdefg"),
            pair<int,string>(6,"abcdefg6666"),
            pair<int,string>(7,"abcdefgh"),
            pair<int,string>(8,"abcdefghi"),
            pair<int,string>(9,"abcdefghij"),
            pair<int,string>(10,"abcdefghijk")
    };

//    auto lower_bound = mis.lower_bound(5);
//    auto upper_bound = mis.upper_bound(5);
//    cout << "lower_bound: " << lower_bound->first << " " << lower_bound->second << endl;
//    cout << "upper_bound: " << upper_bound->first << " " << upper_bound->second << endl;
//    mis.erase(lower_bound, upper_bound);

    auto equal_range = mis.equal_range(5);
    cout << "equal_range: " << equal_range.first->first << " " << equal_range.second->first << endl;
    mis.erase(equal_range.first, equal_range.second);

    for(auto it=mis.begin();it!=mis.end();it++){
        cout<<it->first<<" "<<it->second<<endl;
    }
    return 0;
}

set

STL(标准模板库)中的 set 是 C++ 标准库提供的一种容器,
用于存储唯一的元素,并保持这些元素的有序状态。set 基于红黑树实现,
因此插入、删除和查找操作的时间复杂度均为 O(log n)。

#include <iostream>
#include <set>

int main() {
    // 定义一个存储 int 类型元素的 set
    std::set<int> mySet;

    // 插入元素
    mySet.insert(10);
    mySet.insert(20);
    mySet.insert(30);
    mySet.insert(10); // 重复插入,set 中不会出现重复元素

    // 输出 set 中的元素
    std::cout << "Elements in set: ";
    for (auto it = mySet.begin(); it != mySet.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // 查找元素
    if (mySet.find(20) != mySet.end()) {
        std::cout << "Element 20 found in set." << std::endl;
    } else {
        std::cout << "Element 20 not found in set." << std::endl;
    }

    // 删除元素
    mySet.erase(20);

    // 再次输出 set 中的元素
    std::cout << "Elements in set after erasing 20: ";
    for (auto it = mySet.begin(); it != mySet.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // 获取 set 的大小
    std::cout << "Size of set: " << mySet.size() << std::endl;

    // 清空 set
    mySet.clear();

    // 检查 set 是否为空
    if (mySet.empty()) {
        std::cout << "Set is now empty." << std::endl;
    } else {
        std::cout << "Set is not empty." << std::endl;
    }

    return 0;
}

Unordered Container 无序关联容器

无序关联容器(Unordered Associative Containers)包括 unordered_set、unordered_multiset、unordered_map 和 unordered_multimap。

这些容器使用哈希表实现,因此在插入、删除和查找操作上具有平均 O(1) 的时间复杂度,但在最坏情况下可能达到 O(n)。

头文件

#include <unordered_set>

unordered_set

#include <iostream>
#include <unordered_set>

int main() {
    // 定义一个存储 int 类型元素的 unordered_set
    std::unordered_set<int> myUnorderedSet;

    // 插入元素
    myUnorderedSet.insert(10);
    myUnorderedSet.insert(20);
    myUnorderedSet.insert(30);
    myUnorderedSet.insert(10); // 重复插入,unordered_set 中不会出现重复元素

    // 输出 unordered_set 中的元素
    std::cout << "Elements in unordered_set: ";
    for (auto it = myUnorderedSet.begin(); it != myUnorderedSet.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // 查找元素
    if (myUnorderedSet.find(20) != myUnorderedSet.end()) {
        std::cout << "Element 20 found in unordered_set." << std::endl;
    } else {
        std::cout << "Element 20 not found in unordered_set." << std::endl;
    }

    // 删除元素
    myUnorderedSet.erase(20);

    // 再次输出 unordered_set 中的元素
    std::cout << "Elements in unordered_set after erasing 20: ";
    for (auto it = myUnorderedSet.begin(); it != myUnorderedSet.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // 获取 unordered_set 的大小
    std::cout << "Size of unordered_set: " << myUnorderedSet.size() << std::endl;

    // 清空 unordered_set
    myUnorderedSet.clear();

    // 检查 unordered_set 是否为空
    if (myUnorderedSet.empty()) {
        std::cout << "Unordered_set is now empty." << std::endl;
    } else {
        std::cout << "Unordered_set is not empty." << std::endl;
    }

    return 0;
}

unordered_map

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    // 定义一个存储 string 类型键和 int 类型值的 unordered_map
    std::unordered_map<std::string, int> myUnorderedMap;

    // 插入元素
    myUnorderedMap["apple"] = 10;
    myUnorderedMap["banana"] = 20;
    myUnorderedMap["cherry"] = 30;

    // 输出 unordered_map 中的元素
    std::cout << "Elements in unordered_map: " << std::endl;
    for (auto it = myUnorderedMap.begin(); it != myUnorderedMap.end(); ++it) {
        std::cout << it->first << ": " << it->second << std::endl;
    }

    // 查找元素
    std::string key = "banana";
    if (myUnorderedMap.find(key) != myUnorderedMap.end()) {
        std::cout << "Element with key '" << key << "' found: " << myUnorderedMap[key] << std::endl;
    } else {
        std::cout << "Element with key '" << key << "' not found." << std::endl;
    }

    // 删除元素
    myUnorderedMap.erase("banana");

    // 再次输出 unordered_map 中的元素
    std::cout << "Elements in unordered_map after erasing 'banana': " << std::endl;
    for (auto it = myUnorderedMap.begin(); it != myUnorderedMap.end(); ++it) {
        std::cout << it->first << ": " << it->second << std::endl;
    }

    // 获取 unordered_map 的大小
    std::cout << "Size of unordered_map: " << myUnorderedMap.size() << std::endl;

    // 清空 unordered_map
    myUnorderedMap.clear();

    // 检查 unordered_map 是否为空
    if (myUnorderedMap.empty()) {
        std::cout << "Unordered_map is now empty." << std::endl;
    } else {
        std::cout << "Unordered_map is not empty." << std::endl;
    }

    return 0;
}

unordered_multimap

#include <iostream>
#include <unordered_map>
#include <string>

int main() {
    // 定义一个存储 string 类型键和 int 类型值的 unordered_multimap
    std::unordered_multimap<std::string, int> myUnorderedMultimap;

    // 插入元素
    myUnorderedMultimap.insert({"apple", 10});
    myUnorderedMultimap.insert({"banana", 20});
    myUnorderedMultimap.insert({"cherry", 30});
    myUnorderedMultimap.insert({"apple", 40}); // 允许重复键

    // 输出 unordered_multimap 中的元素
    std::cout << "Elements in unordered_multimap: " << std::endl;
    for (auto it = myUnorderedMultimap.begin(); it != myUnorderedMultimap.end(); ++it) {
        std::cout << it->first << ": " << it->second << std::endl;
    }

    // 查找元素
    std::string key = "apple";
    auto range = myUnorderedMultimap.equal_range(key);
    if (range.first != range.second) {
        std::cout << "Elements with key '" << key << "' found: " << std::endl;
        for (auto it = range.first; it != range.second; ++it) {
            std::cout << it->first << ": " << it->second << std::endl;
        }
    } else {
        std::cout << "Element with key '" << key << "' not found." << std::endl;
    }

    // 删除元素
    myUnorderedMultimap.erase("banana"); // 删除所有键为 "banana" 的元素

    // 再次输出 unordered_multimap 中的元素
    std::cout << "Elements in unordered_multimap after erasing 'banana': " << std::endl;
    for (auto it = myUnorderedMultimap.begin(); it != myUnorderedMultimap.end(); ++it) {
        std::cout << it->first << ": " << it->second << std::endl;
    }

    // 获取 unordered_multimap 的大小
    std::cout << "Size of unordered_multimap: " << myUnorderedMultimap.size() << std::endl;

    // 清空 unordered_multimap
    myUnorderedMultimap.clear();

    // 检查 unordered_multimap 是否为空
    if (myUnorderedMultimap.empty()) {
        std::cout << "Unordered_multimap is now empty." << std::endl;
    } else {
        std::cout << "Unordered_multimap is not empty." << std::endl;
    }

    return 0;
}

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MarkTop1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值