C++ 算法 中的 stable_partition函数

首页 / C++入门教程 / C++ 算法 中的 stable_partition函数

C++算法 stable_partition()函数用于对[first,last)参数内的元素进行分类,以使pred返回true的所有元素都先于false返回的所有元素,保留元素相对顺序的位置。

Note: - 通常使用内部临时缓冲区来实现此功能。

stable_partition - 语法

template <class BidirectionalIterator, class UnaryPredicate>
BidirectionalIterator stable_partition (BidirectionalIterator first,
BidirectionalIterator last,
UnaryPredicate pred);

stable_partition - 参数

first:一个双向迭代器,指向要分区参数内的第一个元素。

last:一个双向迭代器,指向要分区参数内的最后一个最后一个元素。

pred :用户定义的一元谓词函数,用于定义要对元素进行分类时要满足的条件。

stable_partition - 返回值

此函数将迭代器返回到不满足谓词条件的参数的第一个元素。

stable_partition - 例子1

让我们看一个简单的示例,以演示stable_partition()的用法:

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-algorithm-stable-partition-function.html

来源:LearnFk无涯教程网

#include <iostream>    //std::cout
#include <algorithm>   //std::stable_partition
#include <vector>      //std::vector

using namespace std;

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

int main () {
  vector<int> myvector;

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

  vector<int>::iterator bound;
  bound = stable_partition (myvector.begin(), myvector.end(), IsOdd);

 //print out content:
  cout << "odd elements:";
  for (vector<int>::iterator it=myvector.begin(); it!=bound; ++it)
    cout << ' ' << *it;
  cout << '\n';

  cout << "even elements:";
  for (vector<int>::iterator it=bound; it!=myvector.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  return 0;
}

输出:

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

在上面的示例中,从1到9的元素被划分为偶数和奇数元素。

stable_partition - 例子2

让我们看另一个简单的例子:

#include <vector>  
#include <algorithm>  
#include <iostream>  
  
bool greater5 ( int value ) {  
   return value >5;  
}  
  
int main( ) {  
   using namespace std;  
   vector <int> v1, v2;  
   vector <int>::iterator Iter1, Iter2, result;  
  
   int i;  
   for ( i = 0 ; i <= 10 ; i++ )  
      v1.push_back( i );  
  
   int ii;  
   for ( ii = 0 ; ii <= 4 ; ii++ )  
      v1.push_back( 5 );  
  
   random_shuffle(v1.begin( ), v1.end( ) );  
  
   cout << "Vector v1 is ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
  //Partition the range with predicate greater10  
   result = stable_partition (v1.begin( ), v1.end( ), greater5 );  
   cout << "The partitioned set of elements in v1 is:\n ( " ;  
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )  
      cout << *Iter1 << " ";  
   cout << ")." << endl;  
  
   cout << "The first element in v1 to fail to satisfy the"  
        << "\n predicate greater5 is: " << *result << "." << endl;  
        
        return 0;
}

输出:

Vector v1 is ( 4 10 5 5 5 5 5 1 6 9 3 7 8 2 0 5 ).
The partitioned set of elements in v1 is:
 ( 10 6 9 7 8 4 5 5 5 5 5 1 3 2 0 5 ).
The first element in v1 to fail to satisfy the
 predicate greater5 is: 4.

stable_partition - 例子3

让我们看另一个简单的示例,使用partition()函数对vector的元素进行快速排序:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
 
int main()
{
    vector<int> v{0, 0, 3, 0, 2, 4, 5, 0, 7};
    stable_partition(v.begin(), v.end(), [](int n){return n>0;});
    for (int n : v) {
        cout << n << ' ';
    }
    cout << '\n';
    
    return 0;
}

输出:

3 2 4 5 7 0 0 0 0

stable_partition - 例子4

让我们看另一个简单的例子:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
  vector<int> v = {1, 0, 3, 4, 5, 6, 12, 7, 9};

  stable_partition(v.begin(), v.end(), [](int x) { return x % 3 == 0; });

  for_each(v.begin(), v.end(), [](int x) {
   cout << x << endl;
  });
  
  return 0;
}

输出:

0
3
6
12
9
1
4
5
7

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Linux性能优化实战 -〔倪朋飞〕

数据分析实战45讲 -〔陈旸〕

大规模数据处理实战 -〔蔡元楠〕

正则表达式入门课 -〔涂伟忠〕

实用密码学 -〔范学雷〕

操作系统实战45讲 -〔彭东〕

运维监控系统实战笔记 -〔秦晓辉〕

后端工程师的高阶面经 -〔邓明〕

结构思考力 · 透过结构看问题解决 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)