Set container in C++

Level 4194 keystrokes

Inserts numbers into a set, which ignores duplicates and keeps elements sorted, then prints its size 3, the elements 1 3 5 and count(3), which is 1.

C++ code

// A set holds unique values in sorted order
1set<int> nums;
2nums.insert(5);
3nums.insert(3);
// 5 is already present, so this insert does nothing
4nums.insert(5);
5nums.insert(1);
6
// Three distinct values: prints 3
7cout << nums.size() << endl;
// Iteration is in ascending order: 1 3 5
8for (int n : nums) {
9 cout << n << " ";
10}
11cout << endl;
// count returns 1 if the value is in the set, otherwise 0: prints 1
12cout << nums.count(3) << endl;

Output

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

  • <× 15
  • ;× 9
  • (× 7
  • )× 7
  • .× 6
  • "× 2
  • :× 1
  • {× 1
  • }× 1
  • >× 1

More C++ examples at level 4

← C++ typing practice