set()函数 c++用法

SET()是一种包含已排序对象的关联容器。 set集合容器实现了红黑树(Red-Black Tree)的平衡二叉检索树的数据结构,在插入元素时,它会自动调整二叉树的排列,把元素放到适当的位置,它不会插入相同键值的元素,而采取忽略处理。

平衡二叉树的检索使用中序遍历算法,检索效率高于vector、deque和list等容器。

对于set容器中的键值,不可直接去修改!应该先删除该键值,再插入新的键值。

c++ stl集合set介绍

   c++ stl集合(Set)是一种包含已排序对象的关联容器。set/multiset会根据待定的排序准则,自动将元素排序。两者不同在于前者不允许元素重复,而后者允许。

1) 不能直接改变元素值,因为那样会打乱原本正确的顺序,要改变元素值必须先删除旧元素,则插入新元素

2) 不提供直接存取元素的任何操作函数,只能通过迭代器进行间接存取,而且从迭代器角度来看,元素值是常数

3) 元素比较动作只能用于型别相同的容器(即元素和排序准则必须相同)

set模板原型://Key为元素(键值)类型
1
    
template  < class  Key,  class  Compare=less<Key>,  class  Alloc=STL_DEFAULT_ALLOCATOR(Key) >

从原型可以看出,可以看出比较函数对象及内存分配器采用的是默认参数,因此如果未指定,它们将采用系统默认方式。

set的各成员函数列表如下:

c++ stl容器set成员函数:begin()--返回指向第一个元素的迭代器

c++ stl容器set成员函数:clear()--清除所有元素

c++ stl容器set成员函数:count()--返回某个值元素的个数

c++ stl容器set成员函数:empty()--如果集合为空,返回true

c++ stl容器set成员函数:end()--返回指向最后一个元素下一个位置的迭代器

c++ stl容器set成员函数:equal_range()--返回集合中与给定值相等的上下限的两个迭代器

c++ stl容器set成员函数:erase()--删除集合中的元素

c++ stl容器set成员函数:find()--返回一个指向被查找到元素的迭代器

c++ stl容器set成员函数:get_allocator()--返回集合的分配器

c++ stl容器set成员函数:insert()--在集合中插入元素

c++ stl容器set成员函数:lower_bound()--返回指向大于(或等于)某值的第一个元素的迭代器

c++ stl容器set成员函数:key_comp()--返回一个用于元素间值比较的函数

c++ stl容器set成员函数:max_size()--返回集合能容纳的元素的最大限值

c++ stl容器set成员函数:rbegin()--返回指向集合中最后一个元素的反向迭代器

c++ stl容器set成员函数:rend()--返回指向集合中第一个元素的反向迭代器

c++ stl容器set成员函数:size()--集合中元素的数目

c++ stl容器set成员函数:swap()--交换两个集合变量

c++ stl容器set成员函数:upper_bound()--返回大于某个值元素的迭代器

c++ stl容器set成员函数:value_comp()--返回一个用于比较元素间的值的函数

set的应用例子:

Nuanran's Idol II

You have known that nuanran is a loyal fan of Kelly from the last contest. For this reason, nuanran is interested in collecting pictures of Kelly. Of course, he doesn't like each picture equally. So he gives each picture a score which is a positive integer. And the larger the score is, the more he likes that picture. Nuanran often goes to buy new pictures in some shops to increase his picture collection.

Sometimes nuanran's friends ask him for Kelly's picture and he will choose the picture having the smallest score. Because he has many pictures, this is a boring task. Can you help him again?

Input

For each test case, the first line gives an integer n (1 ≤ n ≤ 100000). Then n lines follow, each line has one of the following two formats.

    "B S". Denoting nuanran buys a new picture and S is the score of that picture.
    "G". Denoting nuanran gives a picture to his friends.

The input is terminated when n=0.

Output

For each giving test case, you should output the score of the picture nuanran gives to his friends.

Sample Input

8
B 20
B 10
G
B 9
G
B 100
B 25
G
0

Sample Output

10
9
20

 

//set的应用例子 Nuanran's Idol

#include <iostream>
#include <set>
using namespace std;

int main()
{
	multiset <int> s;
	multiset <int>::iterator it;
	int n, num;
	char c;
	cin >> num;
	for (int i = 0; i < num; i++)
	{
		cin >> c;
		if (c == 'B')
		{
			cin >> n;
			s.insert(n);
		}
		if (c == 'G')
		{
			it = s.begin();
			cout << *it;
			s.erase(*it);
		}
	}
	cin >> num;
	if (num == 0)
		return 0;
}

部分内容转自:set()函数 c++用法_咸鱼一号的博客的博客-CSDN博客_set函数

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值