STL min_element and max_element in C++

Level 4212 keystrokes

Finds the smallest and largest elements of a vector with min_element and max_element, which return iterators, and prints Min: 1 and Max: 9.

C++ code

1vector<int> nums = {5, 2, 8, 1, 9, 3};
// min_element returns an iterator to the smallest element, not the value
2auto minIt = min_element(nums.begin(), nums.end());
// max_element does the same for the largest element
3auto maxIt = max_element(nums.begin(), nums.end());
// Dereference the iterator to get the value: prints Min: 1
4cout << "Min: " << *minIt << endl;
// Prints Max: 9
5cout << "Max: " << *maxIt << endl;

Output

Min: 1
Max: 9
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.

  • <× 13
  • ,× 7
  • (× 6
  • )× 6
  • ;× 5
  • .× 4
  • "× 4
  • =× 3
  • _× 2
  • :× 2

More C++ examples at level 4

← C++ typing practice