×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

vector::insert() function with example in C++ STL

C++ STL vector::insert() function: Here, we are going to learn about the insert() function of vector header in C++ STL with example.
Submitted by IncludeHelp, on May 16, 2019

C++ vector::insert() function

vector::insert() is a library function of "vector" header, it is used to insert elements in a vector, it accepts an element, set of elements with a default value or other values from other containers and insert in the vector from specified iterator position.

Note: To use vector, include <vector> header.

Syntax

Syntax of vector::insert() function

//inserting an element
vector::insert(iterator position, value);

//inserting multiple elements 
vector::insert(iterator position, size, default_value);

//inserting elements from other containers 
vector::insert(iterator position, iterator start_position, iterator end_position);

Parameter(s)

In the case of inserting an element: iterator position, valueiterator position is the index using the iterator of the vector where an element to be added, value is an element.

In the case of inserting multiple elements: iterator position, size, default_valueiterator position is the index using iterator of the vector where elements to be added, size is the number of elements to be added and default_value is the value which will be inserted in the vector.

In the case of inserting elements from other containers: iterator position, iterator start_position, iterator end_positioniterator position is the index using iterator of the vector where elements to be added, start_position, iterator end_position are the iterators of another container whose value will be inserted in the current vector.

Return value

In the case of inserting an element, it returns an iterator pointing to the first newly added element, and in other cases, it returns void.

Sample Input and Output

Input:
//vector declaration
vector<int> v1{ 10, 20, 30, 40, 50 };
//array
int x[] = { 1, 2, 3, 4, 5 };
    

//inserting an element
v1.insert(v1.begin() + 2, 100); //inserts 100 at 2nd index
    
//inserting multiple elements with default value
//inserts 5 elements (99) from 2nd index
v1.insert(v1.begin() + 2, 5, 99);     
//inserting array (other containers elements)
    
//inserts 3 elements (from array) from 1st index
v1.insert(v1.begin() + 1, x + 0, x + 3); 
    
Output:
v1: 10 1 2 3 20 99 99 99 99 99 100 30 40 50

C++ program to demonstrate example of vector::insert() function

//C++ STL program to demonstrate example of
//vector::insert() function

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    //vector declaration
    vector<int> v1{ 10, 20, 30, 40, 50 };
    //array
    int x[] = { 1, 2, 3, 4, 5 };

    //printing elements
    cout << "before inserting the elements..." << endl;
    cout << "size of v1: " << v1.size() << endl;
    cout << "v1: ";
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    //inserting an element
    v1.insert(v1.begin() + 2, 100); //inserts 100 at 2nd index

    //printing elements
    cout << "after inserting an element..." << endl;
    cout << "size of v1: " << v1.size() << endl;
    cout << "v1: ";
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    //inserting multiple elements with default value
    //inserts 5 elements (99) from 2nd index
    v1.insert(v1.begin() + 2, 5, 99);

    //printing elements
    cout << "after inserting multiple elements..." << endl;
    cout << "size of v1: " << v1.size() << endl;
    cout << "v1: ";
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    //inserting array (other containers elements)
    //inserts 3 elements (from array) from 1st index
    v1.insert(v1.begin() + 1, x + 0, x + 3);

    //printing elements
    cout << "after inserting multiple elements..." << endl;
    cout << "size of v1: " << v1.size() << endl;
    cout << "v1: ";
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    return 0;
}

Output

before inserting the elements... 
size of v1: 5
v1: 10 20 30 40 50 
after inserting an element...
size of v1: 6
v1: 10 20 100 30 40 50 
after inserting multiple elements... 
size of v1: 11 
v1: 10 20 99 99 99 99 99 100 30 40 50
after inserting multiple elements... 
size of v1: 14 
v1: 10 1 2 3 20 99 99 99 99 99 100 30 40 50

Reference: C++ vector::insert()

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.