C++(标准库):18---STL容器之(关联式容器set、multiset)

一、关联式容器概述

二、set

格式

  • set<value>:没有key与value之分,只有一个key数据,且默认为升序
  • 头文件:#include <set>

特点:

  • key不可以重复,默认为升序排序
  • 不提供下标操作(下标运算符和at函数)

初始化:

  • 如果没有给出初始化值,就默认为空容器

升序、降序操作

  • set的key默认为升序排序,我们也可以将key设置为显式地升序或者设置为降序排序
  • 下面两个标准库函数(less、greater)都在头文件#include <xfunctional>中
  • 先演示升序操作
//set<int> s; //升序
set<int, std::less<int>> s; //升序
s.insert(1);
s.insert(3);
s.insert(2);

for (const auto &w : s) {
    cout << w<< endl;
}

  • 演示降序操作
set<int, std::greater<int>> s;
s.insert(1);
s.insert(3);
s.insert(2);

for (const auto &w : s) {
    cout << w<< endl;
}

迭代器与遍历

  • 当解引用一个关联容器迭代器时,我们得到的是容器的value_type的值的引用
  • 重点:与map一样,set也只能够通过迭代器访问set的键值(key),但不能改变
set<int> iset = { 0,1,2,3,4 };
set<int>::iterator set_it = iset.begin();
while (set_it != iset.end()) {
    *set_it = 666; //错误,不能改变值
    cout << *set_it<< endl; //正确,可以读取值
}

添加元素(insert、emplace)

删除元素(erase)

演示案例:

  • 我们将字符串输入保存到map中,将不想要统计的字符串事先定义在set中。然后统计字符串出现的次数
#include <iostream>
#include <map>
#include <set>
#include <string>

using namespace std;

int main()
{
    map<string, size_t> word_count;
    set<string> exclude = { "The","a","but","and" };
    string word;

    while (cin >> word) {
        find如果没有查找到,返回set的尾后迭代器
        if(exclude.find(word)==exclude.end())
            ++word_count[word];
    }

    for (const auto &w : word_count) {
        cout <<w.first <<":"<<w.second<<((w.second>1)?"times":"time")<< endl;
    }
    return 0;
}

value的不重复性

  • set的key不可以重复,而multiset的key可以重复
  • 下面用一个案例来演示这种情景
#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main()
{
    vector<int> v;
    for (vector<int>::size_type i = 0; i < 10; i++) {
        v.push_back(i);
        v.push_back(i);
    }

    set<int> set1(v.begin(), v.end());
    multiset<int> set2(v.begin(), v.end());

    cout << v.size() << endl;   //20
    cout << set1.size() << endl;//10
    cout << set2.size() << endl;//20
    return 0;
}

其他操作

三、multiset

格式

  • multiset<value>:没有key与value之分,只有一个key数据且默认为升序
  • 头文件:#include <set>

特点

  • key可以重复(如果有相同的key,则相邻存储)
  • 不提供下标操作(下标运算符和at函数)

初始化

  • 如果没有给出初始化值,就默认为空容器

升序、降序操作

  • multiset的key默认为升序排序,我们也可以将key设置为显式地升序或者设置为降序排序
  • 下面两个标准库函数(less、greater)都在头文件#include <xfunctional>中
  • 演示升序操作
multiset<int, std::less<int>> s; //升序
//multiset<int> s; //升序

s.insert(1);
s.insert(3);
s.insert(2);

for (const auto &w : s) {
    cout << w<< endl;
}

  • 演示降序操作
multiset<int, std::greater<int>> s; //降序
s.insert(1);
s.insert(3);
s.insert(2);

for (const auto &w : s) {
    cout << w<< endl;
}

迭代器与遍历

添加元素(insert、emplace)

删除元素(erase)

其他操作

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

董哥的黑板报

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

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

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

打赏作者

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

抵扣说明:

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

余额充值