Vector push_back in C++

Level 3119 keystrokes

Starts with an empty vector, appends 10, 20 and 30 with push_back, then prints them in the order they were added.

C++ code

// Creates an empty vector of int (size 0)
1vector<int> nums;
// push_back appends an element at the end; the vector grows automatically
2nums.push_back(10);
3nums.push_back(20);
4nums.push_back(30);
// Visits the elements in insertion order: 10, 20, 30
5for (int n : nums) {
// Prints each value on its own line
6 cout << n << endl;
7}

Output

10
20
30
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • ;× 5
  • <× 5
  • (× 4
  • )× 4
  • _× 3
  • .× 3
  • :× 1
  • {× 1
  • }× 1
  • >× 1

More C++ examples at level 3

← C++ typing practice