Chapter 14.关联容器set

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

1.explicit set ( const Compare& comp = Compare(),
               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:

emptyTest whether container is empty
sizeReturn container size
max_sizeReturn maximum size

Modifiers:

insertInsert element 
eraseErase elements
swapSwap content
clearClear content

//insert
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_compReturn comparison object
value_compReturn comparison object

Operations:

findGet iterator to element
countCount elements with a specific key
lower_boundReturn iterator to lower bound
upper_boundReturn iterator to upper bound
equal_rangeGet range of equal elements

//find 如果要查找的key不在容器中,则iterator赋值为c.end()
//一般可与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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值