C++ Vectors

C++ Vectors

Introduction to C++ Vectors

In this article, we will be looking at VECTORS one of the most used STL data structures in C++.

A vector is used to store elements of similar data types like arrays. But, the difference between an array and a vector is that a vector can resize automatically when an element is inserted or deleted, with its storage being automatically handled by the container.

The elements of vectors are stored in contiguous memory locations so that they can be traversed and accessed easily using iterators. In vectors, the data is inserted at the end. The time taken to insert an element may vary as sometimes we just need to add an element at the end and we need to extend the vector i.e., increase the size and then insert the element. Removing the last element takes only constant time because no resizing happens. Inserting and erasing at the beginning or in the middle is linear in time.

Vector Iterators

Now that we know what a vector is, let’s look at some methods used for iterating a vector known as the iterators.

  • begin() – Returns an iterator pointing to the first element of the vector.
  • end() – Returns an iterator pointing to the theoretical element that follows the last element in the vector.
  • rbegin() – Returns a reverse iterator pointing to the last element in the vector (reverse beginning). It moves from the last element to the first one.
  • rend() – Returns a reverse iterator pointing to the theoretical element preceding the first element in the vector (considered as reverse end)
  • cbegin() – Returns a constant iterator pointing to the first element in the vector.
  • cend() – Returns a constant iterator pointing to the theoretical element that follows the last element of the vector.
  • crbegin() – Returns a constant reverse iterator pointing to the last element in the vector (reverse beginning). It moves from the last to the first element.
  • crend() – Returns a constant reverse iterator pointing to the theoretical element preceding the first element in the vector (consider as reverse end).
See also  Operator Overloading in c++

Let us use all the methods in an example to understand them better.

// iterators in vector
#include <iostream>
#include <vector>
 
using namespace std;
 
int main()
{
    vector<int> g1;
 
    for (int i = 1; i <= 5; i++)
        g1.push_back(i);
 
    cout << "Output of begin and end: ";
    for (auto i = g1.begin(); i != g1.end(); ++i)
        cout << *i << " ";
 
    cout << "\nOutput of cbegin and cend: ";
    for (auto i = g1.cbegin(); i != g1.cend(); ++i)
        cout << *i << " ";
 
    cout << "\nOutput of rbegin and rend: ";
    for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir)
        cout << *ir << " ";
 
    cout << "\nOutput of crbegin and crend : ";
    for (auto ir = g1.crbegin(); ir != g1.crend(); ++ir)
        cout << *ir << " ";
 
    return 0;
}

Output:

Output of begin and end: 1 2 3 4 5 
Output of cbegin and cend: 1 2 3 4 5 
Output of rbegin and rend: 5 4 3 2 1 
Output of crbegin and crend : 5 4 3 2 1

Vector Capacity Methods

A vector also has a method to know the vector’s current size and the maximum capacity of the vector. The following are the methods used:

  • size() – Returns the number of elements in the vector.
  • max_size() – Returns the maximum number of elements that the vector can hold.
  • capacity() – Returns the size of the storage space currently allocated to the vector expressed as the number of elements.
  • resize(n) – Resizes the container so that it contains ‘n’ elements.
  • empty() – Returns whether the container is empty.
  • shrink_to_fit – Reduces the capacity of the container to fit its size and destroys all elements beyond the capacity.
  • reserve() – Requests that the vector capacity is at least enough to contain n elements.

Let’s look at an example using all the above methods.

// C++ program to illustrate the
// capacity function in vector
#include <iostream>
#include <vector>
 
using namespace std;
 
int main()
{
    vector<int> g1;
 
    for (int i = 1; i <= 5; i++)
        g1.push_back(i);
 
    cout << "Size : " << g1.size();
    cout << "\nCapacity : " << g1.capacity();
    cout << "\nMax_Size : " << g1.max_size();
 
    // resizes the vector size to 4
    g1.resize(4);
 
    // prints the vector size after resize()
    cout << "\nSize : " << g1.size();
 
    // checks if the vector is empty or not
    if (g1.empty() == false)
        cout << "\nVector is not empty";
    else
        cout << "\nVector is empty";
 
    // Shrinks the vector
    g1.shrink_to_fit();
    cout << "\nVector elements are: ";
    for (auto it = g1.begin(); it != g1.end(); it++)
        cout << *it << " ";
 
    return 0;
}

Output:

Size : 5
Capacity : 8
Max_Size : 4611686018427387903
Size : 4

That’s

See also  C++ Vectors

it for this article. In the next article, we will be looking at Vector element accessing and Vector Modifiers.

Thank you

Leave a Comment

Your email address will not be published.

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Refresh