STL之set 【不重复的有序集合】

set集合,存储唯一的值,并按升序排序。

1.头文件:

#include <iostream>
#include <set>

2.创建set:

std::set<int> mySet; // 创建一个整数类型的 set

3.添加元素:

insert(value):向集合中插入元素。

    mySet.insert(1);
    mySet.insert(2);

4.删除元素:

  • erase(value):从集合中删除指定元素。
  • clear():删除集合中的所有元素。
    mySet.erase(3);

5.查找元素:

  • find(value):查找集合中是否存在特定元素,返回迭代器。
  • count(value):统计特定元素在集合中的出现次数(对于 std::set,要么是0,要么是1)。====>可以用来判断元素是否存在
    if (mySet.count(6) == 1) {
        cout << "Set中存在元素6" << endl;
    }
    else
    {
        cout << "Set中不存在元素6" << endl;
    }

    set<int>::iterator it;
    if (mySet.find(4) != mySet.end()) {
        cout << "the Set exists 4" << endl;
    }else {
        cout << "the Set donesn't exists 4" << endl;
    }

6.遍历set:使用迭代器

    set<int>::iterator it = mySet.begin();
    for (; it != mySet.end(); it++) {
        cout << *it << endl;//迭代器相当于指针,所以要对指针解引用
    }
#include <iostream>
#include <string>
#include <set>

using namespace std;

int main() {
    //1.创建set 无序不重复数组  {value1,value2,value3....}
    set<int>  mySet;

    //2.添加元素
    mySet.insert(1);
    mySet.insert(2);
    mySet.insert(3);
    mySet.insert(4);
    mySet.insert(5);
    mySet.insert(5);

    //3.删除元素  erase(value)
    mySet.erase(3);


    //4.用count(value)函数判断是否存在某个元素
    // 返回值 0,1
    if (mySet.count(6) == 1) {
        cout << "Set中存在元素6" << endl;
    }
    else
    {
        cout << "Set中不存在元素6" << endl;
    }


    //5.查找元素
    set<int>::iterator it = mySet.begin();
    for (; it != mySet.end(); it++) {
        cout << *it << endl;//迭代器相当于指针,所以要对指针解引用
    }
    it = mySet.find(4);// 返回值为4的迭代器
    cout <<"输出值为4的下一个元素:"<< * (++it) << endl;

    //6.遍历set
    for (int element : mySet) {
        cout << element << " ";
    }

    return 0;
}

7.综合示例:学生名单

#include <iostream>
#include <set>
#include <string>

using namespace std;


int main() {
    set<string> studentSet;

    // 添加学生名字到集合
    studentSet.insert("Alice");
    studentSet.insert("Bob");
    studentSet.insert("Charlie");
    studentSet.insert("David");

    // 重复添加相同的名字,不会有多个相同的名字出现
    studentSet.insert("Alice");

    // 查找学生名字是否存在
    string nameToFind = "Bob";
    if (studentSet.find(nameToFind) != studentSet.end()) {
        cout << nameToFind << " is in the set." << endl;
    }
    else {
        cout << nameToFind << " is not in the set." << endl;
    }


    // 删除学生名字
    string nameToDelete = "Charlie";
    studentSet.erase(nameToDelete);

    // 输出所有学生名字
    cout << "Student Names: ";
    for (const string& name : studentSet) {
        cout << name << " ";
    }
    cout << endl;

    return 0;
}

那么set的用法就讲到这里,下一章看unordered_set的用法。
关注我,为大家持续分享更多的内容,让学习变得更简单,与君共勉,共同成长。 也可以关注我的公众号CoderSong,查看更多精彩文章。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

西里小诸葛

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

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

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

打赏作者

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

抵扣说明:

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

余额充值