set简介
set是存储唯一元素的一类关联容器,元素就是key关联容器是通过key有效的访问元素而特别设计的,不像顺序容器,顺序容器是通过相邻或者绝对位置来更有效的访问元素
在内部,set里的元素总是以从低到高的一个特定的严格的弱序标准排序
set是以一棵典型的二叉搜索树来实现的
set的特点
1.唯一的元素值,在同一个set里面不可能出现两个元素相同
2.元素值就是key,以key作为索引来访问
3.在set容器中元素总是是以弱序排序
4.支持双向迭代器
set模板
template < class Key, class Compare = less<Key>,
class Allocator = allocator<Key> > class set;
Member functions
(constructor) | Construct set |
operator= | Copy container content |
const Allocator& = Allocator() );
2.template <class InputIterator>
set ( InputIterator first, InputIterator last,
const Compare& comp = Compare(), const Allocator& = Allocator() );
3.set ( const set<Key,Compare,Allocator>& x );
//set constructor
eg:
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
//operator=
eg:
int iArray[]={1,2,3,4,5,6};
set<int> first(iArray,iArray+6);
set<int> second;
second = first;
Capacity:
empty | Test whether container is empty |
size | Return container size |
max_size | Return maximum size |
insert | Insert element |
erase | Erase elements |
swap | Swap content |
clear | Clear content |
1.pair<iterator,bool> insert ( const value_type& x );
iterator insert ( iterator position, const value_type& x );
2.template <class InputIterator>
void insert ( InputIterator first, InputIterator last );
eg:
set<int,greater<int>> first;
set<int,greater<int>> second;
int iArray[]={1,2,3,4,5,6};
second.insert(iArray,iArray+6);
copy(second.begin(),second.end(),ostream_iterator<int>(cout,"\t"));
cout<<endl;
set<int,greater<int>>::iterator it=second.begin();
advance(it,3);
first.insert(second.begin(),it);
copy(first.begin(),first.end(),ostream_iterator<int>(cout,"\t"));
cout<<endl;
pair<set<int,greater<int>>::iterator,bool> ret = first.insert(100);
copy(first.begin(),first.end(),ostream_iterator<int>(cout,"\t"));
cout<<endl;
if (ret.second)
{
cout<<"insert "<<*ret.first<<" ok!"<<endl;
}
else
{
cout<<*ret.first<<" is already exist!"<<endl;
}
Output:
6 5 4 3 2 1
6 5 4
100 6 5 4
insert 100 ok!
//erase
1.void erase ( iterator position );
2.size_type erase ( const key_type& x );//存在元素且删除成功返回1,其他返回0
3.void erase ( iterator first, iterator last );//[first,last)
eg:
set<int> myset;
set<int>::iterator it;
// insert some values:
for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
it=myset.begin();
it++; // "it" points now to 20
myset.erase (it);
myset.erase (40);
it=myset.find (60);
myset.erase ( it, myset.end() );
//swap
eg:
first.swap(second);
Observers:
key_comp | Return comparison object |
value_comp | Return comparison object |
find | Get iterator to element |
count | Count elements with a specific key |
lower_bound | Return iterator to lower bound |
upper_bound | Return iterator to upper bound |
equal_range | Get range of equal elements |
//一般可与erase配合使用
iterator find ( const key_type& x ) const;
eg:
set<int,greater<int>>::iterator it=second.find(100);
if (it!=second.end())
{
cout<<"find 100 ok!"<<endl;
second.erase(second.find(100));
}
else
{
cout<<"100 is not in second!"<<endl;
}
//count 计数,因为set中元素值唯一,存在返回1,否则返回0
size_type count ( const key_type& x ) const;
eg:
size_t ret=second.count(1);
if (ret==1)
{
cout<<"1 is in second!"<<endl;
}
else//ret == 0
{
cout<<"1 is not in second!"<<endl;
}
//lower_bound >=[包括他在内的他的iterator]
iterator lower_bound ( const key_type& x ) const;
//upper_bound >[比他大的下一个iterator]
iterator upper_bound ( const key_type& x ) const;
//equal_range
返回一个lower_bound和一个upper_bound
pair<iterator,iterator> equal_range ( const key_type& x ) const;
eg:
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;
Output:
lower bound points to: 30
upper bound points to: 40