C++ STL之set

C++  STLSet容器的用法

 set中的元素类型...1

2    set中构造相关函数...2

3    set中的迭代器...3

4    set中的容量相关函数...3

5    set中元素修改函数...3

5.1     insert 函数...3

5.2     erase 函数...4

5.3     clear 函数...5

5.4     swap 函数...5

5.5     emplace 函数...5

5.6     emplace_hint 函数...5

6    set 中的比较函数体...6

6.1     key_com 函数...6

6.2     value_com 函数...6

7    set的其他操作函数...7

7.1     find 函数...7

7.2     count 函数...8

7.3  lower_bound 函数...8

7.4     upper_bound 函数...8

7.5  equal_range 函数...9

7.6  get_allocator 函数...10

Set容器是一个关联容器,容器中的元素互不相同,并且容器中的元素按照键值大小进行排序。每当插入或者删除一个元素,容器都会重新排序。Set容器有两大特点,一个是元素排序,另一个就是查询速度快(当然没有vector快)。Set获取元素时通过键值,关联容器都这样。Set是通过二元查找树实现的,再具体点就是红黑树。

set中的元素类型

member type

definition

notes

key_type

Thefirst template parameter (T)

 

value_type

Thefirst template parameter (T)

 

key_compare

Thesecond template parameter (Compare)

defaults to: less

value_compare

Thesecond template parameter (Compare)

defaults to: less

allocator_type

Thethird template parameter (Alloc)

defaults to:allocator

reference

allocator_type::reference

forthe default allocator:value_type&

const_reference

allocator_type::const_reference

forthe default allocator: constvalue_type&

pointer

allocator_type::pointer

forthe default allocator:value_type*

const_pointer

allocator_type::const_pointer

forthe default allocator: const value_type*

iterator

a bidirectionaliterator to value_type

convertible to const_iterator

const_iterator

a bidirectionaliterator to const value_type

 

reverse_iterator

reverse_iterator

 

const_reverse_iterator

reverse_iterator

 

difference_type

asigned integral type, identical to:iterator_traits::difference_type

usually the same as ptrdiff_t

size_type

anunsigned integral type that can represent any non-negative valueof difference_type

usually the same as size_t

关于这个无需多言,切记set中的iterator是指向const元素。

2       set中构造相关函数

empty (1)

explicit set (const key_compare& comp =key_compare(),

             const allocator_type& alloc = allocator_type());

explicit set (const allocator_type&alloc);

range (2)

template

  set (InputIterator first,InputIterator last,

      const key_compare& comp = key_compare(),

      const allocator_type& = allocator_type());

copy (3)

set (const set& x);

set (const set& x, const allocator_type&alloc);

move (4)

set (set&& x);

set (set&& x, const allocator_type&alloc);

initializer list (5)

set (initializer_list il,

    const key_compare& comp = key_compare(),

    const allocator_type& alloc = allocator_type());

以上分别为默认构造函数,范围构造函数,复制构造函数,移动构造函数,初始化列表构造函数,其中最后两个是C++11中的版本。关于构造函数,不同的编译器可能提供的形式不一样,但是种类都是这几种。

构造函数示例:

#include 
#include 
 
