Vector Empty Check in C++

Level 293 keystrokes

Checks with empty() whether the vector v has no elements; v was just declared and is empty, so Empty is printed.

C++ code

// Declares a vector of ints with no elements
1vector<int> v;
// empty() returns true when size() is 0
2if (v.empty()) {
// v is empty, so Empty is printed
3 cout << "Empty" << endl;
// Runs only if v has at least one element
4} else {
// Would print the number of elements; skipped here
5 cout << v.size() << endl;
6}

Output

Empty
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.

  • <× 9
  • ;× 3
  • (× 3
  • )× 3
  • .× 2
  • "× 2
  • {× 2
  • }× 2
  • >× 1

More C++ examples at level 2

← C++ typing practice