Searching a set in C++

Level 3120 keystrokes

set::find returns an iterator to the element, or end() if it is not there. 20 is in the set, so the result differs from end() and "Found" is printed.

C++ code

1set<int> s = {10, 20, 30};
// find returns end() when the value is missing; a set search is fast (logarithmic)
2if (s.find(20) != s.end()) {
// 20 is present, so this branch runs
3 cout << "Found" << endl;
4} else {
// Would run for a value such as 25
5 cout << "Not found" << endl;
6}

Output

Found
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
  • "× 4
  • ;× 3
  • (× 3
  • )× 3
  • {× 3
  • }× 3
  • ,× 2
  • .× 2
  • =× 2

More C++ examples at level 3

← C++ typing practice