算法之旅,直奔<algorithm>之一 all_of
2013-12-13 17:22
531 查看
all_of(vs2010版本)
上一次写了一个快速排序,分别用自己写的和库中的算法的函数进行了实验,耗费时间真心不忍直视,故此决心学习直奔<algorithm>库。
作用
all_of 的作用就是测试容器中所有的元素是否符合某个条件,如果符合返回为 true,否则返回 false。当然这个条件是自己定义的。
实验
实验数据是10个2,测试所有的数据都是偶数。
#include <vector> #include <algorithm> #include <iterator> #include <iostream> using namespace std; // function declare bool Condition( int i ); int main() { vector<int> v(10, 2); cout << "Among the numbers: "; copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ")); cout << '\n'; //if (all_of(v.cbegin(), v.cend(), [](int i){ return i % 2 == 0; })) if (all_of(v.begin(), v.end(), Condition)) { cout << "All numbers are even\n"; } system("pause"); return 0; } bool Condition( int i ) { return i % 2 == 0; }
相关文章推荐
- 算法之旅,直奔<algorithm>之二十三 none_of
- 算法之旅,直奔<algorithm>之十七 find_first_of
- 算法之旅,直奔<algorithm>之三 any_of
- 算法之旅,直奔<algorithm>之十七 find_first_of
- 算法之旅,直奔<algorithm>之九 count
- 算法之旅,直奔<algorithm>之十四 fill_n
- 算法之旅,直奔<algorithm>之十六 find_end
- 算法之旅,直奔<algorithm>之十一 equal
- 算法之旅,直奔<algorithm>之十三 fill
- 算法之旅,直奔<algorithm>之六 copy_backward
- 算法之旅,直奔<algorithm>之十八 for_each
- 算法之旅,直奔<algorithm>之二 adjacent_find
- 算法之旅,直奔<algorithm>之十九 includes
- 算法之旅,直奔<algorithm>之十五 find
- 算法之旅,直奔<algorithm>之十 count_if
- 算法之旅,直奔<algorithm>之四 binary_search
- 算法之旅,直奔<algorithm>之七 copy_if
- 算法之旅,直奔<algorithm>之十五 find
- 算法之旅,直奔<algorithm>之五 copy
- 算法之旅,直奔<algorithm>之二十一 max