Partager via


vector::insert

Inserts an element or a number of elements or a range of elements into the vector at a specified position.

iterator insert(
   const_iterator _Where,
   const Type& _Val
);
iterator insert(
   const_iterator _Where,
   Type&& _Val
);
void insert(
   const_iterator _Where,
   size_type _Count,
   const Type& _Val
);
template<class InputIterator>
   void insert(
      const_iterator _Where,
      InputIterator _First,
      InputIterator _Last
   );

Parameters

Parameter

Description

_Where

The position in the vector where the first element is inserted.

_Val

The value of the element being inserted into the vector.

_Count

The number of elements being inserted into the vector.

_First

The position of the first element in the range of elements to be copied.

_Last

The position of the first element beyond the range of elements to be copied.

Return Value

The first two insert functions return an iterator that points to the position where the new element was inserted into the vector.

Remarks

Any insertion operation can be expensive, see vector Class for a discussion of vector performance.

Example

// vector_insert.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>
using namespace std;
int main( )
{
      
   vector <int> vec;
    vector <int>::iterator pos;

    vec.push_back(10);
    vec.push_back(20);
    vec.push_back(30);

    
    vec.insert(vec.begin() + 1, 40);
    cout << "After insert vec =";
    for (pos = vec.begin(); pos != vec.end(); ++pos)
    {
        cout << " " << *pos;
    }
        
    cout << endl;
    
}
After insert vec = 10 40 20 30

Requirements

Header: <vector>

Namespace: std

See Also

Reference

vector Class

Standard Template Library