C++ Boost庫中的is_partitioned()函數位於標頭“ boost /algorithm /cxx11 /is_partitioned.hpp”下,該函數測試給定序列是否根據給定謂詞進行了劃分。這裏的分區是指序列中所有滿足謂詞的項目都在序列的開頭。
用法:
bool is_partitioned ( InputIterator first, InputIterator last, Predicate p )
or
bool is_partitioned ( const Range &r, Predicate p )
參數:該函數接受如下所述的參數:
- first:它指定輸入迭代器到序列中的初始位置。
- second:它指定輸入迭代器到序列中的最終位置。
- p:如果指定,則指定比較謂詞。
- r:完全指定給定範圍。
返回值:如果根據給定條件對完整序列進行排序,則該函數返回true,否則返回false。
下麵是上述方法的實現:
程序1::
// C++ program to implement the
// above mentioned function
#include <bits/stdc++.h>
#include <boost/algorithm/cxx11/is_partitioned.hpp>
using namespace std;
// Predicate function to check
// if the element is odd or not
bool isOdd(int i)
{
return i % 2 == 1;
}
// Drivers code
int main()
{
// Declares the sequence with
int c[] = { 1, 2, 5, 6, 8 };
// Run the function
bool ans
= boost::algorithm::is_partitioned(c, isOdd);
// Condition to check
if (ans == 1)
cout << "Sequence is partitioned";
else
cout << "Sequence is not partitioned";
return 0;
}
輸出:
Sequence is not partitioned
程序2::
// C++ program to implement the
// above mentioned function
#include <bits/stdc++.h>
#include <boost/algorithm/cxx11/is_partitioned.hpp>
using namespace std;
// Predicate function to check
// if the element is odd or not
bool isEven(int i)
{
return i % 2 == 0;
}
// Drivers code
int main()
{
// Declares the sequence with
int c[] = { 4, 2, 5, 6, 8 };
// Run the function
bool ans
= boost::algorithm::is_partitioned(c,
c + 2,
isEven);
// Condition to check
if (ans == 1)
cout << "Sequence is partitioned";
else
cout << "Sequence is not partitioned";
return 0;
}
輸出:
Sequence is partitioned
相關用法
- C++ boost::split用法及代碼示例
- C++ boost::algorithm::none_of()用法及代碼示例
- C++ boost::algorithm::clamp()用法及代碼示例
- C++ boost::algorithm::equal()用法及代碼示例
- C++ boost::algorithm::any_of()用法及代碼示例
- C++ boost::algorithm::none_of_equal()用法及代碼示例
- C++ boost::algorithm::one_of_equal()用法及代碼示例
- C++ boost::algorithm::all_of()用法及代碼示例
- C++ boost::algorithm::all_of_equal()用法及代碼示例
- C++ boost::algorithm::one_of()用法及代碼示例
- C++ boost::algorithm::is_sorted()用法及代碼示例
注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 boost::algorithm::is_partitioned() in C++ library。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。