c-plus-plus
  1. c-plus-plus-vector

C++ STL Tutorial: Vector

The vector container is a dynamic array in C++ STL. It provides similar functionality as built-in arrays with the added advantage of dynamic resizing, which means that its size can be increased or decreased during runtime.

Syntax

#include<vector>

vector<datatype> vector_name;

Datatype in vector can be a user-defined datatype, in-built datatype or even an STL container. Example- vector, vector, vector<vector>.

Some of the most used member functions of a vector are:

  • push_back(element): Adds an element at the end of the vector
  • pop_back(): Removes the last element from the vector
  • size(): Returns the number of elements in the vector
  • clear(): Removes all the elements from the vector
  • insert(position, element): Inserts an element at a specified position in the vector
  • erase(position): Removes an element from a specified position in the vector

Example

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

int main() {
    vector<int> vec; // Vector declaration
    vec.push_back(10); // Adding elements
    vec.push_back(20);
    vec.push_back(30);
    vec.pop_back(); // Removing last element
    
    cout << "Vector size: " << vec.size() << endl; // Size of vector
    for (int i = 0; i < vec.size(); i++) { // Traverse through elements
        cout << vec[i] << endl; // Print all elements of vector
    }
    vec.clear(); // Removing all elements
    cout << "Vector size after clear: " << vec.size() << endl; // Size of vector after clear operation
    
    return 0;
}

Output

Vector size: 2
10
20
Vector size after clear: 0

Explanation

In the above example, we have created a vector called "vec" of the datatype int and added 3 elements to it using the push_back function. We then removed the last element from it using the pop_back function and printed the size of the vector using the size function.

We then traversed through the elements of the vector using a for loop and printed each element. Next, we used the clear function to remove all the elements from the vector and printed the size of the vector after the clear operation.

Use

Vectors are useful when we don't know the size of the array during initialization or if we want to add or remove elements at runtime. They are an efficient and convenient alternative to built-in arrays.

For example, we can use a vector to store a list of integers that a user enters at runtime or a list of strings retrieved from a file.

Important Points

  • Vectors are dynamic arrays in C++ STL.
  • They provide dynamic resizing, which means that the size of the vector can be changed at runtime.
  • Default values of elements in a vector are 0 or nullptr (for pointers).
  • Elements can be accessed using the subscript operator.

Summary

In summary, vectors in C++ STL provide a convenient way to work with dynamic arrays. They provide dynamic resizing and useful member functions that make it easy to manipulate and get information about the vector. Using vectors can simplify the process of working with arrays and make your code more efficient and maintainable.

Published on: