Vector in C++ STL

Nitish Singh
5 min readDec 12, 2023

--

Vector container in C++ is a versatile and powerful tool for managing dynamic arrays efficiently. It provides an array-like interface with dynamic resizing and efficient element manipulation and provides efficient random access to elements, enabling programmers to access and modify specific elements quickly. Additionally, it offers efficient insertion and deletion operations, allowing programmers to add or remove elements from the vector with ease. The elements in the vector are stored next to each other, allowing for access through iterators as well as regular pointers. This means that a pointer to a vector element can be used in any function that expects a pointer to an array element.

// Defined in header <vector>
template<
class T,
class Allocator = std::allocator<T>
> class vector;

// (1)
namespace pmr {
template< class T >
using vector = std::vector<T, std::pmr::polymorphic_allocator<T>>;
}

The vector’s storage is managed automatically, expanding as necessary. Vectors generally occupy more space than static arrays because they allocate more memory to accommodate future growth. This enables the vector to avoid reallocating memory each time an element is inserted, only doing so when the additional memory is exhausted. The total allocated memory can be determined using the capacity() function, and excess memory can be returned to the system by calling shrink_to_fit(). Vector is sequence container which resizes itself automatically.

Reallocations are typically performance-intensive operations. To avoid them, the reserve() function can be utilized when the number of elements is known in advance.

The efficiency of common operations on vectors is as follows:

  • Random access: constant time complexity O(1).
  • Insertion or removal of elements at the end: amortized constant time complexity O(1).
  • Insertion or removal of elements at arbitrary positions: linear time complexity O(n) in relation to the distance from the end of the vector.

Declaring and Initializing Vectors in C++:
Before we can utilize its capabilities, let’s examine the process of declaring and initializing vectors in C++. To declare a vector in C++, begin by including the <vector> header file, which provides essential vector-related functionality. The fundamental syntax for declaring a vector is as follows:

#include <vector>
std::vector<datatype> vectorName;

Basic Operations with Vectors:
The user can replace “datatype” with the specific type of data they wish to store in the vector, such as int, double, or string. “vectorName” represents the custom name they select for their vector variable.

Now, let’s examine the different approaches for initializing vectors in C++:

// 1. Empty Initialization:
std::vector<int> numbers;

// 2. Initializing by pushing values one by one:
std::vector<int> vect;
vect.push_back(10);
vect.push_back(20);

// 3. Initialization with Size and Default Value:
std::vector<int> ages(5, 0); // Creates an integer vector with 5 elements, all initialized to 0


// 4. Initialization with Values:
std::vector<int> fibonacci {0, 1, 1, 2, 3, 5, 8}; // Creates an integer vector with given values.


// 5. Initialization using another Vector:
std::vector<int> original {1, 2, 3};
std::vector<int> copy(original); // Creates a new vector 'copy' with the same elements as 'original'.\


// 6. Initialize an array with consecutive numbers using std::iota:
std::vector<int> vec(5);
std::iota(vec.begin(), vec.end(), 1);

Insertion and Deletion of Elements in a Vector:
The C++ vector contains functions for adding and deleting elements. Typically, elements are added to the end of the vector. However, when an element is inserted at a specific position within the vector, all subsequent elements are shifted to accommodate the new addition.

In the case of deletion, removing elements from the end or a specific position reduces the size of the container by the number of elements deleted. Additionally, it removes elements from positions other than the end of the vector, causing the remaining elements to shift to their new positions.

Member Functions of a C++ Vector

The member functions of a C++ vector can be categorized into three types:

  1. Modifiers: These functions are responsible for modifying or changing the vector. For instance, the assign() function is used to clear the existing value and assign a new value to the vector. Please follow the following link to know more about the modifiers.
  2. Iterators: Iterator functions are used to traverse or iterate through the elements of the vector. For example, the end() function is used to point to the last element of the vector.
  3. Capacity: The functions in the capacity category are related to the size of the vector, such as changing the size. For example, the resize(n) function is used to alter the size of the vector.

Understanding Iterators:
Iterators are a fundamental concept in the C++ Standard Template Library (STL), and the vector container provides several types of iterators to traverse and manipulate its elements. Please follow the following link for other iterators and its usage.

Managing Capacity:
The vector container offers several functions to query and adjust its capacity based on the number of elements it holds. By understanding these functions, programmers can optimize memory usage and ensure efficient storage management when working with the vector container. Please refer to the following link for more information about capacity and its usage.

Accessing Elements:
Accessing and modifying elements within the vector container is one of its key strengths.Here are several methods that can be used to access and manipulate elements based on their positions within the vector. Please click the link for more information.

Conclusion:

In conclusion, the vector container in C++ is a powerful and versatile tool for managing collections of elements efficiently. It offers an array-like interface with dynamic resizing and efficient element manipulation, making it a fundamental component of the C++ Standard Template Library (STL). By understanding its features and capabilities, programmers can write optimized and robust code to handle collections of elements effectively in a wide range of applications.

Overall, the vector container in C++ is a versatile and powerful tool that every programmer should be familiar with. By leveraging its features and capabilities, programmers can write efficient and effective code for managing collections of elements in a wide range of applications.

--

--

Nitish Singh

Passionate about empowering devs with practical tips, using modern C++ as the tool, and up-to-date knowledge. Join me on Medium for engaging articles.