C/C++ Arena

Step 3 of 6

Insert and erase

Positions in a vector are expressed with iterators. v.begin() points at the first element, and v.begin() + i at element i.

v.insert(v.begin() + 1, 99);   // insert before index 1
v.erase(v.begin() + 2);        // remove index 2
v.clear();                     // remove everything

Your turn: write void remove_at(std::vector<std::string>& v, int i) that removes element i if it's a valid index and does nothing otherwise.

Previous: Loop over a vector Next: 2D vectors