set操作指南

复制代码
#include <iostream>
using namespace std;
#include <set>
#include <algorithm>

void printSet(const set<int>& st)
{
    for (auto it = st.begin(); it != st.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

//一、构造和赋值
/*
构造:
set<T> st;                    默认构造函数
set(const set & st);          拷贝构造函数
赋值:
set& operator=(const set & st);         重载等号操作符
*/
void stest01()
{
    set<int> s1;
    s1.insert(10);//不用p开头,只有insert
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
    printSet(s1);//自动升序插入

    set<int> s2(s1);
    printSet(s2);

    set<int> s3;
    s3 = s1;
    printSet(s3);
}

//二、大小和交换
/*
size();                        返回容器中元素的数目
empty();                       判断容器是否为空
swap(st);                      交换两个集合容器
*/
void stest02()
{
    set<int> s1;
    s1.insert(10);//不用p开头,只有insert
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
    printSet(s1);//自动升序插入

    if (s1.empty())
    {
        cout << "为空" << endl;
    }

    int size = s1.size();
    cout << "大小:" << size << endl;

    set<int> s2;
    s2.insert(11);//不用p开头,只有insert
    s2.insert(30);
    s2.insert(22);
    s2.insert(40);
    printSet(s2);//自动升序插入

    s1.swap(s2);
    cout << "交换后:" << endl;
    printSet(s1);
    printSet(s2);
}

//三、插入和删除
/*
insert(elem);                  在容器中插入元素
clear();                       清除所有元素
erase(pos);                    删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg, end);               删除区间[beg, end)所有元素,返回下一个元素的迭代器
erase(elem);                   删除容器中值为elem的元素
*/
void stest03()
{
    set<int> s1;
    s1.insert(10);//不用p开头,只有insert
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
    printSet(s1);//自动升序插入

    //删除
    s1.erase(s1.begin());
    s1.erase(30);
    printSet(s1);//自动升序插入
    s1.erase(s1.begin(), s1.end());
    s1.clear();
}

//四、查找和统计
/*
find(key);                         查找key是否存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);                        统计key的元素个数
*/
void stest04()
{
    set<int> s1;
    s1.insert(10);//不用p开头,只有insert
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
    printSet(s1);//自动升序插入

    set<int>::iterator pos = s1.find(30);
    if (pos != s1.end())
    {
        cout << "找到元素" << *pos << endl;
    }
    else
    {
        cout << "未找到元素" << endl;
    }
    int num = s1.count(300);//对于set而言,统计结果要么是0,要么是1
    cout << "num:" << num << endl;
}

//五、和multiset的区别
/*
set不可以插入重复数据,而multiset可以
set插入数据的同时会返回插入结果,表示插入是否成功过.(返回对组)
multiset不会检测数据,因此可以插入重复数据
*/
void stest05()
{
    set<int> s1;
    s1.insert(10);
    pair <set<int>::iterator, bool> ret = s1.insert(10);
    if (ret.second)
    {
        cout << "第二次插入成功" << endl;
    }
    else
    {
        cout << "第二次插入失败" << endl;
    }

    multiset<int> s2;
    s2.insert(10);
    s2.insert(10);
    s2.insert(10);
    s2.insert(10);
    for (multiset<int>::iterator it = s2.begin(); it != s2.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
}

//六、内置类型指定排序规则
/*
set容器默认排序规则为从小到大,掌握如何改变排序规则
利用仿函数,可以改变排序规则
*/
class MyCompare
{
public:
    bool operator()(int v1, int v2) const
    {
        return v1 > v2;
    }
};

void stest06()
{
    set<int, MyCompare> s1;

    s1.insert(10);//不用p开头,只有insert
    s1.insert(30);
    s1.insert(20);
    s1.insert(40);
    
    for (set<int, MyCompare>::iterator it = s1.begin(); it != s1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
    
}

//七、自定义数据类型指定排序规则
class Person
{
public:
    Person(string name, int age)
    {
        this->m_name = name;
        this->m_age = age;
    }
    string m_name;
    int m_age;
};
class Compare2
{
public:
    bool operator()(const Person& p1, const Person& p2) const
    {
        return p1.m_age > p2.m_age;
    }
};
void stest07()
{
    set<Person, Compare2> s1;
    Person p1("刘备", 22);
    Person p2("关羽", 23);
    Person p3("张飞", 21);
    s1.insert(p1);
    s1.insert(p2);
    s1.insert(p3);
    for (auto it = s1.begin(); it != s1.end(); it++)
    {
        cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
    }
}

int main()
{
    stest07();
    return 0;
}
复制代码

 

posted @   花与不易  阅读(252)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示