Iterator and Range-Based Algorithms

Finding the Smallest and Largest Elements in a Range in C++

What is the best way to find the smallest and largest elements in a range?

To find the smallest and largest elements in a range in C++, you can use the std::min_element() and std::max_element() algorithms from the <algorithm>Â header.

These functions are efficient and straightforward, operating in linearÂ time.

Finding the Smallest Element

The std::min_element() function returns an iterator to the smallest element in the range [first, last).

Hereâ€™s anÂ example:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers{3, 1, 4, 1, 5, 9};

auto min_it = std::min_element(
numbers.begin(), numbers.end());

if (min_it != numbers.end()) {
std::cout << "The smallest element is "
<< *min_it;
}
}
The smallest element is 1

Finding the Largest Element

Similarly, the std::max_element() function returns an iterator to the largest element in the range [first, last).

Hereâ€™s anÂ example:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
std::vector<int> numbers{3, 1, 4, 1, 5, 9};

auto max_it = std::max_element(
numbers.begin(), numbers.end());

if (max_it != numbers.end()) {
std::cout << "The largest element is "
<< *max_it;
}
}
The largest element is 9

Finding Both Elements Simultaneously

You can find both the smallest and largest elements in a single pass using std::minmax_element().

This function returns a pair of iterators: the first points to the smallest element, and the second points to the largestÂ element.

Hereâ€™s anÂ example:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
std::vector<int> nums{3, 1, 4, 1, 5, 9};

auto [min_it, max_it] = std::minmax_element(
nums.begin(), nums.end());

if (min_it != nums.end() && max_it != nums.end()) {
std::cout << "The smallest element is "
<< *min_it << "\n";
std::cout << "The largest element is "
<< *max_it << "\n";
}
}
The smallest element is 1
The largest element is 9

Practical Considerations

• Range Validity: Ensure that the range [first, last) is valid and non-empty before calling these functions.
• Custom Comparators: You can provide custom comparator functions to std::min_element(), std::max_element(), and std::minmax_element() if you need to define a specific order.

Conclusion

Using std::min_element(), std::max_element(), and std::minmax_element() provides an efficient and clear way to find the smallest and largest elements in aÂ range.

These algorithms are part of the standard library, making them both reliable and easy to use in a wide variety ofÂ contexts.

This Question is from the Lesson:

Iterator and Range-Based Algorithms

An introduction to iterator and range-based algorithms, using examples from the standard library

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Iterator and Range-Based Algorithms

An introduction to iterator and range-based algorithms, using examples from the standard library

Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

• 124 Lessons
• 550+ Code Samples
• 96% Positive Reviews
• Regularly Updated
• Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.