C++ 算法 is_partitioned() 用於測試範圍 [first, last) 是否根據謂詞進行分區。換句話說,範圍內滿足謂詞的所有元素都在序列的開頭。
如果範圍為空,則返回 true。
用法
template <class InputIterator, class UnaryPredicate>
bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred);
參數
first:指向範圍內第一個元素的輸入迭代器。
last:一個輸入迭代器,指向範圍中過去的最後一個元素。
pred: 一個用戶定義的一元謂詞函數,它為預期在範圍開頭找到的元素返回真。
返回值
如果範圍為空或由給定的謂詞 pred 分區,則此函數返回 true,否則返回 false。
複雜度
複雜度在 [first, last) 範圍內是線性的:為每個元素調用 pred 直到發現不匹配。
數據競爭
訪問範圍 [first, last) 中的對象。
每個元素隻被訪問一次。
異常
如果 pred 或迭代器上的操作引發異常,則此函數將引發異常。
請注意,無效參數會導致未定義的行為。
例子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
例子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.
例子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
示例 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.
相關用法
- C++ Algorithm is_sorted()用法及代碼示例
- C++ Algorithm is_heap()用法及代碼示例
- C++ Algorithm is_sorted_until()用法及代碼示例
- C++ Algorithm is_heap_until()用法及代碼示例
- C++ Algorithm inplace_merge()用法及代碼示例
- C++ Algorithm iter_swap()用法及代碼示例
- C++ Algorithm includes()用法及代碼示例
- C++ Algorithm remove_if()用法及代碼示例
- C++ Algorithm remove()用法及代碼示例
- C++ Algorithm max_element()用法及代碼示例
- C++ Algorithm set_union()用法及代碼示例
- C++ Algorithm next_permutation()用法及代碼示例
- C++ Algorithm upper_bound()用法及代碼示例
- C++ Algorithm minmax()用法及代碼示例
- C++ Algorithm remove_copy_if()用法及代碼示例
- C++ Algorithm random_shuffle()用法及代碼示例
- C++ Algorithm pop_heap()用法及代碼示例
- C++ Algorithm replace()用法及代碼示例
- C++ Algorithm set_intersection()用法及代碼示例
- C++ Algorithm lower_bound()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Algorithm is_partitioned()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。