C++ STD Gems03
transform、for_each
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <cctype>
#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::string a = "heLlo, WorRld";
std::string b;
write_to_cout(a);
std::cout << std::endl;
write_to_cout(b);
std::cout << std::endl;
//test algorithm
std::transform(a.begin(), a.end(), std::back_inserter(b), [](char c) {return std::toupper(c);}); // 将字符串a所有字母大写插入b末尾
write_to_cout(a);
std::cout << std::endl;
write_to_cout(b);
std::cout << std::endl <<std::endl;
}
// 二元谓词
void test1()
{
std::vector<int> a = {1, 2, 3, 4, 5, 6, 7};
std::vector<int> b = {11, 12, 13, 14, 15, 16, 17};
std::vector<int> c;
write_to_cout(a);
std::cout << std::endl;
write_to_cout(b);
std::cout << std::endl;
// test algorithm
// a中每个元素乘以2加上b中每个元素得到值存入c中
transform(a.begin(), a.end(), b.begin(), std::back_inserter(c), [](int x, int y){return 2 * x + y;} );
write_to_cout(c);
std::cout << std::endl << std::endl;
}
void test2()
{
std::vector<int> a = {1, 12, 31, 54, 15, 6, 27};
std::vector<int> b = {11, 12, 13, 14, 15, 16, 17};
write_to_cout(a);
std::cout << std::endl;
write_to_cout(b);
std::cout << std::endl;
//test algotirhm
std::transform(a.begin(), a.end(), b.begin(), b.begin(), [](int a, int b){return a > b ? a : b;} );
write_to_cout(b);
std::cout << std::endl << std::endl;
}
void test3()
{
std::vector<int> a = {1, 12, 31, 54, 15, 6, 27};
write_to_cout(a);
std::cout << std::endl;
// test algorithm
//将a中元素都乘以2
std::for_each(a.begin(), a.end(), [](int& x) {return x = 2*x;});
write_to_cout(a);
std::cout << std::endl << std::endl;
}
int main()
{
test0();
test1();
test2();
test3();
return 0;
}
C++ STD Gems03的更多相关文章
- 【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 ...
随机推荐
- Day3-L-Cup HDU2289
The WHU ACM Team has a big cup, with which every member drinks water. Now, we know the volume of the ...
- 085、Java数组之实现排序
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- Maven 使用Nexus搭建Maven私服
Maven学习 (四) 使用Nexus搭建Maven私服 为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找 ...
- Jenkins + git + maven 安装
1.jenkins安装 sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo ...
- vue实现登陆单页面
一 实现页面的布局 1. 首先在components里建一个login.vue <template> <div class=login_container> 登陆组件 < ...
- 今日份学习:初步的springboot
今日记录 今日份BUG清单 flyway的sql文件有两个下划线 __ , _ 是不可以的. 高版本的freemarker默认的扩展名变成了flth,而不是ftl 今日份用到的网址 1. freema ...
- 使用zabbix server监控tomcat实战案例
使用zabbix server监控tomcat实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 大家都知道,zabbix server效率高是使用C语言编写的,有很多应用程序 ...
- Linux下如何查找sqlnet.ora 和listener.ora 和tnsnames.ora 配置文件的目录
1.首先切换到oracle 用户下 使用env 查看数据库配置文件信息 2.然后找到LD_LIBRARY_PATH=/home/opt/oracle/product/11.2.0.4/db_1 (配置 ...
- 春运到了,带你用python来抢票回家!
不知不觉,一年一度的春运抢票大幕已经拉开,想快速抢到回家的车票吗?作为程序员,这些技术手段,你一定要知道. 为了让大家更快捷更便利的抢火车票,各种各样的抢票软件应需而生,这类软件大部分都是付费抢票的机 ...
- synchronized原理及优化,(自旋锁,锁消除,锁粗化,偏向锁,轻量级锁)
偏向锁:不占用CPU自旋锁:占用CPU.代码执行成本比较低且线程数少时,可以使用 .不经过OS.内核态,效率偏低 理解Java对象头与Monitor 在JVM中,对象在内存中的布局分为三块区域:对象头 ...