function template
<algorithm>

std::is_partitioned

template <class InputIterator, class UnaryPredicate>  bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred);
測試範圍是否已分割槽
如果範圍內所有滿足 pred 返回 true 的元素都位於那些 pred 返回 false 的元素之前,則返回 true

如果範圍為空,函式返回 true

此函式模板的行為等同於
1
2
3
4
5
6
7
8
9
10
11
12
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 指向的元素以及 first 和 last 之間的所有元素,但不包含 last 指向的元素。
pred
一元函式,接受範圍中的元素作為引數,並返回一個可轉換為 bool 的值。返回的值指示元素是否屬於第一組(如果為 true,則該元素預期在所有返回 false 的元素之前)。
該函式不得修改其引數。
這可以是一個函式指標或一個函式物件。

返回值

如果範圍內所有滿足 pred 返回 true 的元素都位於那些 pred 返回 false 的元素之前,則返回 true
否則返回 false

如果範圍為空,函式返回 true

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 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)


複雜度

最多線性於 firstlast 之間的 距離:對每個元素呼叫 pred,直到找到不匹配為止。

資料競爭

範圍 [first,last) 中的一些(或全部)物件被訪問(最多一次)。

異常

如果 pred 或迭代器操作丟擲異常,則丟擲異常。
請注意,無效的引數會導致 *未定義行為*。

另見