set和multiset容器(20221123)

本文详细介绍了C++标准库中的set与multiset容器的基本概念、主要区别及使用方法,包括构造、赋值、大小操作、插入删除、查找统计等功能,并通过示例代码展示如何运用这些容器。

1、基本概念

所有元素在插入时会自动排好序;

属于关联式容器,底层结构是用二叉树实现的

2、set和multiset的区别

set中不允许有重复元素,multiset允许有重复元素。

3、构造和赋值

构造:

set<T>st; //默认构造

set<const set &st>; //拷贝构造函数

赋值:

重载等号操作符 =

void printSet(set<int>&s)

{

    for (set<int>::iterator it = s.begin(); it != s.end(); it++)\

    {

        cout << *it << " ";

    }

    cout << endl;

}





void test01()

{

    set<int>s1;

    //插入数据只能用 insert

    s1.insert(10);

    s1.insert(20);

    s1.insert(5);

    s1.insert(20);

    printSet(s1);//5 10 20 自动排序 且去除重复元素

    set<int>s2(s1);//拷贝构造

    printSet(s2); //5 10 20

}

4、set大小和交换操作

set容器不能重新指定大小

函数原型:

size(); //返回容器中的元素格式

empty(); //判断是否为空

swap(); // 交换两个集合容器

void test02()

{

    set<int>s1;

    //插入数据只能用 insert

    s1.insert(1000);

    s1.insert(200);

    s1.insert(5);

    s1.insert(180);

    printSet(s1);//5 180 200 1000自动排序 且去除重复元素

    if (s1.empty())

    {

        cout << "为空" << endl;

    }

    else

    {

        cout << "不为空" << endl;

        cout << "容器大小为:" << s1.size() << endl;

    }

    set<int>s2;

    s2.insert(9999);

    s1.swap(s2);

    cout << "交换后:" << endl;

    printSet(s1);

}

4、set容器的插入与删除

函数原型:

clear();//清空

erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器

erase(beg,end); //删除区间[beg,end]的所有元素,返回下一个元素的迭代器

erase(elem); //删除容器中值为elem的元素

void test03()

{

    set<int>s1;

    //插入数据只能用 insert

    s1.insert(1000);

    s1.insert(200);

    s1.insert(5);

    s1.insert(180);

    printSet(s1);//5 180 200 1000自动排序 且去除重复元素

    set<int>::iterator it = s1.begin();

    it++; // 将it指向第二个元素

    set<int>::iterator it1 = s1.end();

    it1--; //指向倒数第二个元素

    set<int>::iterator a;

    a = s1.erase(it, it1); //预计返回指向最大元素的迭代器

    cout << "*a=" << *a << endl; //1000

    printSet(s1); //输出最大的元素和最小元素 5 1000

}

5、set容器的查找与统计

函数原型:

find(key); //查找key是否存在 若存在返回该键的元素的迭代器,若不存在,则返回set.end()

count(key); //统计key的个数 0或1

void test04()

{

    set<int>s1;

    s1.insert(1000);

    s1.insert(200);

    s1.insert(5);

    s1.insert(180);

    s1.insert(30);

    printSet(s1);//5 30 180 200 1000自动排序 且去除重复元素

    set<int>::iterator pos = s1.find(30);

    if (pos != s1.end())

    {

        cout << "找到了元素" << *pos << endl;

    }

    else

    {

        cout << "元素不存在" << endl;

    }

    int num = s1.count(180);

    if (num == 0)

    {

        cout << "元素不存在" << endl;

    }

    else

    {

        cout << "元素个数为:" << num << endl;

    }

}

6、set和multiset的区别

set在insert会给一个pair类型的返回结果,而multiset不会,multiset会返回一个迭代器。

void printMSet(multiset<int>&ms)

{

    for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)\

    {

        cout << *it << " ";

    }

    cout << endl;

}



void test05()

{

    set<int>s1;

    s1.insert(1000);

    s1.insert(200);

    pair<set<int>::iterator, bool>ret = s1.insert(10);

    if (ret.second == true)

    {

        cout << "插入成功!"<<endl;

    }

    else

    {

        cout << "插入失败" << endl;

    }

    ret = s1.insert(10);

    if (ret.second == true)

    {

        cout << "插入成功!" << endl;

    }

    else

    {

        cout << "插入失败"<<endl;

    }

    multiset<int>ms;

    ms.insert(10);

    ms.insert(10);

    printMSet(ms);//10 10

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值