C++函数在STL的
句法:
template
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
first : Input iterators to the initial positions in a sequence.
last : Input iterators to the final positions in a sequence.
pred : An unary predicate function that accepts an element and returns a bool.
Exception:如果谓词或迭代器上的操作抛出异常,则抛出异常。
例子:
// C++ code to demonstrate working of all_of()
#include
#include
#include
int main()
{
std::vector v(10, 2);
// illustrate all_of
if (std::all_of(v.cbegin(), v.cend(), [](int i){ return i % 2 == 0; }))
{
std::cout << "All numbers are even\n";
}
}
输出:
All numbers are even
// C++ code to demonstrate working of all_of()
#include
#include // for all_of()
using namespace std;
int main()
{
// Initializing array
int ar[6] = {1, 2, 3, 4, 5, -6};
// Checking if all elements are positive
all_of(ar, ar+6, [](int x) { return x>0; })?
cout << "All are positive elements" :
cout << "All are not positive elements";
return 0;
}
输出:
All are not positive elements
在上面的代码中,-6为负元素会否定条件并返回false。
有用的数组算法STL函数
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。