bool fncomp (int lhs, int rhs) {return lhs
struct classcomp {
  bool operator() (const int& lhs, const int& rhs) const
  {return lhs
};
int main ()
{
  std::set<</span>int> first;                           // empty set of ints
  int myints[]= {10,20,30,40,50};
  std::set<</span>int> second (myints,myints+5);        // range
 
  std::set<</span>int> third (second);                  // a copy of second
 
  std::set<</span>int> fourth (second.begin(), second.end()); //iterator ctor.
 
  std::set<</span>int,classcomp> fifth;                 // class as Compare
 
  bool(*fn_pt)(int,int) = fncomp;
  std::set<</span>int,bool(*)(int,int)> sixth (fn_pt);  // function pointer as Compare
 
  return 0;
}

3      set中的迭代器

begin    Return iterator to beginning (public memberfunction )

end             Return iterator to end (public member function)

rbegin  Return reverse iterator toreverse beginning (public member function)

rend    Return reverse iterator to reverse end (public member function)

cbegin       Return const_iterator to beginning (public member function)

cend   Return const_iterator to end (public member function)

crbegin      Return const_reverse_iterator to reversebeginning (public member function)

crend   Returnconst_reverse_iterator to reverse end (public member function)

关于迭代器也没什么好说的,迭代器和前面的都一样。

4       set中的容量相关函数

empty       Test whether container is empty (public member function )

size                  Return container size (public member function )

max_size   Returnmaximum size (public member function )

这个和其它容量一样。

5       set中元素修改函数

insert               Insert element (public member function )

erase               Erase elements (public member function )

swap                 Swap content (public member function )

clear                 Clear content (public member function )

emplace           Construct and insert element (public member function)

emplace_hint  Construct and insert element with hint (public member function)

5.1       insert 函数

single element (1)

pair insert (const value_type&val);

pair insert (value_type&& val);

with hint (2)

iterator insert (const_iterator position, constvalue_type& val);

iterator insert (const_iterator position,value_type&& val);

range (3)

template

  void insert (InputIteratorfirst, InputIterator last);

initializer list (4)

void insert (initializer_list il);

要注意其中的函数的返回类型,这个在编程中或许会很有用,两种不同的颜色来区分C++11中增加的函数,示例如下:

#include 
#include 
int main ()
{
  std::set<</span>int> myset;
  std::set<</span>int>::iterator it;
  std::pairint>::iterator,bool> ret;
  // set some initial values:
  for (int i=1; i<=5; ++i) myset.insert(i*10);    // set: 10 20 30 40 50
 
  ret = myset.insert(20);               // no new element inserted
 
  if (ret.second==false) it=ret.first;  // "it" now points to element 20
 
  myset.insert (it,25);                 // max efficiency inserting
  myset.insert (it,24);                 // max efficiency inserting
  myset.insert (it,26);                 // no max efficiency inserting
 
  int myints[]= {5,10,15};              // 10 already in set, not inserted
  myset.insert (myints,myints+3);
 
  std::cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';
 
  return 0;
}
结果:
myset contains: 5 10 15 20 24 25 26 30 40 50
5.2     erase 函数

(1)

    void erase (iterator position);

(2)

size_type erase (const value_type&val);

(3)

    void erase (iterator first, iterator last);

 

(1)

iterator  erase (const_iteratorposition);

(2)

size_type erase (const value_type&val);

(3)

iterator  erase (const_iteratorfirst, const_iterator last);

红色表格表示C++98,蓝色表示C++11。函数的返回值,其中第二个函数表示删除的元素的个数,当然在set中其返回值最多是1,在C++11中,其余两个函数皆有返回值为iterator类型的值,其指向删除的最后一个元素,或者指向set的末尾。示例如下:

#include 
#include 
int main ()
{
  std::set<</span>int> myset;
  std::set<</span>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());
  std::cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';
  return 0;
}

结果:

myset contains: 10 30 50

5.3       clear 函数

void clear();

清空set,容器大小变为0

5.4       swap 函数

void swap (set& x);

交换两个set的内容

5.5       emplace 函数

template 
  pair emplace (Args&&... args);

这个是C++11中的函数,也是插入一个元素

5.6       emplace_hint 函数

template 
  iterator emplace_hint (const_iterator position, Args&&... args);

在一定的位置插入元素,position参数只是用来提高插入的速度,并不一定就是说要在此处插入元素。示例

#include 
#include 
#include 
 
int main ()
{
  std::set myset;
  auto it = myset.cbegin();
 
  myset.emplace_hint (it,"alpha");
  it = myset.emplace_hint (myset.cend(),"omega");
  it = myset.emplace_hint (it,"epsilon");
  it = myset.emplace_hint (it,"beta");
 
  std::cout << "myset contains:";
  for (const std::string& x: myset)
    std::cout << ' ' << x;
  std::cout << '\n';
 
  return 0;
}

结果:

myset contains: alpha beta epsilon omega

6       set 中的比较函数体

key_comp        Return comparison object (public member function )

value_comp      Return comparison object (public member function )

这两个函数都是获取set容器中比较函数

6.1      key_com 函数

key_compare key_comp() const;
函数返回比较函数对象,默认的是升序排列。示例:
#include 
#include 
int main ()
{
  std::set<</span>int> myset;
  int highest;
  std::set<</span>int>::key_compare mycomp = myset.key_comp();
  for (int i=0; i<=5; i++) myset.insert(i);
 
  std::cout << "myset contains:";
  highest=*myset.rbegin();
  std::set<</span>int>::iterator it=myset.begin();
  do {
    std::cout << ' ' << *it;
  } while ( mycomp(*(++it),highest) );
  std::cout << '\n';
  return 0;
}
结果:
myset contains: 0 1 2 3 4
6.2    value_com 函数
value_compare value_comp()const
函数返回元素比较函数对象,默认的是升序排列,在set中,value_comp函数和key_value函数的作用一模一样。示例:
#include 
#include 
int main ()
{
  std::set<</span>int> myset;
  std::set<</span>int>::value_compare mycomp = myset.value_comp();
  for (int i=0; i<=5; i++) myset.insert(i);
  std::cout << "myset contains:";
  int highest=*myset.rbegin();
  std::set<</span>int>::iterator it=myset.begin();
  do {
    std::cout << ' ' << *it;
  } while ( mycomp(*(++it),highest) );
  std::cout << '\n';
  return 0;
}
结果:
myset contains: 0 1 2 3 4
7            set的其他操作函数

find                          Get iterator to element (public member function )

count               Count elements with a specific value (public member function )

lower_bound         Return iterator to lower bound (public member function )

upper_bound  Return iterator to upper bound (public member function )

equal_range          Get range of equal elements (public member function )

get_allocator         Get allocator (public member function )

7.1    find 函数
iterator
find(const value_type& val) const;(C++98)
const_iterator
find(const value_type& val) const;(C++11)
iterator
find(const value_type& val);       (C++11)
函数返回找到元素的iterator,如果找不到就指向set的末尾
#include 
#include 
int main ()
{
  std::set<</span>int> myset;
  std::set<</span>int>::iterator it;
  // set some initial values:
  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));
 
  std::cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';
  return 0;
}
结果:
myset contains: 10 30 50
7.2    count 函数
size_type count (const value_type& val) const;
函数返回值为val的元素的个数,当然在set容器中其最大为1.示例:
#include 
#include 
int main ()
{
  std::set<</span>int> myset;
  // set some initial values:
  for (int i=1; i<5; ++i) myset.insert(i*3);    // set: 3 6 9 12
  for (int i=0; i<10; ++i)
  {
    std::cout << i;
    if (myset.count(i)!=0)
      std::cout << " is an element of myset.\n";
    else
      std::cout << " is not an element of myset.\n";
  }
  return 0;
}
结果:
0 is not an element of myset.
1 is not an element of myset.
2 is not an element of myset.
3 is an element of myset.
4 is not an element of myset.
5 is not an element of myset.
6 is an element of myset.
7 is not an element of myset.
8 is not an element of myset.
9 is an element of myset.
7.3  lower_bound 函数
iterator lower_bound (const value_type& val) const; (C++98)
iterator lower_bound (const value_type& val);(C++11)
const_iterator lower_bound (const value_type& val) const;(C++11)
函数返回set中第一个小于或者等于val的元素的iterator。
7.4  upper_bound 函数
iterator upper_bound (const value_type& val) const; (C++98)
iterator upper_bound (const value_type& val);(C++11)
const_iterator upper_bound (const value_type& val) const;(C++11)
函数返回set中第一个大于或者等于val的元素的iterator。示例
#include 
#include 
 
