set的用法 STL

来源:互联网 发布:linux ldconfig 编辑:程序博客网 时间:2024/05/17 22:07

MZL's simple problem

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 297    Accepted Submission(s): 134


Problem Description
A simple problem
Problem Description
You have a multiple set,and now there are three kinds of operations:
1 x : add number x to set
2 : delete the minimum number (if the set is empty now,then ignore it)
3 : query the maximum number (if the set is empty now,the answer is 0)
 

Input
The first line contains a number N (N106),representing the number of operations.
Next N line ,each line contains one or two numbers,describe one operation.
The number in this set is not greater than 109.
 

Output
For each operation 3,output a line representing the answer.
 

Sample Input
61 21 331 31 43
 

Sample Output
34
 

来源: <http://acm.hdu.edu.cn/showproblem.php?pid=5349>
 
题意:
有三种操作,1是插入元素后面的值,2是删除最小的数(相同的数也删除),
3是查询最大的值。用set时注意迭代时
解析:
set的含义是集合,并且是排好序的一个集合,从大到小,支持插入,删除,查找等操作,时间复杂度在logn的时间内,插入相同的元素时,只存储一个,不会重复。但是注意早查询的迭代器的变化。但是同一个数有几个可以查询,他存储了。
set的用法:在最后的附加

代码:
#include <iostream>
#include <cstdio>
#include <set>
 
using namespace std;
set<int>d;
int main()
{
int n;
scanf("%d", &n);
d.clear();
while (n--)
{
 
int m;
scanf("%d", &m);
if (m == 1)
{
int t;
scanf("%d", &t);
d.insert(t);
}
if (m == 2)
{
if (!d.empty())
d.erase(d.begin());
}
if (m == 3)
{
if (d.empty())
printf("0\n");
else
printf("%d\n", *(--d.end()));
}
}
return 0;
}
附加:

1set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就 像一个集合一样。所有的操作的都是严格在logn时间之内完成,效率非常高。 setmultiset的区别是:set插入的元素不能相同,但是multiset可以相同。

 创建 multiset<ss> base;

 删除:如果删除元素a,那么在定义的比较关系下和a相等的所有元素都会被删除

 base.count( a )set能返回0或者1,multiset是有多少个返回多少个.

 Setmultiset都是引用<set>头文件,复杂度都是logn

2Set中的元素可以是任意类型的,但是由于需要排序,所以元素必须有一个序,即大小的比较关系,比如 整数可以用<比较.

3,自定义比较函数;

 include<set>

 typedef struct

 { 定义类型 }

 ss(类型名);

 struct cmp

 {

 bool operator()( const int &a, const int &b ) const

 { 定义比较关系<}

 };

 (运算符重载,重载<)

 set<ss> base; ( 创建一个元素类型是ss,名字是baseset )

 注:定义了<,==和>以及>=,<=就都确定了,STL的比较关系都是用<来确定的,所以必须通 过定义< --严格弱小于来确定比较关

4set的基本操作:

begin() 返回指向第一个元素的迭代器

clear() 清除所有元素

count() 返回某个值元素的个数

empty() 如果集合为空,返回true

end() 返回指向最后一个元素的迭代器

equal_range() 返回集合中与给定值相等的上下限的两个迭代器

erase() 删除集合中的元素

find() 返回一个指向被查找到元素的迭代器

get_allocator() 返回集合的分配器

insert() 在集合中插入元素

lower_bound() 返回指向大于(或等于)某值的第一个元素的迭代器

key_comp() 返回一个用于元素间值比较的函数

max_size() 返回集合能容纳的元素的最大限值

rbegin() 返回指向集合中最后一个元素的反向迭代器

rend() 返回指向集合中第一个元素的反向迭代器

size() 集合中元素的数目

swap() 交换两个集合变量

upper_bound() 返回大于某个值元素的迭代器

value_comp() 返回一个用于比较元素间的值的函数

5,自定义比较函数:

For example

#include<iostream>

#include<set>

using namespace std;

typedef struct {

int a,b;

char s;

}newtype;

struct compare //there is no ().

{

bool operator()(const newtype &a, const newtype &b) const

{

return a.s<b.s;

}

};//the “; ” is here;

set<newtype,compare>element;

int main()

{

newtype a,b,c,d,t;

a.a=1; a.s='b';

b.a=2; b.s='c';

c.a=4; c.s='d';

d.a=3; d.s='a';

element.insert(a);

element.insert(b);

element.insert(c);

element.insert(d);

set<newtype,compare>::iterator it;

for(it=element.begin(); it!=element.end();it++)

cout<<(*it).a<<" ";

cout<<endl;

for(it=element.begin(); it!=element.end();it++)

cout<<(*it).s<<" ";

}

element自动排序是按照char s的大小排序的;

6.其他的set构造方法;

#include <iostream>

#include <set>

using namespace std;

bool fncomp (int lhs, int rhs) {return lhs<rhs;}

struct classcomp {

 bool operator() (const int& lhs, const int& rhs) const

 {return lhs<rhs;}

};

int main ()

{

 set<int> first; // empty set of ints

 int myints[]= {10,20,30,40,50};

 set<int> second (myints,myints+5); // pointers used as iterators

 set<int> third (second); // a copy of second

 set<int> fourth (second.begin(), second.end()); // iterator ctor.

 set<int,classcomp> fifth; // class as Compare

 bool(*fn_pt)(int,int) = fncomp;

 set<int,bool(*)(int,int)> sixth (fn_pt); // function pointer as Compare

 return

0 0
原创粉丝点击