Create Vector with initial value in C Plus Plus Programming Language

Create Vector with initial value in C Plus Plus Programming Language

Instructor-svgAl-Mamun Sarkar
Apr 03 , 2020

Create Vector with initial value in C Plus Plus Programming Language. The following code shows how to create a vector with initial value in C++ programming language.

 

Code:

#include <vector>
#include <iostream>
using namespace std;
int main ()
{
    // Instantiate a vector with 4 elements, each initialized to 90
    vector <int> v (4, 90);

    vector <int>::iterator i;
    for ( i = v.begin (); i != v.end (); ++ i ){
        cout << *i << ' ';
    }
    vector <int> vecAnother (2, 30);

    v.insert (v.begin () + 1,vecAnother.begin (), vecAnother.end ());

    for ( i = v.begin (); i != v.end (); ++ i )
    {
        cout << *i << ' ';
    }

    return 0;
}

 

Output:

90 90 90 90 90 30 30 90 90 90 

 

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