Vector Modifications

Contents

Vector Modifications#

Here are some examples that modify the container.

Inserting#

We saw that .push_back() is used to add an element to the end of a vector. To insert in the middle of the vector, we use .insert(it_pos), where it_pos is an iterator pointing to the element in the vector we want to insert in front of.

Here’s an example: we start with a vector with the elements 100, 200, 300 and then use insert() to put 150 in between 100 and 200.

Listing 89 insert_example.cpp#
#include <iostream>
#include <vector>
#include <algorithm>

int main() {

    std::vector<int> int_vec{100, 200, 300};

    auto it = std::ranges::find(int_vec, 200);

    int_vec.insert(it, 150);

    for (auto e : int_vec) {
        std::cout << e << std::endl;
    }

}

Note: insert() can actually allow you to insert multiple elements by specifying an additional argument.

Erasing#

Erasing works similar to inserting. We give an iterator pointing to the start and end of the range we want to erase, and all elements up to, but not including the end, are erased.

Important

The end point is exclusive rather than inclusive is consistent with .end() returning an iterator that points one-past the end of the vector.

Here’s an example that removes the first 4 elements of a vector.

What happens if we try to remove past the end? To be safe, we should always add a check on whether our end is past .end().

Listing 90 vector_erase.cpp#
#include <iostream>
#include <vector>

int main() {

    std::vector<int> int_vec{-1, 10, 2, 4, 6, 19, -100, 2, 4};

    std::cout << "initial vector: ";
    for (auto e : int_vec) {
        std::cout << e << " ";
    }
    std::cout << std::endl;

    auto it = int_vec.begin();

    int_vec.erase(it, it+4);

    std::cout << "updated vector: ";
    for (auto e : int_vec) {
        std::cout << e << " ";
    }
    std::cout << std::endl;

}