Home » C++ STL

Create an empty vector and initialize by pushing values in C++ STL

C++ STL Vector Initialization: Here, we are going to learn how to create an empty vector and how to initialize it by pushing the values in C++ STL?
Submitted by IncludeHelp, on May 12, 2019

What is the vector?

Vector is a container in C++ STL, it is used to represent array and its size can be changed.

Read more: C++ STL Vector

Creating an empty vector

Here is the syntax to declare an empty vector,

    vector<type> vector_name;


  • type – is the datatype.
  • vector_name – is any use defined name to the vector.

Example to create/declare an empty vector of int type

    vector::<int> v1;

Initialize vector by pushing the element

To insert an element in the vector, we can use vector::push_back() function, it inserts an element at the end of the vector.





C++ STL program to create an empty vector and initialize by pushing values

//C++ STL program to create an empty vector 
//and initialize by pushing values
#include <iostream>
#include <vector>
using namespace std;

int main()
    //vector declaration
    vector<int> v1;

    //pushing the elements

    //printing the vector elements
    //creating iterator to access the elements
    vector<int>::iterator it;
    cout << "Vector v1 elements are: ";
    for (it = v1.begin(); it != v1.end(); it++)
        cout << *it << " ";
    cout << endl;

    return 0;


Vector v1 elements are: 10 20 30 40 50

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.