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. 关闭Hyper-V后,天翼校园宽带(Netkeeper)依旧显示Sorry, this application cannot run under a Virtual Machin的解决方法

    环境: win10专业版,版本1909 经过: 尝试了一下win10 更新后的沙盒系统,当时开启了沙盒,但是未打开Hyper-V,沙盒正常运行. 第二次开机后天翼校园宽带(Netkeeper)显示So ...

  2. 多元线性回归算法的python底层代码编写实现

    1.对于多元线性回归算法,它对于数据集具有较好的可解释性,我们可以对比不过特征参数的输出系数的大小来判断它对数据的影响权重,进而对其中隐含的参数进行扩展和收集,提高整体训练数据的准确性. 2.多元回归 ...

  3. swoole之建立 http server

    一.代码部分 <?php /** * 传统:nginx <-> php-fpm(fast-cgi process manager) <-> php * swoole:ht ...

  4. LVS负载均衡基本原理

    负载均衡基本原理与lvs 基本介绍 1.1 负载均衡的由来 在业务初期,我们一般会先使用单台服务器对外提供服务.随着业务流量越来越大,单台服务器无论如何优化,无论采用多好的硬件,总会有性能天花板,当单 ...

  5. 实验吧——Recursive

    环境:win10,kali虚拟机 工具:ida 虚拟机打开看看,发现是ELF文件,运行一下,额没有什么发现. Ida打开看看,发现是在文件内部运行python解释器,百度搜索了一个基本上可以找到Py_ ...

  6. springCloud 之 Eureka注册中心高可用配置

    springCloud的eureka高可用配置方案思路是:几个服务中心之间相互注册,比如两个注册中心,A注册到B上,B注册到A上,如果是三个注册中心则是:A注册到BC上,B注册到AC上,C注册到AB上 ...

  7. oracle,uuid为主键,插入时直接更新id

    uuid为主键,插入时自动更新 -- Create table create table TECHNOLOGYCOMPANY ( ID VARCHAR2(32) default SYS_GUID() ...

  8. Web基础之Spring IoC

    Spring之IoC 概念   IoC:Inversion of Control,中文通常翻译为"控制反转",它还有一个别名叫做依赖注入(Dependency Injection) ...

  9. 获取指定进程号,并kill掉

    直接上案例: 例子:获取nginx进程 方法:$ps -aux |grep nginx |grep -v grep |awk '{print $2}'  或者 $ps -ef |grep nginx ...

  10. ES6 之 Proxy

    概述 Proxy 用于修改某些操作的默认行为,等同于在语言层面做出修改. Proxy 可以理解在目标对象架设一个“拦截”层外界对该对象的访问都必须先通过这层拦截,因此提供了一种机制可以对外界的访问进行 ...