C++演算法庫 - is_partitioned() 函式



描述

C++函式std::algorithm::is_partitioned()測試範圍是否已分割槽。對於空範圍,此函式返回true。

宣告

以下是來自std::algorithm標頭檔案的std::algorithm::is_partitioned()函式宣告。

C++11

template <class InputIterator, class UnaryPredicate>
bool is_partitioned (InputIterator first, InputIterator last, UnaryPredicate pred);

引數

  • first − 指向初始位置的輸入迭代器。

  • last − 指向最終位置的輸入迭代器。

  • pred − 一個接受一個元素並返回bool值的單元函式。

返回值

如果範圍已分割槽,則返回true;否則返回false。

異常

如果任何一個pred或迭代器上的操作丟擲異常。

請注意,無效引數會導致未定義的行為。

時間複雜度

線性。

示例

以下示例演示了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.
algorithm.htm
廣告