
Vector Functions
—
Functions on the back of a vector.
push_back() void push_back(const int newVal);
Append new element having value newVal.
// playersList initially 55, 99, 44 (size is 3)
playersList.push_back(77); // Appends new element 77
// playersList is now 55, 99, 44, 77 (size is 4)
back() int back();
Returns value of vector’s last element. Vector is unchanged.
// playersList initially 55, 99, 44
cout << playersList.back(); // Prints 44
// playersList is still 55, 99, 44
pop_back() void pop_back();
Removes the last element.
// playersList is 55, 99, 44 (size 3)
playersList.pop_back(); // Removes last element
// playersList now 55, 99 (size 2)
cout << playersList.back(); // Common combination of back()
playersList.pop_back(); // followed by pop_back()
// Prints 99. playersList becomes just 55
cout << playersList.pop_back(); // Common error:
// pop_back() returns void
—