set的基本用法

set是一棵红黑树,在一些操作上十分高效,还具有许多priority_queue没有的功能

下面是基本用法

//set
#include<set>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;

set<int> s;

int main()
{
    int n; 
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int t; 
        cin >> t;
        s.insert(t);
    }


//遍历:   
    set<int>::iterator it;    //正向迭代器 
    for(it = s.begin(); it != s.end(); it++)    //正向遍历(即由小到大输出) 
        cout << *it << " ";

    cout << endl;

    set<int>::reverse_iterator rit;    //反向迭代器 
    for(rit = s.rbegin(); rit != s.rend(); rit++)    //反向遍历(即由大到小输出) 
        cout << *rit << " ";

//元素的删除:可以删除迭代器上的元素,等于某键值的元素,区间上的元素和清空集合
    it = s.begin();
    for(int i = 1; i <= 2; i++)
        s.erase(it++);

    for(it = s.begin(); it != s.end(); it++)    
        cout << *it << " ";
    cout << endl;

    s.clear();
    for(it = s.begin(); it != s.end(); it++)    
        cout << *it << " ";
    cout << endl;

//元素的检索: 用s.find(),如果找到,则返回迭代器位置,否则返回s.end();
    s.insert(5);
    cout << *s.find(5) << endl;
    if(s.find(20) == s.end()) cout << "NO";


    return 0;
}

很多用到set时需要进行重载运算符,用法如下

//set
#include<set>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;

struct mycmp        //当set中的元素不是结构体时 
{
    bool operator () (const int &a, const int &b)
    {
        return a%10 < b%10;
    }
};
set<int, mycmp> s1;

struct point
{
    int x, y;
    bool operator < (const point &a) const      //当set中的元素是结构体时,直接写在结构体中 
    {
        return x < a.x;     //按照x从小到大排序 
    }
};
set<point> s2;

int main()
{
    int n; cin >> n;
    for(int i = 1; i <= n; i++)
    {
        int t; cin >> t; s1.insert(t);
    }

    set<int, mycmp>::iterator it1;

    for(it1 = s1.begin(); it1 != s1.end(); it1++)
        cout << *it1 <<  " ";
    cout << endl;

    for(int i = 1; i <= n; i++)
    {
        point t;
        cin >> t.x >> t.y;
        s2.insert(t);
    }
    set<point>::iterator it2;
    for(it2 = s2.begin(); it2 != s2.end(); it2++)
        cout << (*it2).x << " ";


    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
unordered_set是C++标准库中的一个容器,用于存储唯一的元素集合,且元素的顺序是无序的。以下是unordered_set的使用方法: 1. 包含头文件:首先需要包含<unordered_set>头文件。 2. 定义和初始化:可以使用不同类型的元素进行初始化unordered_set对象,例如: ```cpp std::unordered_set<int> mySet; // 定义一个存储int类型的unordered_set std::unordered_set<std::string> mySet2 = {"apple", "banana", "orange"}; // 使用初始化列表初始化unordered_set ``` 3. 插入元素:使用insert()函数将元素插入到unordered_set中,例如: ```cpp mySet.insert(10); // 插入元素10 mySet.insert(20); // 插入元素20 ``` 4. 查找元素:使用find()函数可以在unordered_set中查找指定元素,如果元素存在,返回指向该元素的迭代器;如果元素不存在,返回unordered_set::end()。例如: ```cpp auto it = mySet.find(10); // 查找元素10 if (it != mySet.end()) { std::cout << "Element found: " << *it << std::endl; } else { std::cout << "Element not found" << std::endl; } ``` 5. 删除元素:使用erase()函数可以从unordered_set中删除指定元素或范围内的元素,例如: ```cpp mySet.erase(10); // 删除元素10 ``` 6. 遍历元素:使用迭代器可以遍历unordered_set中的所有元素,例如: ```cpp for (auto it = mySet.begin(); it != mySet.end(); ++it) { std::cout << *it << " "; } std::cout << std::endl; ``` 7. 其他操作:unordered_set还提供了其他一些常用的操作,如判断是否为空(empty())、获取元素个数(size())等。 这是unordered_set基本使用方法,你可以根据需求进行扩展和深入学习。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值