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. Day3-C-Radar Installation POJ1328

    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...

  2. 前端学习笔记系列一:4 vue中@click.native

    .native - listen for a native event on the root element of component. 作用:[给组件绑定原生事件] 例子:如果使用router-l ...

  3. myeclipse中svn图标状态不显示问题的解决办法

    myeclipse中svn图标状态不显示问题的解决办法 博客分类: svn SVNMyeclipse工作WindowsC  myeclipse中使用 svn 插件,原本正常,未作任何更改,突然有一天, ...

  4. Deep Image Retrieval: Learning global representations for image search In ECCV, 2016学习笔记

    - 论文地址:https://arxiv.org/abs/1604.01325 contribution is twofold: (i) we leverage a ranking framework ...

  5. zabbix 监控linux tcp连接数

    zabbix 监控linux tcp连接数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.TCP的状态概述 1>.端口状态转换 2>.TCP 三次握手 3>. ...

  6. 解决NLPIR汉语分词系统init failed问题

    今天第一次使用NLPIR汉语分词系统. 遇到的问题: 当点击时, 出现以下界面 看了博客https://blog.csdn.net/yuyanyanyanyanyu/article/details/5 ...

  7. DStream-05 updateStateByKey函数的原理和源码

    Demo updateState 可以到达将每次 word count 计算的结果进行累加. object SocketDstream { def main(args: Array[String]): ...

  8. Nature

    1.主干 (1)<河图+洛书>:启发伏羲作八卦. (2)<三坟+五典>:失传:伏羲.神农.轩辕.少昊.颛顼.帝喾.唐尧.虞舜. (3)<八索+九丘>:失传:八卦之书 ...

  9. Linux每日练习-复习紧急救援模式下重改root权限密码 20200225

  10. linux下nginx的安装和配置

    准备目录 [root@sqh ~]# mkdir /usr/local/nginx [root@sqh ~]# cd /usr/local/nginx 添加一些支持和依赖 1.安装gcc 安装redi ...