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的更多相关文章

  1. 【NX二次开发】NX内部函数,libuifw.dll文件中的内部函数

    本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: void U ...

  2. C++ std::set

    std::set template < class T, // set::key_type/value_type class Compare = less<T>, // set::k ...

  3. C++ std::priority_queue

    std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...

  4. C++ std::queue

    std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...

  5. C++ std::multimap

    std::multimap template < class Key, // multimap::key_type class T, // multimap::mapped_type class ...

  6. C++ std::map

    std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...

  7. C++ std::list

    std::list template < class T, class Alloc = allocator > class list; List Lists are sequence co ...

  8. C++ std::forward_list

    std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...

  9. C++ std::deque

    std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...

随机推荐

  1. JavaScript之this的用法

    本文我们介绍下js中this的用法. 由上图可得,默认this指向window,而在node.js中this默认指向global. 由上图可得: 1.原型链为o->MyClass.prototy ...

  2. 使用总结:java多线程总结 <转载>

    转载 https://www.cnblogs.com/rollenholt/archive/2011/08/28/2156357.html java多线程总结 2011-08-28 20:08  Ro ...

  3. 加拿大Assignment写作如何靠第一句话来吸睛?

    最近有留学加拿大的同学咨询我们如何写好assignment的首句,关于话题背景的引入,如何才能自然精妙,让老师对后文充满期待.小编用一句话总结今天的策略:陈述一个跟话题直接相关的事实.“直接相关”,保 ...

  4. redis.rpm 安装

    yum install jemalloc wget http://www6.atomicorp.com/channels/atomic/centos/6/x86_64/RPMS/redis-3.0.7 ...

  5. 序列化sys随笔补充

    sys是在和python解释器做交互 sys.path----模块查找的顺序 sys.path.append() sys.path.insert()sys.argv---只能在终端执行 sys.arg ...

  6. Python安全基础编写

    python所写的程序都是应用程序 python的环境编译型:一次性将所有程序编译成二进制文件缺点:开发效率低,不能跨平台优点:运行速度快使用语言:C,C++等等 解释型:当程序执行时,一行一行的解释 ...

  7. 第1节 IMPALA:7、impala的安装以及配置过程

    6.制作本地yum源 镜像源是centos当中下载相关软件的地址,我们可以通过制作我们自己的镜像源指定我们去哪里下载impala的rpm包,这里我们使用httpd这个软件来作为服务端,启动httpd的 ...

  8. webpack 命令 Module build failed (from ./node_modules/babel-loader/lib/index.js) 错误问题解决方案

    在项目中运行的时候出现报错,错误为Module build failed (from ./node_modules/babel-loader/lib/index.js) 解决方案: 控制台输入  np ...

  9. vDom和domDiff

    虚拟dom和domDiff 1. 构建虚拟DOM var tree = el('div', {'id': 'container'}, [ el('h1', {style: 'color: blue'} ...

  10. NO7 利用三剑客awk-grep-sed-head-tail等7种方法实践

    ·seq   sequence  #序列·sed   stream editor  #(三剑客老二)流编辑器.实现对文件的增删改替换查.        -n #取消默认输出.sed -n '20,30 ...