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 ...
随机推荐
- 1-6SpringBoot之事务管理@Transactional
以前学ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作: 用来保证一致性,即service方法里的多个dao操作,要 ...
- 如何解决 Django 前后端分离开发的跨域问题
一.同源策略 1.先来说说什么是源 • 源(origin)就是协议.域名和端口号. 以上url中的源就是:http://www.company.com:80 若地址里面的协议.域名和端口号均相同则属于 ...
- CCF 201703-4 地铁修建(最小生成树)
题意:A市有n个交通枢纽,其中1号和n号非常重要,为了加强运输能力,A市决定在1号到n号枢纽间修建一条地铁.地铁由很多段隧道组成,每段隧道连接两个交通枢纽.经过勘探,有m段隧道作为候选,两个交通枢纽之 ...
- ROS学习笔记3-基础课程之文件系统向导
准备工作需要使用如下命令安装ros的教程: $ sudo apt-get install ros-<distro>-ros-tutorials 其中,distro为所用ros的发行版本,该 ...
- css 让多出的文字成省略号...
一,单行 white-space:nowrap; overflow:hidden;text-overflow: ellipsis; 二,多行 display: -webkit-box; overflo ...
- R 误差自相关与DW检验
R语言进行DW检验: library(lmtest) dw = dwtest(fm1) > dw Durbin-Watson test data: fm1 DW = 2.4994, p-valu ...
- Day7 - G - Divisors POJ - 2992
Your task in this problem is to determine the number of divisors of Cnk. Just for fun -- or do you n ...
- 基于AQS自己实现一个同步器
前面说了这个多,我们可以自己尝试实现一个同步器,我们可以简单的参考一下ReentrantLock这个类的实现方式,我们就简单的实现一个不可重入的独占锁吧! 一.简单分析ReentrantLock的结构 ...
- 002. 使用IDEA创建MyBatis的JAVAWEB项目 ,每一步都有详细过程,完美绕过各种坑能正常运行
001. 我们新建一个Module,相当于一个工程里面的一个项目 002.选择空白的JAVA程序 003.输入项目的名字为mybatis001 004.我们对这个项目添加Support,各种框架依赖 ...
- JAVA DateUtil 工具类封装(转)
原文链接 https://blog.csdn.net/wangpeng047/article/details/8295623 作者三次整理后的代码 下载链接 https://www.lanzou ...