STL algorithm algorithms is_sorted and is_sorted_until (28)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.