C++ 算法 中的 is_partitioned函数

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

C++算法 is_partitioned()用于测试以查看参数[first,last)是否根据谓词进行了分区。换句话说,满足谓词参数内的所有元素都在序列的开头。

如果参数为空,则返回true。

is_partitioned - 语法

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

is_partitioned - 参数

first :指向参数中第一个元素的输入迭代器。

last:一个输入迭代器,它指向参数中的最后一个最后一个元素。

pred :用户定义的一元谓词函数,对于预期在参数开头找到的元素,该函数返回true。

is_partitioned - 返回值

如果参数为空或被给定的谓词pred分区,则此函数返回true,否则返回false。

is_partitioned - 例子1

让我们看一个简单的示例来演示is_partitioned()的用法:

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

using namespace std;

int main()
{
  vector<int> v = {1, 2, 3, 4, 5};
  
  cout<<"Before Partition: ";
    for_each(v.begin(), v.end(), [](int v) {
   cout << v << " ";
  });


  auto pred = [](int x) { return x % 2 == 0; };

 //Divide it into an even group and an odd group 
  partition(v.begin(), v.end(), pred);
  cout<<"\nAfter partition : ";
  for_each(v.begin(), v.end(), [](int x) {
   cout << x << " ";
  });

  cout<<"\n\nIs it partitioned?"<<endl;
 //Is it divided into an even group and an odd group?  
  if (is_partitioned(v.begin(), v.end(), pred)) {
    cout << "Yes,It is Partitioned" << endl;
  }
  else {
    cout << "No,It is not Partitioned" << endl;
  }
  
  return 0;
}

输出:

Before Partition: 1 2 3 4 5 
After partition : 4 2 3 1 5 

Is it partitioned?
Yes,It is Partitioned

is_partitioned - 例子2

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

#include <iostream> 
#include <algorithm> 
#include <vector> 
  
// Defining the BinaryFunction 
bool pred(int a) 
{ 
    return (a % 2 == 0); 
} 
  
using namespace std; 
int main() 
{ 
   //Declaring first vector 
    vector<int> v1 = { 2, 4, 6, 3, 5, 7, 9 }; 
  
   //Using std::is_partitioned 
    bool b = std::is_partitioned(v1.begin(), v1.end(), pred); 
  
    if (b == 1) { 
        cout << "All the even no. are present before odd no."; 
    } else { 
        cout << "All the even no. are not present before odd no."; 
    } 
  
   //Inserting an even no. at the end of v1 
   //so std::is_partitioned returns false 
    v1.push_back(16); 
  
   //Now again using std::is_partitioned 
    b = std::is_partitioned(v1.begin(), v1.end(), pred); 
  
    if (b == 1) { 
        cout << "\nAll the even no. are present before odd no."; 
    } else { 
        cout << "\nAll the even no. are not present before odd no."; 
    } 
  
    return 0; 
}

输出:

All the even no. are present before odd no.
All the even no. are not present before odd no.

is_partitioned - 例子3

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

#include <algorithm>
#include <array>
#include <iostream>
 
int main()
{
    std::array<int, 9> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
    auto is_even = [](int i){ return i % 2 == 0; };
    std::cout.setf(std::ios_base::boolalpha);
    std::cout << std::is_partitioned(v.begin(), v.end(), is_even) << ' ';
 
    std::partition(v.begin(), v.end(), is_even);
    std::cout << std::is_partitioned(v.begin(), v.end(), is_even) << ' ';
 
    std::reverse(v.begin(), v.end());
    std::cout << std::is_partitioned(v.begin(), v.end(), is_even);
}

输出:

false true false 

is_partitioned - 例子4

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

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

using namespace std;

int main()
{
  vector<int> v = {11, 2, 3, 4, 15, 12};
  
  cout<<"Before Partition: ";
    for_each(v.begin(), v.end(), [](int v) {
   cout << v << " ";
  });


  auto pred = [](int x) { return x < 10; };
 
 //Divide it into an even group and an odd group 
  partition(v.begin(), v.end(), pred);
  cout<<"\nAfter partition: ";
  for_each(v.begin(), v.end(), [](int x) {
   cout << x << " ";
  });

  cout<<"\n\nIs it partitioned?"<<endl;
 //Is it divided into an even group and an odd group?  
  if (is_partitioned(v.begin(), v.end(), pred)) {
    cout << "Yes, It is Partitioned." << endl;
  }
  else {
    cout << "No, It is not Partitioned." << endl;
  }
  
  return 0;
}

输出:

Before Partition: 11 2 3 4 15 12 
After partition: 4 2 3 11 15 12 

Is it partitioned?
Yes, It is Partitioned.

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

技术教程推荐

推荐系统三十六式 -〔刑无刀〕

从0开始学架构 -〔李运华〕

Vue开发实战 -〔唐金州〕

移动端自动化测试实战 -〔思寒〕

设计模式之美 -〔王争〕

动态规划面试宝典 -〔卢誉声〕

说透数字化转型 -〔付晓岩〕

Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕

超级访谈:对话汤峥嵘 -〔汤峥嵘〕

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