STL algorithm partition, partition_copy (43)

Source: Internet
Author: User

Partition prototype:

TD: Partition
  • C ++ 98
  • C ++ 11
template <class ForwardIterator, class UnaryPredicate>  ForwardIterator partition (ForwardIterator first,                             ForwardIterator last, UnaryPredicate pred);
This function divides the elements in the range according to whether the PRED returns true or false.

The return value is the first iterator whose return value is false. If no element returns false, the last is returned.

There are three criteria for division!

1. true for all;

2. The first half is true, and the second half is false.

3. All values are false.

Note: The first half is divided into false, and the second half is true, which does not belong to a division !!!
Look at: http://blog.csdn.net/qq844352155/article/details/39318925

The behavior is similar to the following:

template <class BidirectionalIterator, class UnaryPredicate>  BidirectionalIterator partition (BidirectionalIterator first,                                   BidirectionalIterator last, UnaryPredicate pred){  while (first!=last) {    while (pred(*first)) {      ++first;      if (first==last) return first;    }    do {      --last;      if (first==last) return first;    } while (!pred(*last));    swap (*first,*last);    ++first;  }  return first;

A simple test example:

#include <iostream>  #include <vector>  #include <array>  #include <algorithm>  using namespace std;  int main(){      vector<int> vi{8,5,10,7,1,3,5,8,9,13};      cout<<"vi=";      for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});      cout<<endl;      if(is_partitioned(vi.begin(),vi.end(),[](int n){return n%2==0;}))          cout<<"v1 is a partitioned!"<<endl;      else          cout<<"v1 not a partitioned!"<<endl;  auto it=partition(vi.begin(),vi.end(),[](int n){return n%2==0;});  cout<<"after  auto it=partition(vi.begin(),vi.end(),[](int n){return n%2==0;}); "<<endl;cout<<"vi=";     for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});  cout<<endl;if(is_partitioned(vi.begin(),vi.end(),[](int n){return n%2==0;}))          cout<<"v1 is a partitioned!"<<endl;      else          cout<<"v1 not a partitioned!"<<endl;  cout<<"*it="<<*it<<endl;    }  
Run:




Partition_copy prototype:

STD: partition_copy
template <class InputIterator, class OutputIterator1,          class OutputIterator2, class UnaryPredicate pred>  pair<OutputIterator1,OutputIterator2>    partition_copy (InputIterator first, InputIterator last,                    OutputIterator1 result_true, OutputIterator2 result_false,                    UnaryPredicate pred);

This function copies the elements in the range to the container pointed to by result_true and resutl_false based on the differences returned by calling Pred.

The returned value is a pair, pointing to the iterator where result_true and resutl_false overwrite the next element of the last element.

The behavior is similar to the following:

template <class InputIterator, class OutputIterator1,          class OutputIterator2, class UnaryPredicate pred>  pair<OutputIterator1,OutputIterator2>    partition_copy (InputIterator first, InputIterator last,                    OutputIterator1 result_true, OutputIterator2 result_false,                    UnaryPredicate pred){  while (first!=last) {    if (pred(*first)) {      *result_true = *first;      ++result_true;    }    else {      *result_false = *first;      ++result_false;    }    ++first;  }  return std::make_pair (result_true,result_false);}
A simple example:

#include <iostream>  #include <vector>  #include <array>  #include <algorithm>  using namespace std;  int main(){      vector<int> vi{8,5,10,7,1,3,5,8,9,13};vector<int> vTrue(10);vector<int> vFalse(10);      cout<<"vi=";      for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});      cout<<endl;  auto it=partition_copy(vi.begin(),vi.end(),vTrue.begin(),vFalse.begin(),[](int n){return n%2==0;});  cout<<"after partition_copy(vi.begin(),vi.end(),vTrue.begin(),vFalse.begin(),[](int n){return n%2==0;}) "<<endl;cout<<"vi=";     for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});  cout<<endl;cout<<"vTrue=";      for_each(vTrue.begin(),vTrue.end(),[](int i){cout<<i<<" ";});      cout<<endl; cout<<"vFalse=";      for_each(vFalse.begin(),vFalse.end(),[](int i){cout<<i<<" ";});      cout<<endl;  cout<<"*(it.first-1)="<<*(it.first-1)<<endl;cout<<"*(it.second-1)="<<*(it.second-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: [email protected]

Yu gdut

------------------------------------------------------------------






STL algorithm partition, partition_copy (43)

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.