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
相關用法
- C++ any_of()用法及代碼示例
- C++ asin()用法及代碼示例
- C++ atan()用法及代碼示例
- C++ atan2()用法及代碼示例
- C++ acos()用法及代碼示例
- C++ acosh()用法及代碼示例
- C++ asinh()用法及代碼示例
- C++ atanh()用法及代碼示例
- C++ atof()用法及代碼示例
- C++ atol()用法及代碼示例
- C++ atexit()用法及代碼示例
- C++ at_quick_exit()用法及代碼示例
- C++ asctime()用法及代碼示例
- C++ adjacent_find()用法及代碼示例
- C++ all_of()用法及代碼示例
- C++ abort()用法及代碼示例
- C++ abs()用法及代碼示例
- C++ array::back()用法及代碼示例
- C++ array::front()用法及代碼示例
- C++ atoi()用法及代碼示例
- C++ atoll()用法及代碼示例
- C++ array at()用法及代碼示例
- C++ array get()用法及代碼示例
- C++ array data()用法及代碼示例
- C++ array::max_size()用法及代碼示例
注:本文由純淨天空篩選整理自pushpeshrajdx01大神的英文原創作品 any_of() Function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。