Vector in STL C++

About Vector in STL

A vector in C++ is a sequence container that represents a dynamic array. Unlike traditional arrays with fixed sizes, vectors can change their size during runtime. This feature makes vectors a valuable asset for managing collections of data whose size may vary.
vector in stl

Vector in STL

In C++, vectors are not ordered. Iterators make it simple to retrieve and navigate through vector items that are stored in nearby storage.Data is added at the end of vectors when the push back() method is used. As there may occasionally be a need to expand the vector, adding an element to the end of the vector requires differential time, but adding an element to the beginning or centre of the vector just requires linear time. Due to the absence of any scaling, removing the last piece just requires constant time.

Initializing a Vector

To use a vector, you must include the header file in your C++ program. Then, you can declare a vector of a specific data type.

For example:

#include <vector>

std::vector myVector; // Creates an empty vector of integers

Adding Elements to a Vector

One of the primary reasons for using vectors is to store and manipulate a collection of elements. You can add elements to a vector using the push_back() function, which appends elements at the end of the vector:

myVector.push_back(10); // Adds the integer 10 to the vector
myVector.push_back(20); // Adds the integer 20 to the vector

Accessing Elements in a Vector

You can access elements in a vector using the subscript operator []. Remember that the first element is at index 0:

int firstElement = myVector[0]; // Accesses the first element
int secondElement = myVector[1]; // Accesses the second element

Vector Size and Capacity

Vectors maintain two essential properties: size and capacity.

The size represents the number of elements currently stored in the vector, while the capacity represents the total number of elements the vector can hold before resizing is required.

size_t size = myVector.size(); // Retrieves the current size of the vector
size_t capacity = myVector.capacity(); // Retrieves the current capacity of the vector

Resizing a Vector

If you want to change the size of the vector manually, you can use the resize() function:

myVector.resize(5); // Resizes the vector to hold 5 elements

Removing Elements from a Vector

You can remove elements from a vector using the pop_back() function, which eliminates the last element:

myVector.pop_back(); // Removes the last element from the vector

Vector Iterators

To traverse through a vector, you can use iterators, which act as pointers to elements within the vector:

std::vector::iterator it;
for (it = myVector.begin(); it != myVector.end(); ++it) {
    // Access and manipulate the elements using the iterator
}

Sorting a Vector

Sorting a vector in C++ is straightforward using the sort() function from the &ltalgorithm&gt header:
#include <algorithm>
std::sort(myVector.begin(), myVector.end()); // Sorts the elements in ascending order

Vector Capacity Management

Vectors automatically manage their capacity, but you can explicitly request a capacity change using the reserve() function:

myVector.reserve(100); // Reserves space for 100 elements

Functions in Vector :

 Function Working
begin() gives back an iterator pointing to the vector’s first element.
end() gives back an iterator pointing to the vector’s last element.
size() gives the number of elements in vector
push_back() Add an element in the last of the vector
empty() return whether the vector is empty or not

Example of Vector in STL :

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
   vector< int >vect;
   // adding elements to the vector
   vect.push_back(2);
   vect.push_back(5);
   vect.push_back(10);
   vect.push_back(12);
   
   //size of vector
   cout<< "size of vector "<< vect.size()<< endl;
   
   //printing elements of vector
   cout<<"The elements of vector : ";
   for(int i=0;i< vect.size();i++){
       cout<< vect[i]<<" ";
   }

    return 0;
}

Output :

size of vector 4
The elements of vector : 2 5 10 12 

In the above program, we take the vector vect and display the size and elements of vector

Example of Vector in STL:

Run
#include<iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
   vector< int>vect;
   // adding elements to the vector
   vect.push_back(2);
   vect.push_back(5);
   vect.push_back(10);
   vect.push_back(12);
   
   //size of vector
   cout<< "size of vector "<< vect.size()<< endl;
   
   //printing elements of vector
   cout<<"The elements of vector : ";
   for(int i=0;i< vect.size();i++){
       cout<< vect[i]<<" ";
   }
   cout<< endl;
   vect.pop_back();
   cout<<"The elements of vector after pop_back() ";
   for(int i=0; i< vect.size();i++){
       cout<< vect[i] << " ";
   }
   cout<< endl;
   // checking whether vector is empty or not
   if(vect.empty()){
       cout<<"vector is empty";
   }
       else{
           cout<<"vector is not empty";
       }
   

    return 0;
}

Output :

size of vector 4
The elements of vector : 2 5 10 12 
The elements of vector after pop_back() 2 5 10 
vector is not empty

In the above program, we take the vector vect and used different functions like push_back(), empty(), pop_back() to understand its functioning.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription