當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ any_of()用法及代碼示例

any_of()是STL中<algorithm>庫中定義的C++函數。該函數確定給定範圍內的一個元素是否滿足指定的標準。如果至少有一個元素滿足該屬性,則返回 true;否則,返回 false。此外,如果範圍為空,則此函數返回 false。 any_of() 函數采用 3 個參數,第一個元素、最後一個元素和函數 condition_function。

函數模板:

template <class InputIterator, class UnaryPredicate>
bool any_of (InputIterator start_limit, InputIterator end_limit, UnaryPredicate condition_function);

參數:

  • start_limit- 它是指定範圍內的第一個元素。
  • end_limit- 它是範圍中的最後一個元素。
  • condition_function- 它是一個接受範圍內參數的函數。

時間複雜度:在)
輔助空間:O(1)

例子:

C++


// C++ Program to demonstrate
// working of any_of()
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
    // Initializing the array
    int ar[6] = { 2, 6, 7, 10, 8, 4 };
    // Checking if any odd number is
    // present or not in the array
    if (any_of(ar, ar + 6,
               [](int x) { return x % 2 != 0; })) {
        cout << "There exists odd number in the array";
    }
    else {
        cout << "No odd number found in the array";
    }
    return 0;
}
輸出
There exists odd number in the array

相關用法


注:本文由純淨天空篩選整理自pushpeshrajdx01大神的英文原創作品 any_of() Function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。