Skip to main content

Command Palette

Search for a command to run...

Vector STL in C++ : A Comprehensive Guide

Published
3 min read
Vector STL in C++ : A Comprehensive Guide
P

Hello I am Prajwal Mandlik a Full-Stack Developer. Interested in Blogging ⭐

Vectors are a popular data structure in C++, and they are part of the Standard Template Library (STL). Vectors are dynamic arrays, which means that their size can grow and shrink as needed. This makes them very flexible and useful for a variety of tasks.

Vectors are also called Dynamic Array in C++.

Including the Necessary Header

Before you can use vectors in your C++ program, you need to include the appropriate header :

#include <vector>

This header file contains the vector template class and the necessary functions and methods to work with vectors.

Declaring a Vector

To declare a vector, you need to specify its type, like int, double, or even a custom data type. Here's how you declare a vector of integers :

std::vector<int> myVector; // Default vector with 0 size and no data
std::vector<int> myVector(5,2); // Vector of size 5 with specific data 2
std::vector<int> myVector = {1,2,3,4,5}; // Vector with some values
std::vector<int> myVector {1,2,3,4,5}; // We can declare in this type also

Pushing Elements into a Vector

Adding elements to a vector is a common operation. You can use the push_back method to add elements to the end of the vector :

std::vector<int> myVector;
myVector.push_back(10);
myVector.push_back(20);
myVector.push_back(30);

After executing the above code, myVector will contain the values 10, 20, and 30 in that order.

Popping Elements from a Vector

Removing elements from the back of a vector can be done using the pop_back method :

std::vector<int> myVector = {1, 2, 3, 4, 5};
myVector.pop_back(); // Delete the last element from the vector

After this operation, myVector will contain the values 1, 2, 3, and 4, as the last element (5) has been removed.

Finding the Size of a Vector

To determine the number of elements in a vector, you can use the size method :

std::vector<int> myVector = {1, 2, 3, 4, 5};
int size = myVector.size();
std::cout << "Size of vector is : " << size << endl; 
// It will print the size of vector

In this example, the variable size will be assigned the value 5, as there are five elements in the vector myVector.

Printing the Contents of a Vector

Printing the elements of a vector can be accomplished using a simple loop. Here's how you can print the contents of a vector of integers :

std::vector<int> myVector = {1,2,3,4,5};
int size = myVector.size();
for(int i = 0 ; i < size ; i++){
    std::cout << myVector[i] << " ";
}

An Alternate method to print the content of the vector :

std::vector<int> myVector = {1,2,3,4,5};
for(auto items : myVector){
    std::cout << items << " ";
}

Example of all operations on vector :

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

int main(){

    // Vector Declaration :
    vector<int> v = {10,20,30,40,50};

    // Push the elements in vector :
    v.push_back(60);

    // Pop the element from the vector :
    v.pop_back();

    // Size of the vector :
    int size = v.size();

    // Print the vector :
    for(int i = 0 ; i < size ; i++){
        cout << v[i] << " ";
    }

    // Print the size of the vector :
    cout << "Size of vector : " << size << endl;

    return 0;

}

Output :

10 20 30 40 50
Size of vector : 5

Conclusion

In conclusion, vectors are a fundamental part of the C++ STL and provide a dynamic array that can be easily manipulated. By following the examples and code provided in this article, you should have a solid understanding of how to declare vectors, push and pop elements, find their size, and print their contents in C++. Vectors are highly versatile and can be used in various scenarios to manage collections of data efficiently.

S

C++ là một phần trong IT phải không