现在的位置: 首页 > 综合 > 正文

stable_partition http://www.cplusplus.com/reference/algorithm/stable_partition/

2013年02月08日 ⁄ 综合 ⁄ 共 572字 ⁄ 字号 评论关闭
//通用算法stable_partition的作用是将容器中符合表达式条件的元素全都排列到容器的前半部,并返回最后一个符合项的迭代器地址
// stable_partition example
#include <iostream>
#include
<algorithm>
#include
<vector>
using namespace std;

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

int main () {
vector
<int> myvector;
vector
<int>::iterator it, bound;

// set some values:
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9

bound
= stable_partition (myvector.begin(), myvector.end(), IsOdd);

// print out content:
cout << "odd members:";
for (it=myvector.begin(); it!=bound; ++it)
cout
<< " " << *it;

cout
<< "\neven members:";
for (it=bound; it!=myvector.end(); ++it)
cout
<< " " << *it;

cout
<< endl;

return 0;
}

抱歉!评论已关闭.