6.3) Adding And Removing Vector Elements
Some of the methods that can be called on a vector are shown below.
push_back |
Adds an element with the given value to the end of the vector |
pop_back |
Removes the element currently at the end of the vector |
insert |
Inserts an element with the given value into the vector at the given position (note: uses iterators - covered later) |
erase |
Removes element(s) at the given position(s) in the vector (note: uses iterators - covered later) |
clear |
Removes all elements from the vector |
Here are some examples of using these methods:
vector<string> words; // vector initially empty words.push_back("Apple"); words.push_back("Banana"); word.push_back("Kiwi"); // vector has 3 elements words.pop_back(); // 2 elements ("Kiwi" removed) words.clear(); // vector empty again