C++ STL set和multiset的使用
C++ STL set和multiset的使用
std::set<int> s;那个s这个对象里面存贮的元素是从小到大排序的,(因为用std::less作为比较工具。)
1,set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就 像一个集合一样。所有的操作的都是严格在logn时间之内完成,效率非常高。 set和multiset的区别是:set插入的元素不能相同,但是multiset可以相同。
创建 multiset<ss> base;
删除:如果删除元素a,那么在定义的比较关系下和a相等的所有元素都会被删除
base.count( a ):set能返回0或者1,multiset是有多少个返回多少个.
Set和multiset都是引用<set>头文件,复杂度都是logn
2,Set中的元素可以是任意类型的,但是由于需要排序,所以元素必须有一个序,即大小的比较关系,比如 整数可以用<比较.
3,自定义比较函数;
include<set>
typedef struct
{ 定义类型 }
ss(类型名);
struct cmp
{
bool operator()( const int &a, const int &b ) const
{ 定义比较关系<}
};
(运算符重载,重载<)
set<ss> base; ( 创建一个元素类型是ss,名字是base的set )
注:定义了<,==和>以及>=,<=就都确定了,STL的比较关系都是用<来确定的,所以必须通 过定义< --“严格弱小于”来确定比较关
4,set的基本操作:
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() 返回一个用于比较元素间的值的函数
1:
set元素的插入:
- #include <iostream>
- #include <string>
- #include <set>
- using namespace std;
- void printSet(set<int> s)
- {
- set<int>::iterator i;
- for(i=s.begin();i!=s.end();i++)
- printf("%d ",*i);
- cout<<endl;
- }
- void main()
- {
- //创建空的set对象,元素类型为int,
- set<int> s1;
- for (int i = ; i <5 ; i++)
- s1.insert(i*10);
- printSet(s1);
- cout<<"s1.insert(20).second = "<<endl;;
- if (s1.insert(20).second)//再次插入20
- cout<<"Insert OK!"<<endl;
- else
- cout<<"Insert Failed!"<<endl;
- cout<<"s1.insert(50).second = "<<endl;
- if (s1.insert(50).second)
- {cout<<"Insert OK!"<<endl; printSet(s1);}
- else
- cout<<"Insert Failed!"<<endl;
- pair<set<int>::iterator, bool> p;
- p = s1.insert(60);
- if (p.second)
- {cout<<"Insert OK!"<<endl; printSet(s1);}
- else
- cout<<"Insert Failed!"<<endl;
- }
- 继续更新中
2: set 的 empty erase 删除特定元素
- #include <iostream>
- #include <set>
- using namespace std;
- int main ()
- {
- set<int> myset;
- myset.insert(20);
- myset.insert(30);
- myset.insert(10);
- while (!myset.empty())
- {
- cout <<" "<< *myset.begin();
- myset.erase(myset.begin());
- }
- cout << endl;
- return 0;
- }
- //set::find
- #include <iostream>
- #include <set>
- using namespace std;
- int main ()
- {
- set<int> myset;
- set<int>::iterator it;
- for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50
- it=myset.find(20);
- myset.erase (it);
- myset.erase (myset.find(40));
- myset.erase (30);
- cout << "myset contains:";
- for (it=myset.begin(); it!=myset.end(); it++)
- cout << " " << *it;
- cout << endl;
- return 0;
- }
lower_bound()返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个大于等于value 的值。 例如,有如下序列: ia[]={12,15,17,19,20,22,23,26,29,35,40,51}; 用值21调用lower_bound(),返回一个指向22的iterator。用值22调用lower_bound(),也返回一个指向22的iterator。
iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值> key的第一个元素。
- // 6.set::lower_bound/upper_bound
- #include <iostream>
- #include <set>
- using namespace std;
- int main ()
- {
- set<int> myset;
- set<int>::iterator it,itlow,itup;
- for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
- itlow=myset.lower_bound (30); // >=
- itup=myset.upper_bound (60); // >
- printf("%d %d",*itlow,*itup); // 30 70
- return 0;
- }
- // 7.set::equal_elements
- #include <iostream>
- #include <set>
- using namespace std;
- int main ()
- {
- set<int> myset;
- pair<set<int>::iterator,set<int>::iterator> ret;
- for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50
- ret = myset.equal_range(30);
- cout << "lower bound points to: " << *ret.first << endl;
- cout << "upper bound points to: " << *ret.second << endl;
- return 0;
- }
- //lower bound points to: 30
- //upper bound points to: 40
set结构体的应用
- #include<iostream>
- #include<set>
- using namespace std;
- struct haha
- {
- int a,b;
- char s;
- friend bool operator<(struct haha a,struct haha b)
- {
- return a.s<b.s;
- }
- };
- set<struct haha>element;
- int main()
- {
- struct haha 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(d);
- element.insert(b);
- element.insert(c);
- element.insert(a);
- set<struct haha>::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<<" ";
- }
集合的并集 交集 差集 等等
- #include<stdio.h>
- #include<string>
- #include<set>
- #include<iostream>
- #include <algorithm>//包含
- using namespace std;
- struct compare//自定义排序方式
- {
- bool operator ()(string s1,string s2)
- {
- return s1>s2;
- }///自定义一个仿函数
- };
- int main()
- {
- typedef set<string,compare> SET;
- SET s;//建立第一个集合
- s.insert(string("sfdsfd"));
- s.insert(string("apple"));
- s.insert(string("english"));
- s.insert(string("dstd"));
- cout<<"第一个集合s1为:"<<endl;
- set<string,compare>::iterator it = s.begin();
- while(it!=s.end())
- cout<<*it++<<" ";
- SET s2;//建立第二个集合
- s2.insert(string("abc"));
- s2.insert(string("apple"));
- s2.insert(string("english"));
- cout<<endl<<"第一个集合s2为:"<<endl;
- it = s2.begin();
- while(it!=s2.end())
- cout<<*it++<<" ";
- cout<<endl<<endl;
- string str[10];
- string *end =set_intersection(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//求交集,返回值指向str最后一个元素的尾端
- /*set_intersection包含于#include <algorithm> 头文件中 其中上面的不一定非要为set容器 也可以使数组 但是前提是要把2个数组都排好序才可以
- 返回值是一个指向交集序列末尾的迭代器 至于是什么迭代器与第5个参数有关 如果是数组 返回为int的迭代器 */
- cout<<"s1,s2的交集为:"<<endl;
- string *first = str;
- while(first<end)
- cout <<*first++<<" ";
- cout<<endl<<endl<<"s1,s2的并集为:"<<endl;
- end =set_union(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//并集
- first = str;
- while(first<end)
- cout <<*first++<<" ";
- cout<<endl<<endl<<"s2相对于s1的差集:"<<endl;
- first = str;
- end = std::set_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//s2相对于s1的差集
- while(first<end)
- cout <<*first++<<" ";
- cout<<endl<<endl<<"s1相对于s2的差集:"<<endl;
- first = str;
- end = std::set_difference(s2.begin(),s2.end(),s.begin(),s.end(),str,compare());//s1相对于s2的差集
- while(first<end)
- cout <<*first++<<" ";
- cout<<endl<<endl;
- first = str;
- end = std::set_symmetric_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//上面两个差集的并集
- while(first<end)
- cout <<*first++<<" ";
- cout<<endl;
- /*
- set<int> s3 ;
- set<int>::iterator iter = s3.begin() ;
- set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s3,iter));
- copy(s3.begin(),s3.end(), ostream_iterator<int>(cout," "));
- */
- }
另外一个实例
- /*set_intersection()算法计算两个集合[start1, end1)和[start2, end2)的交集, 交集被储存在result中.
- 两个集合以序列的形式给出, 并且必须先按升序排好位置.
- set_intersection()的返回值是一个指向交集序列末尾的迭代器.
- set_intersection()以线性时间(linear time)运行.
- 如果严格弱排序函数对象cmp未指定, set_intersection()将使用<操作符比较元素.
- 范例
- */
- // set_intersection example
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- int main () {
- int first[] = {5,10,15,20,25};
- int second[] = {50,40,30,20,10};
- vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0
- vector<int>::iterator it;
- sort (first,first+5); // 5 10 15 20 25
- sort (second,second+5); // 10 20 30 40 50
- it=set_intersection (first, first+5, second, second+5, v.begin());
- // 10 20 0 0 0 0 0 0 0 0
- cout << "intersection has " << int(it - v.begin()) << " elements.\n";
- return 0;
- }
- /*输出: intersection has 2 elements*/
multiset的删除 重要
a
.
erase
(
x
);//删除集合中所有的x
multiset<
int
>::iterator
it
=
a
.
find
(
x
);
if(
it
!=
a
.end
())
{
a
.
erase
(
it
);//这里是删除其中的一个x; 删除的是一个位置 而arase是删除所有位置
}
- #include <iostream>
- #include <string>
- #include <set>
- using namespace std;
- void printSet(set<int> s)
- {
- set<int>::iterator i;
- for(i=s.begin();i!=s.end();i++)
- printf("%d ",*i);
- cout<<endl;
- }
- void main()
- {
- //创建空的set对象,元素类型为int,
- set<int> s1;
- for (int i = ; i <5 ; i++)
- s1.insert(i*10);
- printSet(s1);
- cout<<"s1.insert(20).second = "<<endl;;
- if (s1.insert(20).second)//再次插入20
- cout<<"Insert OK!"<<endl;
- else
- cout<<"Insert Failed!"<<endl;
- cout<<"s1.insert(50).second = "<<endl;
- if (s1.insert(50).second)
- {cout<<"Insert OK!"<<endl; printSet(s1);}
- else
- cout<<"Insert Failed!"<<endl;
- pair<set<int>::iterator, bool> p;
- p = s1.insert(60);
- if (p.second)
- {cout<<"Insert OK!"<<endl; printSet(s1);}
- else
- cout<<"Insert Failed!"<<endl;
- }
- 继续更新中
2: set 的 empty erase 删除特定元素
- #include <iostream>
- #include <set>
- using namespace std;
- int main ()
- {
- set<int> myset;
- myset.insert(20);
- myset.insert(30);
- myset.insert(10);
- while (!myset.empty())
- {
- cout <<" "<< *myset.begin();
- myset.erase(myset.begin());
- }
- cout << endl;
- return 0;
- }
- //set::find
- #include <iostream>
- #include <set>
- using namespace std;
- int main ()
- {
- set<int> myset;
- set<int>::iterator it;
- for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50
- it=myset.find(20);
- myset.erase (it);
- myset.erase (myset.find(40));
- myset.erase (30);
- cout << "myset contains:";
- for (it=myset.begin(); it!=myset.end(); it++)
- cout << " " << *it;
- cout << endl;
- return 0;
- }
lower_bound()返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个大于等于value 的值。 例如,有如下序列: ia[]={12,15,17,19,20,22,23,26,29,35,40,51}; 用值21调用lower_bound(),返回一个指向22的iterator。用值22调用lower_bound(),也返回一个指向22的iterator。
iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值> key的第一个元素。
- // 6.set::lower_bound/upper_bound
- #include <iostream>
- #include <set>
- using namespace std;
- int main ()
- {
- set<int> myset;
- set<int>::iterator it,itlow,itup;
- for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
- itlow=myset.lower_bound (30); // >=
- itup=myset.upper_bound (60); // >
- printf("%d %d",*itlow,*itup); // 30 70
- return 0;
- }
- // 7.set::equal_elements
- #include <iostream>
- #include <set>
- using namespace std;
- int main ()
- {
- set<int> myset;
- pair<set<int>::iterator,set<int>::iterator> ret;
- for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50
- ret = myset.equal_range(30);
- cout << "lower bound points to: " << *ret.first << endl;
- cout << "upper bound points to: " << *ret.second << endl;
- return 0;
- }
- //lower bound points to: 30
- //upper bound points to: 40
set结构体的应用
- #include<iostream>
- #include<set>
- using namespace std;
- struct haha
- {
- int a,b;
- char s;
- friend bool operator<(struct haha a,struct haha b)
- {
- return a.s<b.s;
- }
- };
- set<struct haha>element;
- int main()
- {
- struct haha 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(d);
- element.insert(b);
- element.insert(c);
- element.insert(a);
- set<struct haha>::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<<" ";
- }
集合的并集 交集 差集 等等
- #include<stdio.h>
- #include<string>
- #include<set>
- #include<iostream>
- #include <algorithm>//包含
- using namespace std;
- struct compare//自定义排序方式
- {
- bool operator ()(string s1,string s2)
- {
- return s1>s2;
- }///自定义一个仿函数
- };
- int main()
- {
- typedef set<string,compare> SET;
- SET s;//建立第一个集合
- s.insert(string("sfdsfd"));
- s.insert(string("apple"));
- s.insert(string("english"));
- s.insert(string("dstd"));
- cout<<"第一个集合s1为:"<<endl;
- set<string,compare>::iterator it = s.begin();
- while(it!=s.end())
- cout<<*it++<<" ";
- SET s2;//建立第二个集合
- s2.insert(string("abc"));
- s2.insert(string("apple"));
- s2.insert(string("english"));
- cout<<endl<<"第一个集合s2为:"<<endl;
- it = s2.begin();
- while(it!=s2.end())
- cout<<*it++<<" ";
- cout<<endl<<endl;
- string str[10];
- string *end =set_intersection(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//求交集,返回值指向str最后一个元素的尾端
- /*set_intersection包含于#include <algorithm> 头文件中 其中上面的不一定非要为set容器 也可以使数组 但是前提是要把2个数组都排好序才可以
- 返回值是一个指向交集序列末尾的迭代器 至于是什么迭代器与第5个参数有关 如果是数组 返回为int的迭代器 */
- cout<<"s1,s2的交集为:"<<endl;
- string *first = str;
- while(first<end)
- cout <<*first++<<" ";
- cout<<endl<<endl<<"s1,s2的并集为:"<<endl;
- end =set_union(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//并集
- first = str;
- while(first<end)
- cout <<*first++<<" ";
- cout<<endl<<endl<<"s2相对于s1的差集:"<<endl;
- first = str;
- end = std::set_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//s2相对于s1的差集
- while(first<end)
- cout <<*first++<<" ";
- cout<<endl<<endl<<"s1相对于s2的差集:"<<endl;
- first = str;
- end = std::set_difference(s2.begin(),s2.end(),s.begin(),s.end(),str,compare());//s1相对于s2的差集
- while(first<end)
- cout <<*first++<<" ";
- cout<<endl<<endl;
- first = str;
- end = std::set_symmetric_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//上面两个差集的并集
- while(first<end)
- cout <<*first++<<" ";
- cout<<endl;
- /*
- set<int> s3 ;
- set<int>::iterator iter = s3.begin() ;
- set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s3,iter));
- copy(s3.begin(),s3.end(), ostream_iterator<int>(cout," "));
- */
- }
另外一个实例
- /*set_intersection()算法计算两个集合[start1, end1)和[start2, end2)的交集, 交集被储存在result中.
- 两个集合以序列的形式给出, 并且必须先按升序排好位置.
- set_intersection()的返回值是一个指向交集序列末尾的迭代器.
- set_intersection()以线性时间(linear time)运行.
- 如果严格弱排序函数对象cmp未指定, set_intersection()将使用<操作符比较元素.
- 范例
- */
- // set_intersection example
- #include <iostream>
- #include <algorithm>
- #include <vector>
- using namespace std;
- int main () {
- int first[] = {5,10,15,20,25};
- int second[] = {50,40,30,20,10};
- vector<int> v(10); // 0 0 0 0 0 0 0 0 0 0
- vector<int>::iterator it;
- sort (first,first+5); // 5 10 15 20 25
- sort (second,second+5); // 10 20 30 40 50
- it=set_intersection (first, first+5, second, second+5, v.begin());
- // 10 20 0 0 0 0 0 0 0 0
- cout << "intersection has " << int(it - v.begin()) << " elements.\n";
- return 0;
- }
- /*输出: intersection has 2 elements*/
multiset的删除 重要
a
.
erase
(
x
);//删除集合中所有的x
multiset<
int
>::iterator
it
=
a
.
find
(
x
);
if(
it
!=
a
.end
())
{
a
.
erase
(
it
);//这里是删除其中的一个x; 删除的是一个位置 而arase是删除所有位置
}
C++ STL set和multiset的使用的更多相关文章
- STL Set和multiset 容器
STL Set和multiset 容器 set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列. 元素插入过程是按排序规则插入,所以不能指定插入位 ...
- STL之set && multiset
一.set 在了解关联容器set之前,让我们先来看看下面这个例子,并猜测该例子输出什么: // stl/set1.cpp #include <iostream> #include < ...
- STL - set和multiset
set/multiset的简介 set是一个集合容器,其中所包含的元素是唯一的,集合中的元素按一定的顺序排列.元素插入过程是按排序规则插入,所以不能指定插入位置. set采用红黑树变体的数据结构实现, ...
- STL之set&multiset使用简介
关于set,必须说明的是set关联式容器.set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序.应该注 ...
- c++ STL -- set和multiset
set和multiset 1.结构 set和multiset会根据特定的排序原则将元素排序.两者不同之处在于,multisets允许元素重复,而set不允许重复. 只要是assignable.copy ...
- STL:set/multiset用法详解
集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...
- STL使用————SET&MULTISET
SET函数的基本用法 by hhl 使用set的好处 1. 当增加元素后,集合会自动删重并从小到大排列(时间比快排还快)2. 相当于一棵伸展树(能快速求出后继) 使用基础 #include<se ...
- C++ STL——set和multiset
目录 一 set和multiset 二 对组pair 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 一 set和multiset set和multis ...
- 转自http://blog.sina.com.cn/daylive——C++ STL set&multiset
C++ STL set和multiset的使用 1,set的含义是集合,它是一个有序的容器,里面的元素都是排序好的,支持插入,删除,查找等操作,就 像一个集合一样.所有的操作的都是严格在logn时间 ...
随机推荐
- WD backup西部盘数据备份
西部数据(WD),硬盘备份数据!防止数据丢失.损坏.... 起因: 电脑上存储很多资料,之前有500G的东芝硬盘(现在插头不灵敏),故决定换个好点的1T硬盘.电脑在一夜间打不开,不能识别硬盘!怎么重启 ...
- HDU 1069 Monkey and Banana(最大的单调递减序列啊 dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 Problem Description A group of researchers are d ...
- Oracle Tuxedo工作站客户端与服务端的样例程序
服务端代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <cty ...
- Intent传递数据全解
概述 之前的博文也有介绍,查看-->用户界面开发基础 这里单独抽取出来,更加具体的记录一下,事实上主要是API的使用. Intent传递简单数据 能够以直接通过调用Intent的putExtra ...
- 解决linux的centos版本修改时间重启后无效的问题
安装完centos后,发现时间与本地时间不匹配,在网上找了好多的办法,但是一直没有奏效,重启之后,又恢复为原来的时间.很是纳闷.最后抱着试一试的心态加上了这句指令: ln -sf /usr/share ...
- __cplusplus的用法(转)
经常在/usr/include目录下看到这种字句: #ifdef __cplusplusextern "C" {#endif...#ifdef __cplusplus}#endif ...
- 强制IE浏览器或WebBrowser控件
注册表: 32 bit: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BRO ...
- C# 使用系统方法发送异步邮件
项目背景: 最近在对几年前的一个项目进行重构,发现发送邮件功能需要一定的时间来处理,而由于发送是同步的因此导致在发送邮件时无法执行后续的操作 实际上发送邮件后只需要将发送结果写入系统日志即可对其他业务 ...
- Config程序配置文件操作实践进阶之ConfigurationSectionGroup
今天又进一步对System.Configuration下的ConfigurationSectionGroup类及相关的类与方法进行了研究.发现要构建多层次嵌套的XML标签 则必须用到Configura ...
- EF GroupBy 根据key 分组 再把key求和(取决于每条数据中 arr的条数) arr 中有多少条数据 就把多少个key 加起来
List<A> alist = new List<A>{ ,b=,c=,d=,e=}, ,b=,c=,d=,e=}, ,b=,c=,d=,e=}, ,b=,c=,d=,e=}, ...