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 - Obejct
关于Object类(Java 10)中的方法,根据其所涉及的知识点,分为如下4个部分: 基础 clone: protected Object clone() throws CloneNotSuppor ...
- Lesson 47 The great escape
What is one of the features of modern camping where nationality is concerned? Economy is one powerfu ...
- webpack 4 x使用详细
1.首先安装node.js 2.打开控制台cmd,输入npm install webpack webpack-cli webpack-dev-server -g 3.在本地磁盘上建一个文件夹,然后通过 ...
- 文本情感分析(二):基于word2vec、glove和fasttext词向量的文本表示
上一篇博客用词袋模型,包括词频矩阵.Tf-Idf矩阵.LSA和n-gram构造文本特征,做了Kaggle上的电影评论情感分类题. 这篇博客还是关于文本特征工程的,用词嵌入的方法来构造文本特征,也就是用 ...
- vue table已选列数据
vue Table@on-selection-change="test" 已选中项数据 test(selection){} <Table :data="tableD ...
- LOJ #10002. 喷水装置
题目 裸的贪心. 基本思想见图: Code: #include<iostream> #include<cstdio> #include<cstring> #incl ...
- Metasploit学习笔记——网络服务渗透攻击
1.内存攻防技术 1.1缓冲区溢出漏洞机理 1.2栈溢出利用机理 1.3缓冲区溢出利用的限制条件 2.网络服务渗透攻击面 3. Windows服务渗透攻击实战案例——MS08-067安全漏洞 示例代码 ...
- Javascript获取当前鼠标在元素内的坐标定位
代码如下: <!doctype html> <html> <head> <meta charset="utf-8"> <tit ...
- 自己写个tween
public Vector3 begin,end;//起始终止坐标 public float BtoE_time;//用时 float timer,lerp;//计时器和进度值 void Update ...
- Flume 1.9.0 的安装(比较简单, 操作也不像老版本那么繁琐了)
之前已经完成了Hadoop集群.Hbase集群.Hive的搭建, 这次来安装一下flume-1.9.0 安装过程 将tar包上传并解压到指定目录, 并修改名称 tar -zxvf apache-flu ...