Книга: Standard Template Library Programmer
min_element
Разделы на этой странице:
min_element
Category: algorithms
Component type: function
Prototype
Min_element is an overloaded name; there are actually two min_element functions.
template <class ForwardIterator>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last);
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last, BinaryPredicate comp);
Description
Min_element finds the smallest element in the range [first, last). It returns the first iterator i in [first, last) such that no other iterator in [first, last) points to a value smaller than *i. The return value is last if and only if [first, last) is an empty range.
The two versions of min_element differ in how they define whether one element is less than another. The first version compares objects using operator<, and the second compares objects using a function object comp.
The first version of min_element returns the first iterator i in [first, last) such that, for every iterator j in [first, last), *j < *i is false. The second version returns the first iterator i in [first, last) such that, for every iterator j in [first, last), comp(*j, *i) is false.
Definition
Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.
Requirements on types
For the first version:
• ForwardIterator is a model of Forward Iterator.
• ForwardIterator's value type is LessThan Comparable.
For the second version:
• ForwardIterator is a model of Forward Iterator.
• BinaryPredicate is a model of Binary Predicate.
• ForwardIterator's value type is convertible to BinaryPredicate's first argument type and second argument type.
Preconditions
• [first, last) is a valid range.
Complexity
Linear. Zero comparisons if [first, last) is an empty range, otherwise exactly (last – first) – 1 comparisons.
Example
int main() {
list<int> L;
generate_n(front_inserter(L), 1000, rand);
list<int>::const_iterator it = min_element(L.begin(), L.end());
cout << "The smallest element is " << *it << endl;
}
See also
min, max max_element, LessThan Comparable, sort, nth_element
- 3.3 Programming Examples
- Querying Elements
- 9. TERMINATION
- Элемент xsl:element
- Абстрактный базовый класс FileSystemInfo
- PART V Programming Linux
- CHAPTER 18 Administering Database Services
- Elements of the xorg.conf File
- Streaming Audio
- Linux Gaming
- Granting System Administrator Privileges to Regular Users
- Controlling Services at Boot with Administrative Tools