Vector Operations
In the Vectors lesson, push_back added a value at the end of a vector. A vector can also remove values, give you its first and last value, and add or remove a value in the middle.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
vector<string> tasks = {"wash", "cook", "read"};
cout << tasks.front() << "\n";
cout << tasks.back() << "\n";
tasks.pop_back();
cout << tasks.back() << "\n";
cout << tasks.size() << "\n";
return 0;
}
Output:
wash
read
cook
2
front() gives the first value, the same as tasks[0]. back() gives the last value, the same as tasks[tasks.size() - 1], but much easier to read.
pop_back() removes the last value. It is the opposite of push_back. After it, read is gone, so back() gives cook and the size is 2. pop_back does not give you the removed value. If you need it, read it with back() first.
An empty vector has no first or last value, so do not call these three functions on an empty vector.
Adding and removing in the middle
insert adds a value at any position, and erase removes the value at a position. They do not take a plain index. They take a position written as nums.begin() + i. For now, read it as "the position i places from the start". The Iterators lesson later in the course explains what begin() really is.
vector<int> nums = {10, 20, 30, 40};
nums.insert(nums.begin() + 1, 15);
nums.erase(nums.begin() + 3);
for (int n : nums) {
cout << n << " ";
}
cout << "\n";
Output:
10 15 20 40
insert put 15 at index 1, and the values after it moved one place to the right. The vector was then 10 15 20 30 40. Then erase removed the value at index 3, which was 30, and the values after it moved one place to the left. nums.begin() + 0, or just nums.begin(), is the very first position.
Moving the values takes time. In a vector with a million values, inserting at the front moves all million. Adding and removing at the end with push_back and pop_back moves nothing, so prefer the end when you can.
If you give erase a plain index, the program does not compile:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums = {10, 20, 30};
nums.erase(1);
return 0;
}
Output:
main.cpp: In function ‘int main()’:
main.cpp:7:15: error: no matching function for call to ‘std::vector<int>::erase(int)’
The message says that there is no erase that takes an int. Write nums.erase(nums.begin() + 1) instead.
Emptying a vector
clear() removes all the values. empty() tells you if the vector has no values. It returns a bool, so it prints 1 for true and 0 for false:
vector<int> nums = {1, 2, 3};
cout << nums.empty() << "\n";
nums.clear();
cout << nums.empty() << "\n";
cout << nums.size() << "\n";
Output:
0
1
0
empty() is useful in a loop. This loop takes values from the end until none are left, so it prints them in reverse order:
vector<int> nums = {1, 2, 3};
while (!nums.empty()) {
cout << nums.back() << "\n";
nums.pop_back();
}
Output:
3
2
1
In the exercise, edit a playlist: remove the last song, then remove one song and add a new one at the given positions.