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.

Operations on Vectors

Vectors support several operations such as concatenation, insertion, and resizing.

#include <iostream>
#include <vector>

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

    // Concatenation: Inserting vec2 elements into vec1
    vec1.insert(vec1.end(), vec2.begin(), vec2.end());

    for (const auto& fruit : vec1) {
        std::cout << fruit << " ";
    }
    // Output: apple banana cherry durian
    
    std::cout << std::endl;

    // Resizing
    vec1.resize(6, "elderberry"); // Resizing to 6 elements, initializing new elements with "elderberry"

    for (const auto& fruit : vec1) {
        std::cout << fruit << " ";
    }
    // Output: apple banana cherry durian elderberry elderberry

    std::cout << std::endl << (vec1.front() == "apple") << std::endl;  // Output: 1 (true)

    return 0;
}

In the above example:

  • vec1.insert(vec1.end(), vec2.begin(), vec2.end()) concatenates vec2 to vec1. The insert method takes an iterator pointing to the position where elements should be inserted, followed by a range defined by two iterators (vec2.begin() and vec2.end()).
  • vec1.resize(6, "elderberry") resizes vec1 to contain 6 elements. If the new size is greater than the current size, new elements are initialized with "elderberry".
  • vec1.front() returns a reference to the first element of the vector, which in this case is "apple". This is useful for accessing the first element directly without needing to use an index, which can make it easier to write more reusable code.
Nested Vectors and Other Advanced Concepts

A vector can contain other vectors, resulting in nested vectors. Here's an example of creating a nested vector:

#include <iostream>
#include <vector>

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

Structured bindings can also simplify vector usage, making it more intuitive to work with.

Lesson Summary

Excellent job! In this lesson, you've learned what C++ vectors are and how to create, inspect, and operate them. You've also learned some advanced concepts, such as nested vectors.

Going forward, our focus will be on meticulously designed practice exercises that solidify your understanding. Remember, the key to successful learning is practice. Reinvent these examples by breaking them and fixing them again. Let's dive deeper!

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