set

构造函数

  1. empty (1)

     explicit set (const key_compare& comp = key_compare(),
           const allocator_type& alloc = allocator_type());
     explicit set (const allocator_type& alloc);
    
  2. range (2)

     template <class InputIterator>
       set (InputIterator first, InputIterator last,
            const key_compare& comp = key_compare(),
            const allocator_type& = allocator_type());
    
  3. copy (3)

     set (const set& x);
     set (const set& x, const allocator_type& alloc);
    
  4. move (4)

     set (set&& x);
     set (set&& x, const allocator_type& alloc);
    
  5. initializer list (5)

     set (initializer_list<value_type> il,
          const key_compare& comp = key_compare(),
          const allocator_type& alloc = allocator_type());
    

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <set>

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 ()
{
  std::set<int> first;                           // empty set of ints

  int myints[]= {10,20,30,40,50};
  std::set<int> second (myints,myints+5);        // range

  std::set<int> third (second);                  // a copy of second

  std::set<int> fourth (second.begin(), second.end());  // iterator ctor.

  std::set<int,classcomp> fifth;                 // class as Compare

  bool(*fn_pt)(int,int) = fncomp;
  std::set<int,bool(*)(int,int)> sixth (fn_pt);  // function pointer as Compare

  return 0;
}

赋值运算符

copy (1)

set& operator= (const set& x);

move (2)

set& operator= (set&& x);

initializer list (3)

	set& operator= (initializer_list<value_type> il);

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// assignment operator with sets
#include <iostream>
#include <set>

int main ()
{
  int myints[]={ 12,82,37,64,15 };
  std::set<int> first (myints,myints+5);   // set with 5 ints
  std::set<int> second;                    // empty set

  second = first;                          // now second contains the 5 ints
  first = std::set<int>();                 // and first is empty

  std::cout << "Size of first: " << int (first.size()) << '\n';
  std::cout << "Size of second: " << int (second.size()) << '\n';
  return 0;
}

迭代器

容量

修改

insert函数

single element (1)

pair<iterator,bool> insert (const value_type& val);

pair<iterator,bool> insert (value_type&& val);

with hint (2)

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

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

range (3)

template <class InputIterator>
  void insert (InputIterator first, InputIterator last);

initializer list (4)

void insert (initializer_list<value_type> il);

插入元素

通过插入新元素来扩展容器,通过插入的元素数量有效地增加容器大小。

因为元素是唯一的,插入操作检查是否每个插入的元素等价于在容器已经是一个元素,如果是这样,该元素没有被插入,则返回这个现有的元素的迭代器(如果该函数返回一个值)。

对于允许重复元素的类似容器,请参见multiset。

在内部,设置容器使其所有元素按照其比较对象指定的标准进行排序。这些元件总是插入其相应的位置。

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// set::insert (C++98)
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;
  std::set<int>::iterator it;
  std::pair<std::set<int>::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;
}

erase函数

(1)

iterator  erase (const_iterator position);

(2)

size_type erase (const value_type& val);

(3)

iterator  erase (const_iterator first, const_iterator last);

删除元素

从集合容器中移除单个元素或一系列元素([first,最last))。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// erasing from set
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;
  std::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());

  std::cout << "myset contains:";
  for (it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

swap函数

void swap(set&x);

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// swap sets
#include <iostream>
#include <set>

main ()
{
  int myints[]={12,75,10,32,20,25};
  std::set<int> first (myints,myints+3);     // 10,12,75
  std::set<int> second (myints+3,myints+6);  // 20,25,32

  first.swap(second);

  std::cout << "first contains:";
  for (std::set<int>::iterator it=first.begin(); it!=first.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  std::cout << "second contains:";
  for (std::set<int>::iterator it=second.begin(); it!=second.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

clear函数

void clear()noexcept;

清除内容

###emplace函数

template <class... Args>
  pair<iterator,bool> emplace (Args&&... args);

构造和插入元素

在集合中插入一个新的元素,如果是唯一的。这个新元素是使用args作为构造的参数来构建的。

仅当容器中没有其他元素等同于放置的元素(集合容器中的元素是唯一的)时,才会进行插入。

举例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <iostream>
#include <set>
#include <string>

int main ()
{
  std::set<std::string> myset;

  myset.emplace("foo");
  myset.emplace("bar");
  auto ret = myset.emplace("foo");

  if (!ret.second) std::cout << "foo already exists in myset\n";

  return 0;
}

emplace_hint函数

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

用提示构造和插入元素

在集合中插入一个新元素(如果是唯一的),在插入位置上提示。这个新元素是使用args作为构造的参数来构建的。

仅当容器中没有其他元素等同于放置的元素(集合容器中的元素是唯一的)时,才会进行插入。

如果插入,这有效地将容器尺寸增加1。

position的值作为在插入点的提示。然后,该元素将按照其内部比较对象描述的顺序插入其相应位置,但是该功能使用该提示开始搜索插入点,当实际插入点位于或靠近它时,会大大加快进程。

元素通过使用转发的args调用allocator_traits :: construct来就地构建。

操作

find函数

const_iterator find (const value_type& val) const;
iterator       find (const value_type& val);

获取迭代器到元素

搜索容器一个等价于val的元素,如果找到则返回一个迭代器,否则返回一个迭代器set :: end。

count函数

size_type count(const value_type&val)const;

搜索容器的等价于val的元素并返回匹配数。

lower_bound函数

  iterator lower_bound(const value_type&val);
const_iterator lower_bound(const value_type&val)const;

返回一个不小于val的第一个元素的迭代器

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;
  std::set<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<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

upper_bound函数

  iterator upper_bound(const value_type&val);
const_iterator upper_bound(const value_type&val)const;

函数将返回一个迭代器到大于val的第一个元素

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;
  std::set<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<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

equal_range函数

pair<const_iterator,const_iterator> equal_range (const value_type& val) const;
pair<iterator,iterator>             equal_range (const value_type& val);

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <iostream>
#include <set>

int main ()
{
  std::set<int> myset;

  for (int i=1; i<=5; i++) myset.insert(i*10);   // myset: 10 20 30 40 50

  std::pair<std::set<int>::const_iterator,std::set<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;
}

multiset

迭代器

容量

修改

操作