public member function
<set>

std::set::insert

single element (1)
pair<iterator,bool> insert (const value_type& val);
with hint (2)
iterator insert (iterator position, const value_type& val);
range (3)
template <class InputIterator>  void insert (InputIterator first, InputIterator last);
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);
Insert element
Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted.

Because elements in a set are unique, the insertion operation checks whether each inserted element is equivalent to an element already in the container, and if so, the element is not inserted, returning an iterator to this existing element (if the function returns a value).

For a similar container allowing for duplicate elements, see multiset.

Internally, set containers keep all their elements sorted following the criterion specified by its comparison object. The elements are always inserted in its respective position following this ordering.

参数决定了插入多少元素以及它们被初始化为哪个值。

参数

val
Value to be copied (or moved) to the inserted elements.
成员类型value_type是容器中元素的类型,在 set 中定义为其第一个模板参数的别名(T).
position
元素可插入位置的提示。
The function optimizes its insertion time if position points to the element that will precede the inserted element.
The function optimizes its insertion time if position points to the element that will follow the inserted element (or to the end, if it would be the last).
Notice that this is just a hint and does not force the new element to be inserted at that position within the set container (the elements in a set always follow a specific order).
成员类型iteratorconst_iteratorare defined in map as a bidirectional iterator type that point to elements.
first, last
指定元素范围的迭代器。范围中元素的副本[first,last)are inserted in the container.
请注意,范围包括 first 和 last 之间的所有元素,包括 first 指向的元素,但不包括 last 指向的元素。
函数模板参数InputIteratorshall be an input iterator type that points to elements of a type from whichvalue_type对象的类型的元素。
il
An initializer_list object. Copies of these elements are inserted.
这些对象是从初始化列表声明符自动构造的。
成员类型value_type是容器中元素的类型,在 set 中定义为其第一个模板参数的别名(T).

返回值

The single element versions (1) return a pair, with its memberpair::firstset to an iterator pointing to either the newly inserted element or to the equivalent element already in the set. Thepair::secondelement in the pair is set totrueif a new element was inserted orfalseif an equivalent element already existed.

The versions with a hint (2) return an iterator pointing to either the newly inserted element or to the element that already had its same value in the set.

成员类型iterator是指向元素的双向迭代器类型。
pair是声明在<utility>中的类模板(参见pair)。

示例

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;
}
输出
myset contains: 5 10 15 20 24 25 26 30 40 50


复杂度

If a single element is inserted, logarithmic in size in general, but amortized constant if a hint is given and the position given is the optimal.

If N elements are inserted,Nlog(size+N)in general, but linear insize+Nif the elements are already sorted according to the same ordering criterion used by the container.
If N elements are inserted,Nlog(size+N).
Implementations may optimize if the range is already sorted.

迭代器有效性

没有变化。

数据竞争

The container is modified.
Concurrently accessing existing elements is safe, although iterating ranges in the container is not.

异常安全

If a single element is to be inserted, there are no changes in the container in case of exception (strong guarantee).
Otherwise, the container is guaranteed to end in a valid state (basic guarantee).
如果不支持带有元素构造适当参数的 allocator_traits::construct,或者指定了无效的position,则会导致未定义行为

另见