Vector operations in C++
std::vector is the default container: a growable array with the whole toolkit attached.
#include <vector>
int stack_demo() {
std::vector<int> nums = {3, 1, 4};
nums.push_back(15);
int last = nums.back();
nums.pop_back();
nums[0] = 2; // unchecked element access
return last + nums.front() + static_cast<int>(nums.size());
}
How it works
- A brace initializer seeds three elements;
push_backappends a fourth. back()reads the last element andpop_back()removes it, stack-style.nums[0]writes without bounds checking;front()andsize()read the result.
Keywords and builtins used here
intreturnstack_demostatic_cast
The run, in numbers
- Lines
- 10
- Characters to type
- 257
- Tokens
- 77
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 66 seconds.
Step 1 of 3 in Containers, step 7 of 27 in Language basics.