int main ()
{
  std::set<</span>int> myset;
  std::set<</span>int>::iterator 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);                 //                   ^
  myset.erase(itlow,itup);                     // 10 20 70 80 90
 
  std::cout << "myset contains:";
  for (std::set<</span>int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';
  return 0;
}
结果:
myset contains: 10 20 70 80 90
7.5  equal_range 函数
pair equal_range (const value_type& val) const; (C++98)
pair equal_range (const value_type& val) const;(C++11)
pair  equal_range (const value_type& val);(C++11)
函数返回等于set中val的上下界的iterator。示例:
#include 
#include 
int main ()
{
  std::set<</span>int> myset;
  for (int i=1; i<=5; i++) myset.insert(i*10);   // myset: 10 20 30 40 50
  std::pairint>::const_iterator,std::set<</span>int>::const_iterator> ret;
  ret = myset.equal_range(30);
 
  std::cout << "the lower bound points to: " << *ret.first << '\n';
  std::cout << "the upper bound points to: " << *ret.second << '\n';
  return 0;
}
结果:
the lower bound points to: 30
the upper bound points to: 40
7.6  get_allocator 函数
allocator_type get_allocator() const;
函数返回set的分配器对象 示例:
#include 
#include 
 
int main ()
{
  std::set<</span>int> myset;
  int * p;
  unsigned int i;
 
  // allocate an array of 5 elements using myset's allocator:
  p=myset.get_allocator().allocate(5);
 
  // assign some values to array
  for (i=0; i<5; i++) p[i]=(i+1)*10;
 
  std::cout << "The allocated array contains:";
  for (i=0; i<5; i++) std::cout << ' ' << p[i];
  std::cout << '\n';
 
  myset.get_allocator().deallocate(p,5);
 
  return 0;
}
结果:
The allocated array contains: 10 20 30 40 50
 
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值