Vector in C++ STL (original) (raw)

Last Updated : 11 May, 2026

A vector represents a dynamic sized array in the Standard Template Library(STL) that automatically grows when elements are added beyond current capacity.

#include #include using namespace std; int main() {

// Declares an empty vector
vector<int> v1;

// Declares vector with given size
// and fills it with a value
vector<int> v2(3, 5);  

// Print items of v2
for (int x : v2) {
    cout << x << " ";
}

cout << endl;

// Initializes vector using 
// initializer list.
vector<int> v3 = {1, 2, 3};  

// Print items of v3
for (int x : v3) {
    cout << x << " ";
}

return 0;

}

`

Declaration and Initialization of a Vector

A vector is defined as the std::vector class template in the <****vector**> header file.

vector v;

where T is the data type of elements and **v is the name assigned to the vector.

Operations in Vector

Vectors in C++ support various useful operations that allow you to add, remove, access, and modify elements dynamically.

Insert Elements

#include #include using namespace std;

int main() { vector v = {'a', 'f', 'd'};

  // Inserting 'z' at the back
  v.push_back('z');

  // Inserting 'c' at index 1
  v.insert(v.begin() + 1, 'c');

  for (char i : v)
    cout << i << " ";
return 0;

}

`

Access elements

#include #include using namespace std;

int main() { vector v = {10, 20, 30, 40};

// Accessing using operator[]
cout << "Element at index 2 using []: " << v[2] << endl;

// Accessing using at()
cout << "Element at index 3 using at(): " << v.at(3) << endl;

// Uncommenting the next line will throw an 
// out_of_range exception 
// cout v.at(10)<<endl;
return 0;

}

`

Output

Element at index 2 using []: 30 Element at index 3 using at(): 40

Update elements

#include #include using namespace std;

int main() {

vector<int> v = {10, 20, 30};
cout << "Original value at index 1: " << v[1] << endl;

// Updating the element at index i
v[1] = 50;

cout << "Updated value at index 1: " << v[1] << endl;
return 0;

}

`

Output

Original value at index 1: 20 Updated value at index 1: 50

Find Vector Size

We can find the size(number of elements) of a vector using the size() function, like v.size().

C++ `

#include #include using namespace std;

int main() { vector v = {'a', 'c', 'f', 'd', 'z'};

// Finding size
cout << v.size();

return 0;

}

`

Traverse Vector

#include #include using namespace std;

int main() { vector v = {'a', 'c', 'f', 'd', 'z'};

// Range-based loop
  for (char i : v)
    cout << i << " ";
return 0;

}

`

**Recommendation:

Delete Elements

#include #include #include using namespace std;

int main() { vector v = {'a', 'c', 'f', 'd', 'z'};

// Deleting last element 'z'
  v.pop_back();

  // Deleting element 'f'
  v.erase(find(v.begin(), v.end(), 'f'));
  
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
}
return 0;

}

`

Vector is Empty

#include #include using namespace std;

int main() {

vector<int>v;

// Check if the vector is empty
if(v.empty()){
    cout<<"Vector is empty."<<endl;
}

// Add an element
v.push_back(100);
if(!v.empty()){
    cout<<"Vector is not empty. First element "<<v[0]<<endl;
}
return 0;

}

`

Output

Vector is empty. Vector is not empty. First element 100

Multidimensional Vectors

Multidimensional vectors are dynamic arrays that can store data in more than one dimension, like tables or grids. They are implemented using vector inside another vector, allowing flexible row-column (2D), or even higher-dimensional structures.

**Syntax to declare a 2D vector:

vector<vector> matrix;

Below is an example of creating a 2D vector and traversing over it:

C++ `

#include #include using namespace std;

int main() { // Declaring and Initializing a 2D vector vector<vector> matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

// Traversing using range-based loop with auto
for (const auto &row : matrix) {
    for (const auto &val : row) {
        cout << val << " ";
    }
    cout<<endl;
}
return 0;

}

`