Vector Push Back in C++

Level 286 keystrokes

Starts with an empty vector, adds 10 and 20 with push_back(), and prints the new size, 2.

C++ code

// Declares an empty vector of ints (size 0)
1vector<int> nums;
// push_back() adds 10 at the end; the vector grows to 1 element
2nums.push_back(10);
// Adds 20 after 10, giving {10, 20}
3nums.push_back(20);
// Prints the number of elements, 2
4cout << nums.size() << endl;

Output

2
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
  • ;× 4
  • .× 3
  • (× 3
  • )× 3
  • _× 2
  • >× 1

More C++ examples at level 2

← C++ typing practice