Insert data into vector in C Plus Plus Programming Language

Insert data into vector in C Plus Plus Programming Language

Instructor-svgAl-Mamun Sarkar
Apr 03 , 2020

Insert data into vector in C Plus Plus Programming Language. The following code shows how to insert item into vector in C++ programming language.

 

Code:

#include <iostream>
#include <vector>
#include <algorithm> // For find();
using namespace std;

int main()
{
    vector < int > v; // Declare Vector

    v.push_back( 1 ); // Insert Data to vector
    v.push_back( 2 );
    v.push_back( 3 );
    v.push_back( 4 );
    v.push_back( 5 );

    v.at(2) = 33;
    v.insert (v.begin (), 2, 25); // Insert At begin
    v.insert (v.end(), 5, 88); // Insert at End

    cout << "Size is : " << v.size() << endl; // Show size of vector

    // Find by data
    vector <int>::iterator f= find(v.begin(), v.end(), 2);
    cout << "2 Find at : " << *f << endl;


    // Print Output via Iterator
    vector <int>::iterator p = v.begin();
    while (p != v.end()) {
        cout << *p << endl;
        p++;
    }
}

 

Output:

Size is : 12
2 Find at : 2
25
25
1
2
33
4
5
88
88
88
88
88

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram