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 ...
随机推荐
- [7b2美化]柒比贰 魔改系列|7B2-分类封面添加波浪效果&每日诗词
本文转载自:钻芒博客 https://www.zmki.cn/5105.html 效果如图: 代码: 首先在style.css样式表里添加波浪样式 /*浪来了*/ .lang { overflow: ...
- NPOI,导出Execl,压缩文件zip,发送Email
private void SendEmail(string emailAddress, string companyName,string proxy, string officer, DataTab ...
- SciPy 信号处理
章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...
- 计算机网络中OSI参考模型
OSI参考模型 应用层 包括所有能产生网络流量的程序 DNS属于这一层 表示层 用来判断传输之前是否进行加密或压缩处理(二进制.ASCII) 比如出现乱码情况,可能就是表示层的问题 会话层 一个浏览器 ...
- docker-compose 修改zabbix images 添加微信报警插件 时间同步 中文乱码 添加grafana美化zabbix
我们先来看一下我们要修改得 zabbix.yaml github https://github.com/bboysoulcn/awesome-dockercompose ve ...
- 在线关闭 CLOSE_WAIT状态TCP连接
1.查看某个端口的所有TCP连接: [root@Centos projects]# netstat -anp | tcp6 ::: :::* LISTEN /java tcp6 CLOSE_WAIT ...
- 洛谷P1351 联合权值
\(\Large\textbf{Description:}\) \(\large一棵树,父子之间距离为1,求距离为2的两点点权之积的最大值与和.\) \(\Large\textbf{Solution: ...
- 如何创建一个SpringBoot多模块项目
创建主模块rail-plate-line 1.点击Create New Project --> 选择Spring Initializr -- > 选择本地jdk 2.Group为com ...
- Vulkan SDK Demo 之一 熟悉
DiligentEngine的API是D3d11和D3D12风格的,vulkan也被封装成了这种风格的API. 在了解Diligent Engine是如何对vulkan进行封装之前,我准备先学习下Vu ...
- UCENTER同步登录工作原理和配置要点
ucenter的同步登录原理: 1)Ucenter是和uc_client同步的.每个PHP应用,加入了UCENTER后,都会在主目录下有个UC_CLIENT目录.这个目录里,都有一个client.PH ...