C++算法库 (3)划分操作



/*is_partitioned   检测某个范围是否按指定谓词划分过*/
#include <iostream>
#include <algorithm>
#include <array>
using namespace std;



bool IsOdd(int i) {return (i % 2) == 1;}


int main(int argc, const char * argv[])
{

    array<int, 7> foo = {1,2,3,4,5,6,7};
    
    //print contents:
    cout<<"foo:";
    for (int& x:foo)
        cout<<" "<<x;
    if (is_partitioned(foo.begin(), foo.end(), IsOdd))
        cout<<" (partitioned)"<<endl;
    else
        cout<<" (not partitioned)"<<endl;
    
    
    //partition array:
    partition(foo.begin(), foo.end(), IsOdd);
    
    //print contents again:
    cout<<"foo:";
    for (int& x:foo)
        cout<<" "<<x;
    if (is_partitioned(foo.begin(), foo.end(), IsOdd))
        cout<<" (partitioned)"<<endl;
    else
        cout<<" (not partitioned)"<<endl;
    return 0;
}


输出结果:

foo: 1 2 3 4 5 6 7 (not partitioned)
foo: 1 7 3 5 4 6 2 (partitioned)


/*partition   将某个范围划分为两组*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;



bool IsOdd(int i) {return (i % 2) == 1;}


int main(int argc, const char * argv[])
{

    vector<int> myvector;
    vector<int>::iterator it;
    //set some values:
    for (int i = 1; i < 10; ++i)
        myvector.push_back(i);
    
    vector<int>::iterator bound;
    bound = partition(myvector.begin(), myvector.end(), IsOdd);
    
    //print out content:
    cout<<"odd elements:";
    for (it = myvector.begin(); it != bound; ++it)
        cout<<" "<<*it;
    cout<<endl;
    
    cout<<"even elements:";
    for (it = bound;it != myvector.end(); ++it )
        cout<<" "<<*it;
    cout<<endl;
    
    return 0;
}


输出结果:

odd elements: 1 9 3 7 5
even elements: 6 4 8 2


/*partition_copy   拷贝指定范围的划分结果*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;



bool IsOdd(int i) {return (i % 2) == 1;}


int main(int argc, const char * argv[])
{

    vector<int> foo;
    vector<int> odd,even;
    
    vector<int>::iterator it;
    
    //set some values:
    for (int i = 1; i < 10; ++i)
        foo.push_back(i);
    
    unsigned n = (unsigned)count_if(foo.begin(), foo.end(), IsOdd);
    odd.resize(n);
    even.resize(foo.size() - n);
    
    //partition:
    partition_copy(foo.begin(), foo.end(), odd.begin(), even.begin(), IsOdd);
    
    
    //print contents:
    cout<<"odd: ";
    for (int& x:odd)
        cout<<" "<<x;
    cout<<endl;
    
    cout<<"even: ";
    for (int& x:even)
        cout<<" "<<x;
    cout<<endl;
    
        
        
    return 0;
}

输出结果:

odd:  1 3 5 7 9
even:  2 4 6 8


/*partition_point    返回被划分范围的划分点*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;



bool IsOdd(int i) {return (i % 2) == 1;}


int main(int argc, const char * argv[])
{

    vector<int> foo;
    vector<int> odd;
    
    
    //set some values:
    for (int i = 1; i < 10; ++i)
        foo.push_back(i);
    partition(foo.begin(), foo.end(), IsOdd);
    
    auto it = partition_point(foo.begin(), foo.end(), IsOdd);
    odd.assign(foo.begin(), it);
    
    //print contents of odd:
    cout<<"odd:";
    for (int& x:odd)
        cout<<" "<<x;
    cout<<endl;
    
    return 0;
}


输出结果:

odd: 1 9 3 7 5

/*stable_partition    稳定划分,两组元素各维持相对顺序*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;



bool IsOdd(int i) {return (i % 2) == 1;}


int main(int argc, const char * argv[])
{

    vector<int> myvector;
    vector<int>::iterator it;
    vector<int>::iterator bound;
    
    
    for (int i = 1; i < 10; ++i)
        myvector.push_back(i);
    
    bound = stable_partition(myvector.begin(), myvector.end(), IsOdd);
    
    //print out content:
    cout<<"odd elements:";
    for (it = myvector.begin(); it != bound  ; ++it)
        cout<<" "<<*it;
    cout<<endl;
    
    cout<<"even elements:";
    for (it = bound; it != myvector.end(); ++it)
        cout<<" "<<*it;
    cout<<endl;
    return 0;
}


输出结果:
odd elements: 1 3 5 7 9
even elements: 2 4 6 8




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杜甲同学

感谢打赏,我会继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值