STL algorithm min, min_element (35), stlalgorithm
Min prototype:
Std: min
Default (1) |
template <class T> const T& min (const T& a, const T& b); |
Custom (2) |
template <class T, class Compare> const T& min (const T& a, const T& b, Compare comp); |
Initializer list (3) |
template <class T> T min (initializer_list<T> il);template <class T, class Compare> T min (initializer_list<T> il, Compare comp); |
For (1), return the smallest of the two elements. If the two are the same, return.
Use operator <or comp for comparison.
For (3), the smallest element is returned. If there are multiple smallest elements, the first element is returned.
The behavior is similar:
23
|
template <class T> const T& min (const T& a, const T& b) { return !(b<a)?a:b; // or: return !comp(b,a)?a:b; for version (2)}
|
A simple example:
#include <iostream>#include <algorithm>#include <vector>using namespace std;void min2(){ cout<<"min(10,22)="<<min(10,22)<<endl; cout<<"min({1,2,5,7,9,999,888})="<<min({1,2,5,7,9,999,888})<<endl;}
Run:
Min_element prototype:
Std: min_element
Default (1) |
template <class ForwardIterator> ForwardIterator min_element (ForwardIterator first, ForwardIterator last); |
Custom (2) |
template <class ForwardIterator, class Compare> ForwardIterator min_element (ForwardIterator first, ForwardIterator last, Compare comp); |
Iterator of the element with the largest return value. If there are multiple elements, the first element is returned.
The behavior is similar:
template <class ForwardIterator> ForwardIterator min_element ( ForwardIterator first, ForwardIterator last ){ if (first==last) return last; ForwardIterator smallest = first; while (++first!=last) if (*first<*smallest) // or: if (comp(*first,*smallest)) for version (2) smallest=first; return smallest;}
A simple example:
#include <iostream>#include <algorithm>#include <vector>using namespace std;void minelement(){ vector<int> vi{1,1,2,3,4}; cout<<" vi="; for(int i:vi) cout<<i<<" "; cout<<endl; cout<<"min_element(vi.begin(),vi.end())="<<*min_element(vi.begin(),vi.end())<<endl; cout<<"min_element(vi.begin(),vi.begin()+1)="<<*min_element(vi.begin(),vi.begin()+1)<<endl;}
Run:
------------------------------------------------------------------
// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.
Reprinted please indicate the source: http://blog.csdn.net/qq844352155
Author: unparalleled
Email: coderguang@gmail.com
Yu GDUT
------------------------------------------------------------------
4. The following describes the algorithm in STL. Answer:
1. What algorithms are involved in sorting algorithm?
Sorting algorithms arrange unordered elements in the desired order, including fast sorting and heap sorting.
2. What is the nth_element function?
According to the sorting rule you specified, find the final element at the nth position
3. Which algorithm is sort implemented based on? What is average and worst case time complexity?
Sort is implemented based on the fast sorting algorithm. The worst is lgN for aver, Which is n ^ 2.
4. What is the difference between partial_sort () or stable_sort () and sort?
Partial_sor only sorts the first few of all elements.
Stable_sort (the sorting will be yo-yo. He guarantees that if a = B, a is originally in front of B, then after sorting, a is still in front of B, partial_sor is not guaranteed, so it may reverse after sorting
Sort is the general arrangement, and the original order is not guaranteed.
5. List the sorting algorithm in STL algorithms?
Including fast sorting and heap sorting
Reference: your head
Why is for_each a non-variable algorithm (stl c ++)
Std: for_each () is not a non-variable algorithm. A non-variable algorithm is an algorithm that does not modify data. For example
Element count count_if
Minimum and maximum min_element
Find find_if search
Interval comparison equal
For_each can modify the container value, so it is not a non-variable algorithm. In VC2012, we can track the core implementation of for_each as follows:
Template <class _ InIt,
Class _ Fn1> inline
_ Fn1 _ For_each (_ InIt _ First, _ InIt _ Last, _ Fn1 _ Func)
{// Perform function for each element
For (; _ First! = _ Last; ++ _ First)
_ Func (* _ First );
Return (_ Func );
}
You can see that the _ Func () function cannot change the value of * _ First.