編輯本段集和多集(set 和multiset 容器類)
#include <set>
一個集合(set)是一個容器,它其中所包含的元素的值是唯一的。
集和多集的區(qū)別是:set支持唯一鍵值,set中的值都是特定的,而且只出現(xiàn)一次;而multiset中可以出現(xiàn)副本鍵,同一值可以出現(xiàn)多次。
explicit set(const Compare&=compare());
如:set<int,less<int> > set1;
less<int>是一個標(biāo)準(zhǔn)類,用于形成降序排列函數(shù)對象。升序排列是用greater<int>。
Template<class InputIterator> set(InputIterator, InputIterator,\ const Compare&=compare());
如:set<int ,less<int> >set2(vector1.begin(),vector1.end());
通過指定某一預(yù)先定義的區(qū)間來初始化set對象的構(gòu)造函數(shù)。
set(const set<Key,Compare&>);
如:set<int ,less<int> >set3(set2);
復(fù)制構(gòu)造函數(shù)。
begin() 返回指向第一個元素的迭代器
clear() 清除所有元素
count() 返回某個值元素的個數(shù)
empty() 如果集合為空,返回true
end() 返回指向最后一個元素的迭代器
equal_range() 返回集合中與給定值相等的上下限的兩個迭代器
erase() 刪除集合中的元素
find() 返回一個指向被查找到元素的迭代器
get_allocator() 返回集合的分配器
insert() 在集合中插入元素
lower_bound() 返回指向大于(或等于)某值的第一個元素的迭代器
key_comp() 返回一個用于元素間值比較的函數(shù)
max_size() 返回集合能容納的元素的最大限值
rbegin() 返回指向集合中最后一個元素的反向迭代器
rend() 返回指向集合中第一個元素的反向迭代器
size() 集合中元素的數(shù)目
swap() 交換兩個集合變量
upper_bound() 返回大于某個值元素的迭代器
value_comp() 返回一個用于比較元素間的值的函數(shù)
std::set_intersection() :這個函數(shù)是求兩個集合的交集。
std::set_union() :求兩個集合的并集
std::set_difference():差集
std::set_symmetric_difference():得到的結(jié)果是第一個迭代器相對于第二個的差集并 上第二個相當(dāng)于第一個的差集
struct compare{
bool operator ()(string s1,string s2){
return s1>s2;
}///自定義一個仿函數(shù)
};
std::set<string,compare> s
string str[10];
string *end = set_intersection(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//求交集,返回值指向str最后一個元素的尾端
end = std::set_union(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//并集
end = std::set_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//s2相對于s1的差集
end = std::set_difference(s2.begin(),s2.end(),s.begin(),s.end(),str,compare());//s1相對于s2的差集
end = std::set_symmetric_difference(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//上面兩個差集的并集
1///////////////////////////////////////////////////////////////////
#include <iostream>
#include <set>
using namespace std;
int main(void)
{
set<int> set1;
for(int i=0; i<10; ++i)
set1.insert(i);
for(set<int>::iterator p=set1.begin();p!=set1.end();++p)
cout<<*p<<"";
if(set1.insert(3).second)//把3插入到set1中
//插入成功則set1.insert(3).second返回1,否則返回0
//此例中,集中已經(jīng)有3這個元素了,所以插入將失敗
cout<<"set insert success";
else
cout<<"set insert failed";
int a[] = {4, 1, 1, 1, 1, 1, 0, 5, 1, 0};
multiset<int> A;
A.insert(set1.begin(),set1.end());
A.insert(a,a+10);
cout<<endl;
for(multiset<int>::iterator p=A.begin();p!=A.end();++p)
cout<<*p<<" ";
cin.get();
return 0;
}
2////////////////////////////////////////
struct compare
{
bool operator ()(string s1,string s2)
{
return s1>s2;
}///自定義一個仿函數(shù)
};
int main()
{
typedef std::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;
std::set<string,compare>::iterator it = s.begin();
while(it!=s.end())
cout<<*it++<<" ";
cout<<endl<<"s2:"<<endl;
_SET s2;
s2.insert(string("abc"));
s2.insert(string("apple"));
s2.insert(string("english"));
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最后一個元素的尾端cout<<"result of set_intersection s1,s2:"<<endl;
string *first = str;
while(first<end)
cout <<*first++<<" ";
cout<<endl<<endl<<"result of set_union of s1,s2"<<endl;
end = std::set_union(s.begin(),s.end(),s2.begin(),s2.end(),str,compare());//并集 first = str;
while(first<end)
cout <<*first++<<" ";
cout<<endl<<endl<<"result of set_difference of s2 relative to 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<<"result of set_difference of s1 relative to 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;
}