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 = 0; 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;
- }
-
- #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);
- 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的第一個元素。
-
- #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);
-
- itlow=myset.lower_bound (30);
- itup=myset.upper_bound (60);
- printf("%d %d",*itlow,*itup);
- return 0;
- }
-
- #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);
-
- ret = myset.equal_range(30);
-
- cout << "lower bound points to: " << *ret.first << endl;
- cout << "upper bound points to: " << *ret.second << endl;
-
- return 0;
- }
-
-
-
-
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());
-
-
- 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());
- 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());
-
- 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;
-
-
-
-
-
-
-
- }
另外一個實例
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #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);
- vector<int>::iterator it;
-
- sort (first,first+5);
- sort (second,second+5);
-
- it=set_intersection (first, first+5, second, second+5, v.begin());
-
-
- cout << "intersection has " << int(it - v.begin()) << " elements.\n";
-
- return 0;
- }
-
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 = 0; 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;
- }
-
- #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);
- 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的第一個元素。
-
- #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);
-
- itlow=myset.lower_bound (30);
- itup=myset.upper_bound (60);
- printf("%d %d",*itlow,*itup);
- return 0;
- }
-
- #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);
-
- ret = myset.equal_range(30);
-
- cout << "lower bound points to: " << *ret.first << endl;
- cout << "upper bound points to: " << *ret.second << endl;
-
- return 0;
- }
-
-
-
-
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());
-
-
- 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());
- 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());
-
- 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;
-
-
-
-
-
-
-
- }
另外一個實例
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #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);
- vector<int>::iterator it;
-
- sort (first,first+5);
- sort (second,second+5);
-
- it=set_intersection (first, first+5, second, second+5, v.begin());
-
-
- cout << "intersection has " << int(it - v.begin()) << " elements.\n";
-
- return 0;
- }
-
multiset的刪除 重要
a
.
erase
(
x
);//刪除集合中所有的x
multiset<
int
>::iterator
it
=
a
.
find
(
x
);
if(
it
!=
a
.end
())
{
a
.
erase
(
it
);//這里是刪除其中的一個x; 刪除的是一個位置 而arase是刪除所有位置
}