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*或範圍,則會導致*未定義行為*。

另見