C++ vectors are part of the STL (Standard Template Library). The STL is one of many libraries available for C++ that boost applications' efficiency, flexibility, and performance. Vectors are very useful and a vital concept for anyone wanting to become proficient in C++.

C++ vectors are essentially containers that you can use to store data. They make working with arrays clean and quick, so knowing how to use C++ vectors is a valuable skill, if you’re looking to develop a C++ application. Read on as we discuss everything you need to know about C++ vectors.

What Are C++ Vectors?

C++ vectors are sequence containers that are an essential component of the Standard Template Library. Vectors represent arrays that can dynamically change size during runtime if required.

Unlike traditional arrays, you do not need to manage a vector's memory allocation explicitly. The vector's container automatically manages its memory. You can think of C++ vectors as a dynamic array that adjusts itself after adding or removing an element.

Additionally, a vector arranges its elements contiguously. This means you can easily access them using pointers or iterators.

How std::vector Works

Now that you’re familiar with what C++ vectors are, you need to understand how they function before you can start using them within your code.

Related: Is C++ Still Relevant in 2021?

When you use a C++ vector, it will insert new elements at the end in differential time. This is because, in some cases, the vector will have to expand to accommodate the new element. Similarly, if you were to extract the last element of a vector, it would take constant time the vector does not need to adjust its length (size).

Hands type on a laptop with blocks of code displayed on its screen

To use vectors effectively, we make use of iterators and in-built functions. Iterators are pointers used alongside C++ vectors. They simplify traversing the vector container. There are many different vector functions, and we’ll be looking at some of the important ones in the next section.

C++ Vector Functions

Let’s look at some iterator and STL container functions that are commonly used with C++ vectors:

  1. begin() returns an iterator pointer that points to the first element of the vector.
  2. end() returns an iterator pointer to the after-end position of the vector container.
  3. rbegin() is like begin(), but it gives you a reverse iterator pointing to the last element in the vector.
  4. size() returns a count of elements stored in the vector.
  5. max_size() returns the maximum capacity of elements that the vector container can accommodate.
  6. resize(x) will resize the vector container so that it can hold x elements.
  7. empty() returns true if the vector container has no elements.
  8. push_back() inserts a new element into the vector at the end.
  9. pop_back() extracts an element from the end of the vector container.
  10. insert() allows you to add an element to the vector at a specified position.

C++ Vector Example

Using vectors in C++ makes coding with arrays flexible and quick. Let’s take a look at some sample code that uses C++ vectors.

        #include<vector>
#include<iostream>
using namespace std;
 
void main()
{
    vector<int> arr;
 
    // push_back to add elements to end of our container
    arr.push_back(10);
    arr.push_back(5);
    arr.push_back(20);
 
    // vector now contains [10,5,20]
 
    // use begin() iterator to traverse container until end() pointer
    cout << "begin() and end() iterators: ";

    for (auto j = arr.begin(); j != arr.end(); j++)
    {
        cout << *j << ' ';
    }
 
   // Expected output: 10 5 20
 
    // rbegin() and rend() used to reverse traverse vector container
    cout << "rbegin() and rend() iterators: ";
 
    for (auto j = arr.rbegin(); j != arr.rend(); j++)
    {
        cout << *j << ' ';
    }
 
    // Expected output: 20 5 10
 
    cout << "The size of array is: " << arr.size() << endl;
    // Expected output: The size of array is: 3
 
    // Resize array to have only 2 elements
    arr.resize(2);
 
    cout << "The size of array is: " << arr.size() << endl;
    // Expected output: The size of array is: 2
 
    cout << "MaxSize(): " << arr.max_size() << endl;
    cout << "Vector Empty? " << arr.empty() << endl;
 
    // pop_back last element
    arr.pop_back();
 
    // insert 5 at beginning of array
    arr.insert(arr.begin(), 5);
}

Related: How to Learn C++ Programming

vector expected output

Using Vectors in C++

C++ vectors are flexible and powerful, which is why they are an essential feature for every C++ developer out there. The STL is a significant C++ library, and exploring it in detail can help you create better applications.