C++ STL-Set的用法

一、定义

        SET是存储排序键的关联容器,其中每个键都是唯一的,可以插入或删除但不能更改。

        即SET容器中的元素默认按升序排列并且只能存在一个相同的元素(插入多次相同元素等于只插入了一个)。

二、函数

1.insert()

        insert()函数的作用就是往集合中添加元素,只有一个参数,就是你想要添加的元素,无返回值。

2.begin()和end()

        begin()的返回值为指向第一个元素的迭代器;end()的返回值为指向最后一个元素的后一个位置的迭代器。

#include <set>;
#include <iostream>
using namespace std;
int main()
{
	set<int> S;
	S.insert(0);
	S.insert(1);
	S.insert(8);
	S.insert(3);
	S.insert(3);
	S.insert(3);
	S.insert(3);
	set<int>::iterator it;      //迭代器定义
	for(it=S.begin();it!=S.end();it++)        
	cout << *it;
}

遍历部分代码的第二种写法:

	for(auto it=S.begin();it!=S.end();it++){
		cout<<*it<<endl;

最后输出为 0138

3.count()

        count()函数是C++ STL中的内置函数,它返回元素在集合中出现的次数。由于set容器仅包含唯一元素,因此只能返回1或0。因此在set容器中的count()函数可以理解为检查set中是否存在某一元素,如:

#include <set>;
#include <iostream>
using namespace std;
int main()
{
	set<int> S;
	S.insert(0);
	S.insert(1);
	S.insert(8);
	S.insert(3);
	S.insert(3);
	S.insert(3);
	S.insert(3);
	cout<<count(3)<<" "<<count(2)<<endl;
    
}

最后输出结果为 1 0

4.erase()

        erase()函数用于删除某一指定的元素,若删除成功则返回1,删除失败则返回0。

#include <set>;
#include <iostream>
using namespace std;
int main()
{
	set<int> S;
	set<int>::iterator it;
	S.insert(0);
	S.insert(1);
	S.insert(8);
	S.insert(3);
	S.insert(3);
	S.insert(3);
	S.insert(3);
	cout  <<S.erase(3)<<" "<<S.erase(2) << endl;
	for (it = S.begin(); it != S.end(); it++)
		cout << *it<<" ";
}

输出结果为 1 0
                   0 1 8

注:

        erase(iterator)  ,删除迭代器iterator指向的元素;

        erase(first,second),删除迭代器first和second之间的元素;

        erase(key_value),删除键值为key_value的元素。

5.size()

       得到set容器中的元素个数。

6.find()

        find()函数用来查找你要寻找的元素是否在集合中,参数为要找的元素值,返回值是指向该元素的迭代器。

        如果容器中不存在你要寻找的元素,则程序会报错。

7.empty()

        判断set容器是否为空,为空则返回1,不为空则返回0。

8.clear()

        清空容器中的元素。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C++ STL库中的set是一个集合容器,其中的元素是唯一的,即相同的元素只会出现一次。set是按照一定的顺序排列的,通常默认情况下是升序排列。set可以进行快速的查找、插入和删除操作,时间复杂度为O(log n)。 以下是set的常用操作: 1. 插入元素:使用insert()函数,可以插入单个元素或一组元素。 2. 删除元素:使用erase()函数,可以删除单个元素、指定位置的元素或一个范围内的元素。 3. 查找元素:使用find()函数,可以查找指定元素是否存在于set中。 4. 获取set中元素个数:使用size()函数。 5. 判断set是否为空:使用empty()函数。 以下是一些set的常见用法: ``` #include <iostream> #include <set> using namespace std; int main() { // 创建一个set set<int> mySet; // 插入元素 mySet.insert(3); mySet.insert(1); mySet.insert(4); mySet.insert(2); mySet.insert(5); // 遍历set中的所有元素 for (auto it = mySet.begin(); it != mySet.end(); it++) { cout << *it << " "; } cout << endl; // 查找元素 if (mySet.find(4) != mySet.end()) { cout << "4 is found." << endl; } else { cout << "4 is not found." << endl; } // 删除元素 mySet.erase(4); // 遍历set中的所有元素 for (auto it = mySet.begin(); it != mySet.end(); it++) { cout << *it << " "; } cout << endl; // 获取set中元素个数 cout << "The size of set is " << mySet.size() << endl; // 判断set是否为空 if (mySet.empty()) { cout << "The set is empty." << endl; } else { cout << "The set is not empty." << endl; } return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值