algorithm::is_partitioned()
描述 (Description)
C ++函数std::algorithm::is_partitioned()测试范围是否已分区。 对于空范围,此函数返回true。
声明 (Declaration)
以下是std :: algorithm :: is_partitioned()函数形式std :: algorithm头的声明。
C++11
template <class InputIterator, class UnaryPredicate>
bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred);
参数 (Parameters)
first - 将迭代器输入到初始位置。
last - 将迭代器输入到最终位置。
pred - 一个接受元素并返回bool的一元函数。
返回值
如果范围是分区则返回true,否则返回false。
异常 (Exceptions)
如果对迭代器的pred或操作抛出异常,则抛出异常。
请注意,无效参数会导致未定义的行为。
时间复杂
线性。
例子 (Example)
以下示例显示了std :: algorithm :: is_partitioned()函数的用法。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool is_even(int n) {
return (n % 2 == 0);
}
int main(void) {
vector<int> v = {1, 2, 3, 4, 5};
bool result;
result = is_partitioned(v.begin(), v.end(), is_even);
if (result == false)
cout << "Vector is not partitioned." << endl;
partition(v.begin(), v.end(), is_even);
result = is_partitioned(v.begin(), v.end(), is_even);
if (result == true)
cout << "Vector is paritioned." << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
Vector is not partitioned.
Vector is paritioned.