Working with Vectors in C++

Lesson Overview

In today's lesson, we'll explore vectors in C++, a versatile and widely used dynamic array data structure. Unlike arrays, std::vector allows dynamic resizing, making it a powerful tool for scenarios where the size of the dataset can change over time.

The beauty of std::vector lies in its ability to manage storage automatically, providing efficient access and modification options. By the end of this lesson, you'll be able to create, manipulate, and understand the unique applications of vectors in C++.

Understanding Vectors

A vector, stored as an instance of std::vector in C++, holds multiple items of the same type and supports dynamic resizing. This makes it more flexible compared to arrays, which have a fixed size. Vectors manage their own memory allocation, and their size can be changed as needed.

Consider this C++ vector declaration as an example:

#include <iostream>
#include <vector>

int main() {
    std::vector<std::string> my_vector = {"apple", "banana", "cherry"};
    for (const auto& fruit : my_vector) {
        std::cout << fruit << " ";
    }
    // Output: apple banana cherry
    return 0;
}

Inspecting and Modifying Vectors

In C++, you can access vector elements using the [] operator or the at method. Vectors can be modified by adding, removing, or changing elements.

The following is a simple example of inspecting and modifying vectors:

#include <iostream>
#include <vector>

int main() {
    std::vector<std::string> my_vector = {"apple", "banana", "cherry"};

    // Accessing elements
    std::cout << my_vector[1] << std::endl;  // Output: banana
    std::cout << my_vector.at(2) << std::endl;  // Output: cherry

    // Modifying elements
    my_vector[1] = "blueberry";  // Modifying the second element
    std::cout << my_vector[1] << std::endl;  // Output: blueberry

    // Adding and removing elements
    my_vector.push_back("durian");  // Adding a new element at the end
    my_vector.erase(my_vector.begin() + 2);  // Removing the third element ("cherry")

    for (const auto& fruit : my_vector) {
        std::cout << fruit << " ";
    }
    // Output: apple blueberry durian
    
    return 0;
}

In this example:

  • Accessing elements: my_vector[1] gets the second element ("banana"), and my_vector.at(2) gets the third element ("cherry").
  • Modifying elements: my_vector[1] = "blueberry" changes the second element from "banana" to "blueberry".
  • Adding and removing elements: my_vector.push_back("durian") adds "durian" at the end. my_vector.erase(my_vector.begin() + 2) removes the third element ("cherry").

Iterators are objects that point to elements within a container (like std::vector). They allow traversal through the container, similar to pointers, but also provide a level of abstraction and safety. Two common examples of functions returning iterators are begin(), which returns an iterator to the first element, and end(), which returns an iterator one past the last element.

In the example, my_vector.begin() returns an iterator to the first element, and adding 2 to it moves the iterator to the third element, which is then removed by erase(). Iterators are fundamental to many operations in C++ containers, offering versatile and efficient ways to manipulate and interact with the elements.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal