STLalgorithm算法is_partitioned(26)


STLalgorithm算法is_partitioned(26)

is_partitioned原型:

std::is_partitioned

template <class InputIterator, class UnaryPredicate>
  bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred);

测试范围内的元素是否是以pred为准则的一个划分。如果是,则返回true,否则返回false.

也可以说成是测试满足pred的元素是否都在头部。

划分的意思是说,对每个元素进行pred(*it),得到的结果中true和false的值是否是一个划分

____________________________________________________________________________________________

回头试了一下,发现下面的结论是错误的请直接跳到后面的分隔线开始看正确的———2014.9.17

例如:T T T T F F F F或者T T T或者F F F或者F F F T都算是一个划分。

但是T F T或者F T F 就不是一个划分了。

其行为如下:(这个行为好像有点问题,例如得到的划分是是FFFTTT的话似乎应该是返回false才对)

template <class InputIterator1, class InputIterator2>
  bool is_partitioned (InputIterator1 first, InputIterator1 last, UnaryPredicate pred)
{
  while (first!=last && pred(*first)) {
    ++first;
  }
  while (first!=last) {
    if (pred(*first)) return false;
    ++first;
  }
  return true;
}

一个简单的例子:

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
void ispartitioned2(){
    vector<int> vi{1,3,5,7,9,8,6,10,12};
    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;

    vector<int> v2{2,4,6,8};
    cout<<"v2=";
    for_each(v2.begin(),v2.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    if(is_partitioned(v2.begin(),v2.end(),[](int n){return n%2!=0;}))
        cout<<"v2 is a partitioned!"<<endl;
    else
        cout<<"v2 not a partitioned!"<<endl;

    vector<int> v3{1,3,4,6,8};
    cout<<"v3=";
    for_each(v3.begin(),v3.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    if(is_partitioned(v3.begin(),v3.end(),[](int n){return n%2!=0;}))
        cout<<"v3 is a partitioned!"<<endl;
    else
        cout<<"v3 not a partitioned!"<<endl;


    vector<int> v4{1,2,5,4,6,8};
    cout<<"v4=";
    for_each(v4.begin(),v4.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    if(is_partitioned(v4.begin(),v4.end(),[](int n){return n%2!=0;}))
        cout<<"v4 is a partitioned!"<<endl;
    else
        cout<<"v4 not a partitioned!"<<endl;

}


运行截图:


可以看到,v3属于FFTTT类型,但是还是一个划分!

———————————————————————————————————————————————————

从这里开始是正确的!
其行为如下:

template <class InputIterator1, class InputIterator2>
  bool is_partitioned (InputIterator1 first, InputIterator1 last, UnaryPredicate pred)
{
  while (first!=last && pred(*first)) {
    ++first;
  }
  while (first!=last) {
    if (pred(*first)) return false;
    ++first;
  }
  return true;
}

该函数划分的标准有三种情况!

1.全部为true;

2.前半部分为true,后半部分为false.

3.全部为false.

注意,前半部分为false,后半部分为true不属于一个划分!!!
修改后的测试例子:

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
void ispartitioned2(){
    vector<int> vi{8,6,10,12,1,3,5,7,9,11};
    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;

    vector<int> v2{2,4,6,8};
    cout<<"v2=";
    for_each(v2.begin(),v2.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    if(is_partitioned(v2.begin(),v2.end(),[](int n){return n%2==0;}))
        cout<<"v2 is a partitioned!"<<endl;
    else
        cout<<"v2 not a partitioned!"<<endl;

    vector<int> v3{1,3,5,7,9};
    cout<<"v3=";
    for_each(v3.begin(),v3.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    if(is_partitioned(v3.begin(),v3.end(),[](int n){return n%2==0;}))
        cout<<"v3 is a partitioned!"<<endl;
    else
        cout<<"v3 not a partitioned!"<<endl;


    vector<int> v4{1,3,5,4,6,8};
    cout<<"v4=";
    for_each(v4.begin(),v4.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    if(is_partitioned(v4.begin(),v4.end(),[](int n){return n%2==0;}))
        cout<<"v4 is a partitioned!"<<endl;
    else
        cout<<"v4 not a partitioned!"<<endl;

}


运行截图:


可以看出来,FFTT这种是不属于这种划分的!

至于原因是因为is_partitioned函数的定义!就如前面所说的,我发现了一个问题,然后尝试去解决它。

我的办法就是写一个类似的函数去替代原本的is_partitioned,然后查看运行结果。

我的测试代码:

#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <iterator>
using namespace std;
  bool is_partitioned2 (vector<int>::iterator first,vector<int>::iterator  last,bool (*pred)(int))
{
  while (first!=last && pred(*first)) {
    ++first;
  }
  while (first!=last) {
    if (pred(*first)) return false;
    ++first;
  }
  return true;
}
void ispartitioned22(){
    vector<int> vi{1,3,5,7,9,8,6,10,12};
    cout<<"vi=";
    for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    if(is_partitioned2(vi.begin(),vi.end(),[](int n){return n%2==0;}))
        cout<<"v1 is a partitioned!"<<endl;
    else
        cout<<"v1 not a partitioned!"<<endl;

    vector<int> v2{2,4,6,8};
    cout<<"v2=";
    for_each(v2.begin(),v2.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    if(is_partitioned2(v2.begin(),v2.end(),[](int n){return n%2==0;}))
        cout<<"v2 is a partitioned!"<<endl;
    else
        cout<<"v2 not a partitioned!"<<endl;

    vector<int> v3{1,3,4,6,8};
    cout<<"v3=";
    for_each(v3.begin(),v3.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    if(is_partitioned2(v3.begin(),v3.end(),[](int n){return n%2==0;}))
        cout<<"v3 is a partitioned!"<<endl;
    else
        cout<<"v3 not a partitioned!"<<endl;


    vector<int> v4{1,2,5,4,6,8};
    cout<<"v4=";
    for_each(v4.begin(),v4.end(),[](int i){cout<<i<<" ";});
    cout<<endl;
    if(is_partitioned2(v4.begin(),v4.end(),[](int n){return n%2==0;}))
        cout<<"v4 is a partitioned!"<<endl;
    else
        cout<<"v4 not a partitioned!"<<endl;

}


运行结果:


遇到问题多动手,问题终会解决!!
——-2014.9.17


——————————————————————————————————————————————————————————————————

//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155

author:天下无双

Email:coderguang@gmail.com

2014-9-16

于GDUT

——————————————————————————————————————————————————————————————————




发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注