C++之关联式容器set和map的使用

目录

1、set的使用​编辑

1、初始化

2、遍历

3、查找​编辑

4、插入​编辑

5、不支持修改与下标

2、map的使用

1、初始化​编辑

2、遍历

3、map的下标(重点)


 

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <utility>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::set;
using std::map;
using std::pair;
using std::string;

void test()
{
    /* vector<int> vec; */
    /* vec.push_back(1); */

    //set的基本特征
    //1、元素是唯一的,不能重复
    //2、默认情况下,元素会按照从小到大的顺序进行排列
    //3、set的底层使用的是红黑树结构
    //
    //初始化
    set<int> number2;
    set<int> number = {1, 2, 5, 7, 9, 8, 5, 3, 2};

    //迭代器(看成是一个指针)
    //遍历
    set<int>::iterator it;
    for(it = number.begin(); it != number.end(); ++it)
    {
        cout << *it << "  ";
    }
    cout << endl;

    //auto可以让编译器自动推导变量的类型
    /* auto a = 10; */
    /* auto s1 = "hello"; */
    /* auto b;//error */
    for(auto &elem : number)
    {
        cout << elem << "  ";
    }
    cout << endl;

    cout << endl << "set的查找" << endl;
    size_t cnt1 = number.count(7);
    size_t cnt2 = number.count(10);
    cout << "cnt1 = " << cnt1 << endl;
    cout << "cnt2 = " << cnt2 << endl;

    /* auto it2 = number.find(3); */
    set<int>::iterator it2 = number.find(3);
    if(it2 == number.end())//迭代器判空
    {
        cout << "该元素不存在" << endl;
    }
    else
    {
        cout << "该元素存在, *it2 = "  << *it2 << endl;
    }

    cout << endl << "set的插入操作" << endl;
    pair<set<int>::iterator, bool> ret = number.insert(6);
    if(ret.second)
    {
        cout << "插入成功 : " << *ret.first << endl;
    }
    else
    {
        cout << "插入失败" << endl;
    }

    for(auto &elem : number)
    {
        cout << elem << "  ";
    }
    cout << endl;

    cout << endl << endl;
    vector<int> vec = {1, 4, 20, 10, 7, 0};
    number.insert(vec.begin(), vec.end());
    for(auto &elem : number)
    {
        cout << elem << "  ";
    }
    cout << endl;

    /* cout << endl << endl; */
    //set不支持下标访问
    /* cout << "number[0] = " << number[0] << endl;//error */

    cout << endl << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    /* *it = 100;//error,不能修改 */
}

void test00()
{
    pair<int, string> number = {1, "hello"};
    cout << number.first << "     " << number.second << endl;
}

void test2()
{
    //map的特征:
    //1、存放的是key-value类型,也就是pair类型,
    //key值是唯一的,不能重复
    //2、默认情况下, 会按照key值进行升序排列
    //3、map底层使用的也是红黑树
    //
    //初始化
    map<int, string> number = {
        pair<int, string>(1, "hello"),
        pair<int, string>(4, "hello"),
        pair<int, string>(2, "world"),
        {5, "wangdao"},
        {3, "wuhan"},
        {1, "hello"},
    };

    //遍历
    map<int, string>::iterator it;
    for(it = number.begin(); it != number.end(); ++it)
    {
        cout << it->first << "  " << it->second << endl;
    }
    cout << endl << endl;

    for(auto &elem : number)
    {
        cout << elem.first << "  " << elem.second << endl;
    }
    cout << endl << endl;

    cout << endl << "map的查找" << endl;
    size_t cnt1 = number.count(3);
    size_t cnt2 = number.count(7);
    cout << "cnt1 = " << cnt1 << endl;
    cout << "cnt2 = " << cnt2 << endl;

    /* auto it2 = number.find(3); */
    map<int,string>::iterator it2 = number.find(3);
    if(it2 == number.end())//迭代器判空
    {
        cout << "该元素不存在" << endl;
    }
    else
    {
        cout << "该元素存在, *it2 = "  
             << it2->first << "    "
             << it2->second << endl;
    }

    cout << endl << "map的插入操作" << endl;
    pair<map<int, string>::iterator, bool> ret 
        = number.insert(pair<int, string>(6, "kiki"));
    if(ret.second)
    {
        cout << "插入成功 : " 
             << ret.first->first << "   "
             << ret.first->second << endl;
    }
    else
    {
        cout << "插入失败" << endl;
    }

    for(auto &elem : number)
    {
        cout << elem.first << "  " << elem.second << endl;
    }

    cout << endl << endl;
    cout << "number[0] = " << number[0] << endl;//插入
    cout << "number[5] = " << number[5] << endl;//查找

    cout << endl << endl;
    for(auto &elem : number)
    {
        cout << elem.first << "  " << elem.second << endl;
    }

    cout << endl << endl;
    number[0] = "zhongguo";//修改
    number[1] = "zhongguo";//修改
    for(auto &elem : number)
    {
        cout << elem.first << "  " << elem.second << endl;
    }

}

int main(int argc, char **argv)
{
    test2();
    return 0;
}

test的运行结果:

1  2  3  5  7  8  9
1  2  3  5  7  8  9

set的查找
cnt1 = 1
cnt2 = 0
该元素存在, *it2 = 3

set的插入操作
插入成功 : 6
1  2  3  5  6  7  8  9


0  1  2  3  4  5  6  7  8  9  10  20


*it = 2

test2的运行结果:

1  hello
2  world
3  wuhan
4  hello
5  wangdao


1  hello
2  world
3  wuhan
4  hello
5  wangdao



map的查找
cnt1 = 1
cnt2 = 0
该元素存在, *it2 = 3    wuhan

map的插入操作
插入成功 : 6   kiki
1  hello
2  world
3  wuhan
4  hello
5  wangdao
6  kiki


number[0] =
number[5] = wangdao


0
1  hello
2  world
3  wuhan
4  hello
5  wangdao
6  kiki


0  zhongguo
1  zhongguo
2  world
3  wuhan
4  hello
5  wangdao
6  kiki

1、set的使用

1、初始化

2、遍历

3、查找

4、插入

5、不支持修改与下标

2、map的使用

1、初始化

2、遍历

3、map的下标(重点)

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

~|Bernard|

你的鼓励是我写下去最大的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值