C++ STD Gems04
count、count_if、all_of、any_of、none_of
#include <iostream>
#include <vector>
#include <iterator>
#include <string>
#include <algorithm>
template<typename Container>
void write_to_cout(Container& container, const char* delimiter = " ")
{
std::copy(container.begin(), container.end(),
std::ostream_iterator<typename Container::value_type>(std::cout, delimiter) );
}
void test0()
{
std::vector<int> a = {1, 2, 3, 4, 2, 6, 7, 4, 5, 2};
write_to_cout(a);
std::cout << std::endl;
//test algorithm
std::cout << std::count(a.begin(), a.end(), 2) << std::endl; // 计算a中元素2的个数
}
void test1()
{
std::vector<int> a = {1, 2, 3, 4, 2, 6, 7, 4, 5, 11, 12, 6, 10, 9};
write_to_cout(a);
std::cout << std::endl;
//test algorithm
// 统计a中偶数的个数
std::cout << std::count_if(a.begin(), a.end(), [](int x){return x % 2 == 0;} ) << std::endl;
}
void test2()
{
std::vector<int> a = {2, 3, 4, 2, 6, 7, 4, 5, 11, 12, 6, 10, 9};
write_to_cout(a);
std::cout << std::endl;
// test algorithm
std::cout << std::all_of(a.begin(), a.end(), [](int a){return a < 13;}) << std::endl; //所有元素都小于13返回true
std::cout << std::any_of(a.begin(), a.end(), [](int a){return a < 5;}) << std::endl; // 只要有一个元素小于5返回true
std::cout << std::none_of(a.begin(), a.end(), [](int a){return a < 3;}) << std::endl; // 所有元素都不小于3返回ture
}
int main()
{
test0();
test1();
test2();
return 0;
}
C++ STD Gems04的更多相关文章
- 【NX二次开发】NX内部函数,libuifw.dll文件中的内部函数
本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: void U ...
- C++ std::set
std::set template < class T, // set::key_type/value_type class Compare = less<T>, // set::k ...
- C++ std::priority_queue
std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...
- C++ std::queue
std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...
- C++ std::multimap
std::multimap template < class Key, // multimap::key_type class T, // multimap::mapped_type class ...
- C++ std::map
std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...
- C++ std::list
std::list template < class T, class Alloc = allocator > class list; List Lists are sequence co ...
- C++ std::forward_list
std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...
- C++ std::deque
std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...
随机推荐
- 树莓派开启VNC远程桌面
分类: Raspberry Pi Linux2013-03-12 10:18 4288人阅读 评论(1) 收藏 举报 目录(?)[+] 1.安装VNC sudo apt-get install ...
- 【C#】关于左移/右移运算符的使用
吐槽先~为什么我的老师大学时候没教过我这东西 - -. 继续送栗子: 比如 “(1+2)<<3” 你们猜等于几~ Debug.Log((1+2)<<3)之后输出的是“24”. ...
- FTP 服务器在WIN10上的搭建及服务端下载文件实例
1.搭建 (1)控制面板--->程序----->将FTP服务器打勾 (2)输入iis,或者右键桌面-->管理-->服务和应用程序--->internet informat ...
- 5G/NR OTA (Over The Air) 测试详解
原文链接:http://www.sharetechnote.com/html/5G/5G_OTA.html 1 什么是OTA (Over The Air) OTA代表Over The Air.为了使用 ...
- 8051单片机中访问int中字节的方法
在使用单片机中,unsigned int 占2个字节,unsigned char 占一个字节.而单片机是实行的字节寻址.16字节的bit寻址实在是不好用, 不好用在不能建数组. 在实际的开发过程中,要 ...
- POJ 3348:Cows 凸包+多边形面积
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7739 Accepted: 3507 Description ...
- 编程题目:输入一个链表,输出该链表中倒数第k个节点
两种方法 1.在链表的初始化数据中加入 num 数据, 每添加一个节点,num加1,每删除一个节点,num减1 查找倒数第k个元素,即 指向第一个节点的指针向后移动 num - k 步. 2.使用两个 ...
- Day7 - E - Strange Way to Express Integers POJ - 2891
Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative ...
- delphi关闭和禁用Windows服务
function StopServices(const SvrName: string): Boolean; var SCH, SvcSCH: SC_HANDLE; SS: TServiceStatu ...
- python scipy优化器模块(optimize)
pyhton数据处理与分析之scipy优化器及不同函数求根 1.Scipy的优化器模块optimize可以用来求取不同函数在多个约束条件下的最优化问题,也可以用来求取函数在某一点附近的根和对应的函数值 ...