Set basics in C++

Level 3116 keystrokes

A set keeps only unique values and stores them in sorted order. The duplicate 1 is dropped, so the size is 4 and the loop prints 1 3 4 5.

C++ code

// The second 1 is ignored because a set cannot hold duplicates
1set<int> nums = {3, 1, 4, 1, 5};
// Four distinct values: prints 4
2cout << nums.size() << endl;
// A set is iterated in ascending order: 1 3 4 5
3for (int n : nums) {
4 cout << n << " ";
5}
// Ends the line after the loop
6cout << endl;

Output

4
1 3 4 5 
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.

  • <× 11
  • ,× 4
  • ;× 4
  • "× 2
  • (× 2
  • )× 2
  • {× 2
  • }× 2
  • :× 1
  • .× 1

More C++ examples at level 3

← C++ typing practice