typestar

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

  1. A brace initializer seeds three elements; push_back appends a fourth.
  2. back() reads the last element and pop_back() removes it, stack-style.
  3. nums[0] writes without bounds checking; front() and size() read the result.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 3 in Containers, step 7 of 27 in Language basics.

← Previous Next →

Vector operations in other languages