Sorting a vector in C++

Level 3109 keystrokes

sort from <algorithm> sorts the range from begin() to end() in ascending order. Duplicates are kept, so 1, 1, 3, 4, 5 is printed.

C++ code

// Unsorted values, with 1 appearing twice
1vector<int> nums = {3, 1, 4, 1, 5};
// Sorts the whole vector in place, in ascending order
2sort(nums.begin(), nums.end());
// Prints 1, 1, 3, 4, 5, one per line
3for (int n : nums) {
4 cout << n << endl;
5}

Output

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

  • ,× 5
  • <× 5
  • (× 4
  • )× 4
  • ;× 3
  • .× 2
  • {× 2
  • }× 2
  • :× 1
  • =× 1

More C++ examples at level 3

← C++ typing practice