Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

is_partitioned

函数模板  <algorithm>
template <class InputIterator, class UnaryPredicate>
  bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred);

测试是否对范围进行了分区

如果pred返回true的范围[first,last)中的所有元素都在返回false的元素之前,则返回true。

如果范围为空,函数返回true。

这个函数模板的行为相当于:
template <class InputIterator, class UnaryPredicate>
  bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred)
{
  while (first!=last && pred(*first)) {
    ++first;
  }
  while (first!=last) {
    if (pred(*first)) return false;
    ++first;
  }
  return true;
}

☲  参数


first, last
指向序列的初始和最终位置的输入迭代器。 使用的范围是[first,last), 它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。

pred
一个一元函数,它接受范围中的一个元素作为参数,并返回一个可转换为bool的值。 返回的值指示该元素是否属于第一组(如果为真,则期望该元素在它返回false的所有元素之前)。
函数不应修改其参数。
它可以是函数指针,也可以是函数对象。

☉  返回值



如果pred返回的[first,last)范围内的所有元素都在返回false的元素之前,则为True。 否则返回false.

如果范围为空,函数返回true。

☣  示例



// is_partitioned example
#include <iostream>     // std::cout
#include <algorithm>    // std::is_partitioned
#include <array>        // std::array

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

int main () {
  std::array<int,7> foo {1,2,3,4,5,6,7};

  // print contents:
  std::cout << "foo:"; for (int& x:foo) std::cout << ' ' << x;
  if ( std::is_partitioned(foo.begin(),foo.end(),IsOdd) )
    std::cout << " (partitioned)\n";
  else
    std::cout << " (not partitioned)\n";

  // partition array:
  std::partition (foo.begin(),foo.end(),IsOdd);

  // print contents again:
  std::cout << "foo:"; for (int& x:foo) std::cout << ' ' << x;
  if ( std::is_partitioned(foo.begin(),foo.end(),IsOdd) )
    std::cout << " (partitioned)\n";
  else
    std::cout << " (not partitioned)\n";

  return 0;
}

输出:
foo: 1 2 3 4 5 6 7 (not partitioned)
foo: 1 7 3 5 4 6 2 (partitioned)

✥ 复杂度



first and last 之间的距离线性:对每个元素调用pred,直到发现不匹配。

⇄ 数据竞争


[first,last)范围内的部分(或全部)对象被访问(最多一次)。

☂ 异常安全性



如果迭代器上的pred或操作抛出,则抛出。
注意,无效的参数会导致未定义的行为。

🍄  另请参阅



partition 对范围分区 (函数模板)
partition_point 获取分区点(函数模板)

联系我们 免责声明 关于CandCplus 网站地图