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 ...
随机推荐
- java创建线程方式
1.继承Thread类 public class ThreadCreator extends Thread{ public static void main(String[] args) { //第一 ...
- centos 虚拟机安装调试
service network restart reboot yum update -y cd /etccd sysconfigcd network-scripts[root@u0mo5 networ ...
- js取值问题----key为数字
今天远程调用一个接口在处理返回的数据的时候突然发现数组的Key是一个数字 然后如果继续用“.”的话是不会的,会报错 要用中括号就可以解决
- firewalld学习-zone
原文地址:http://www.excelib.com/article/290/show firewalld默认提供了九个zone配置文件: block.xml.dmz.xml.drop.xml.ex ...
- 微软重制Windows 1.0系统:祖师爷出山了
Windows官方推特在7月1日发布了一条很有趣的动态,“向大家介绍全新的Windows 1.0,带MS-DOS.时钟等”.配发的视频回顾了从Windows 1.0/3.1到Windows 10期间, ...
- idea中跑mapreduce报错, PATH设置错误
问题如题,报错: [root@node01 servers]# hadoop jar loginVisit.jar cn.itcast.loginVisit.step1.Step1Main19/07/ ...
- 编程题目:输入一个链表,输出该链表中倒数第k个节点
两种方法 1.在链表的初始化数据中加入 num 数据, 每添加一个节点,num加1,每删除一个节点,num减1 查找倒数第k个元素,即 指向第一个节点的指针向后移动 num - k 步. 2.使用两个 ...
- CentOS下安装Orcale
以前没有安装过,最近安装了.感觉在Liunx安装真的超麻烦.这是技术文档,分享给大家. LINUX安装oracle数据库步骤: 1.安装依赖包 yum -y install gcc gcc-c ...
- prepareBeanFactory方法源码跟踪
看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractApplic ...
- A convenient way to recognize and handwrite multidimensional arrays in Numpy
As a new learner of Numpy, it is very common to be confused by the form of array, braces nested in b ...