The prototype of is_sort:
: Is_sorted
Default (1) |
template <class ForwardIterator> bool is_sorted (ForwardIterator first, ForwardIterator last); |
Custom (2) |
template <class ForwardIterator, class Compare> bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp); |
This function is used to test whether the elements in the range are ordered!
Use operator <or comp for comparison.
If the number of elements in the range is less than two, true is always returned.
The behavior is similar to the following:
123456789101112
|
template <class ForwardIterator> bool is_sorted (ForwardIterator first, ForwardIterator last){ if (first==last) return true; ForwardIterator next = first; while (++next!=last) { if (*next<*first) // or, if (comp(*next,*first)) for version (2) return false; ++first; } return true;}
|
A simple test example:
#include <iostream>#include <vector>#include <algorithm>using namespace std;int main(int argv,char **argc){vector<int> v1{1,2,3,4};vector<double> v2{4.0,5.0,3.5,6.0};cout<<"v1=";for(int i:v1)cout<<i<<" ";cout<<endl;if(is_sorted(v1.begin(),v1.end()))cout<<"v1 is sorted!"<<endl;elsecout<<"v1 is not sorted!"<<endl;cout<<"v2=";for(double i:v2)cout<<i<<" ";cout<<endl;if(is_sorted(v2.begin(),v2.end()))cout<<"v2 is sorted!"<<endl;elsecout<<"v2 is not sorted!"<<endl;}
Running result:
Is_sorted_until prototype:
STD: is_sorted_until
Default (1) |
template <class ForwardIterator> ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last); |
Custom (2) |
template <class ForwardIterator, class Compare> ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last, Compare comp); |
This function is similar to the relationship with is_heap and is_heap_until.
Returns the first element iterator that destroys sequence order.
Use operator <or comp for comparison.
Its behavior is similar:
234567891011
|
template <class ForwardIterator> ForwardIterator is_sorted_until (ForwardIterator first, ForwardIterator last){ if (first==last) return first; ForwardIterator next = first; while (++next!=last) { if (*next<*first) return next; ++first; } return last;}
|
A simple example:
#include <iostream>#include <vector>#include <algorithm>using namespace std;int main(int argv,char **argc){vector<int> v1{1,2,3,4,5};vector<double> v2{4.0,5.0,3.5,6.0,1.0};cout<<"v1=";for(int i:v1)cout<<i<<" ";cout<<endl;cout<<"v2=";for(double i:v2)cout<<i<<" ";cout<<endl;auto it=is_sorted_until(v1.begin(),v1.end());if(it==v1.end())cout<<"v1 is sorted!"<<endl;auto it2=is_sorted_until(v2.begin(),v2.end());cout<<"v2 the return is "<<*it2<<endl;}
Run:
By comparing the source code, we can know that the first return is v1.end (), and the second returns 3.5!
------------------------------------------------------------------
// 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: [email protected]
Yu gdut
------------------------------------------------------------------
STL algorithm algorithms is_sorted and is_sorted_until (28)