public member function
<vector>

std::vector::insert

single element (1)
iterator insert (iterator position, const value_type& val);
fill (2)
    void insert (iterator position, size_type n, const value_type& val);
range (3)
template <class InputIterator>    void insert (iterator position, InputIterator first, InputIterator last);
single element (1)
iterator insert (const_iterator position, const value_type& val);
fill (2)
iterator insert (const_iterator position, size_type n, const value_type& val);
range (3)
template <class InputIterator>iterator insert (const_iterator position, InputIterator first, InputIterator last);
move (4)
iterator insert (const_iterator position, value_type&& val);
initializer list (5)
iterator insert (const_iterator position, initializer_list<value_type> il);
插入元素
The vector is extended by inserting new elements before the element at the specified position, effectively increasing the container size by the number of elements inserted.

This causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current vector capacity.

Because vectors use an array as their underlying storage, inserting elements in positions other than the vector end causes the container to relocate all the elements that were after position to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers (such as list or forward_list).

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

参数

position
Position in the vector where the new elements are inserted.
iteratoris a member type, defined as a random access iterator type that points to elements.
val
Value to be copied (or moved) to the inserted elements.
成员类型value_type是容器中元素的类型,在 deque 中定义为其第一个模板参数(T).
n
要插入的元素数量。每个元素都将使用*val*的副本进行初始化。
成员类型size_type是一种无符号整型类型。
first, last
指定元素范围的迭代器。范围中元素的副本[first,last)将被插入到*position*(按相同顺序)。
请注意,范围包括 first 和 last 之间的所有元素,包括 first 指向的元素,但不包括 last 指向的元素。
函数模板参数InputIteratorshall be an input iterator type that points to elements of a type from whichvalue_type对象的类型的元素。
il
一个initializer_list对象。这些元素的副本将被插入到*position*(按相同顺序)。
这些对象是从初始化列表声明符自动构造的。
成员类型value_typeis the type of the elements in the container, defined in vector as an alias of its first template parameter (T).

返回值

指向新插入的第一个元素的迭代器。

成员类型iterator随机访问迭代器 类型,指向元素。

If reallocations happen, the storage is allocated using the container's allocator, which may throw exceptions on failure (for the default allocator,bad_alloc如果分配请求不成功,则抛出)。

示例

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
// inserting into a vector
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector (3,100);
  std::vector<int>::iterator it;

  it = myvector.begin();
  it = myvector.insert ( it , 200 );

  myvector.insert (it,2,300);

  // "it" no longer valid, get a new one:
  it = myvector.begin();

  std::vector<int> anothervector (2,400);
  myvector.insert (it+2,anothervector.begin(),anothervector.end());

  int myarray [] = { 501,502,503 };
  myvector.insert (myvector.begin(), myarray, myarray+3);

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

  return 0;
}
输出
myvector contains: 501 502 503 300 300 400 400 200 100 100 100



复杂度

Linear on the number of elements inserted (copy/move construction) plus the number of elements after position (moving).

Additionally, ifInputIteratorin the range insert (3) is not at least of a forward iterator category (i.e., just an input iterator) the new capacity cannot be determined beforehand and the insertion incurs in additional logarithmic complexity in size (reallocations).

If a reallocation happens, the reallocation is itself up to linear in the entire size at the moment of the reallocation.

迭代器有效性

如果发生重新分配,则所有与容器相关的迭代器、指针和引用都将失效。
Otherwise, only those pointing to position and beyond are invalidated, with all iterators, pointers and references to elements before position guaranteed to keep referring to the same elements they were referring to before the call.

数据竞争

All copied elements are accessed.
The container is modified.
如果发生重新分配,则所有包含的元素都将被修改。
Otherwise, none of the elements before position is accessed, and concurrently accessing or modifying them is safe (although see iterator validity above).

异常安全

If the operation inserts a single element at the end, and no reallocations happen, there are no changes in the container in case of exception (strong guarantee). In case of reallocations, the strong guarantee is also given in this case if the type of the elements is either copyable or no-throw moveable.
Otherwise, the container is guaranteed to end in a valid state (basic guarantee).
如果不支持具有适当参数的allocator_traits::construct进行元素构造,或者指定了无效的*position*或范围,则会导致*未定义行为*。

另见