Demo00

std::transform在指定的范围内应用于给定的操作,并将结果存储在指定的另一个范围内。要使用std::transform函数需要包含头文件。

以下是std::transform的两个声明,一个是对应于一元操作,一个是对应于二元操作:

template <class InputIterator, class OutputIterator, class UnaryOperation>

OutputIterator transform (InputIterator first1, InputIterator last1,

OutputIterator result, UnaryOperation op);

template <class InputIterator1, class InputIterator2,

class OutputIterator, class BinaryOperation>

OutputIterator transform (InputIterator1 first1, InputIterator1 last1,

InputIterator2 first2, OutputIterator result,

BinaryOperation binary_op);

对于一元操作,将op应用于[first1, last1)范围内的每个元素,并将每个操作返回的值存储在以result开头的范围内。给定的op将被连续调用last1-first1次。op可以是函数指针或函数对象或lambda表达式。

如op的一个实现 即将[first1, last1)范围内的每个元素加5,然后依次存储到result中。

int op_increase(int i) {return (i + 5)};

调用std::transform的方式如下:

std::transform(first1, last1, result, op_increase);

对于二元操作,使用[first1, last1)范围内的每个元素作为第一个参数调用binary_op,并以first2开头的范围内的每个元素作为第二个参数调用binary_op,每次调用返回的值都存储在以result开头的范围内。给定的binary_op将被连续调用last1-first1次。binary_op可以是函数指针或函数对象或lambda表达式。

如binary_op的一个实现即将first1和first2开头的范围内的每个元素相加,然后依次存储到result中。

int op_add(int, a, int b) {return (a + b)};

调用std::transform的方式如下:

std::transform(first1, last1, first2, result, op_add);
std::transform支持in place,即result和first1指向的位置可以是相同的。std::transform的主要作用应该就是省去了我们自己写for循环实现。

以下是摘自对std::transform的英文解释:

/*
// reference: http://en.cppreference.com/w/cpp/algorithm/transform
template< class InputIt, class OutputIt, class UnaryOperation >
OutputIt transform( InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation unary_op )
{
while (first1 != last1) {
*d_first++ = binary_op(*first1++, *first2++);
}
return d_first;
}
template< class InputIt1, class InputIt2, class OutputIt, class BinaryOperation >
OutputIt transform( InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOperation binary_op );
std::transform applies the given function to a range and stores the result in another range, beginning at d_first.
(1): The unary operation unary_op is applied to the range defined by [first1, last1).
(2): The binary operation binary_op is applied to pairs of elements from two ranges:
one defined by [first1, last1) and the other beginning at first2.
Parameters:
first1, last1: the first range of elements to transform
first2: the beginning of the second range of elements to transform
d_first:the beginning of the destination range, may be equal to first1 or first2
unary_op: unary operation function object that will be applied.
binary_op: binary operation function object that will be applied.
Return value: Output iterator to the element past the last element transformed.
std::for_each: ignores the return value of the function and guarantees order of execution.
std::transform: assigns the return value to the iterator, and does not guarantee the order of execution.
*/

以下是std::transform用法举例:

#include "transform.hpp"
#include <algorithm> // std::transform
#include <string>
#include <cctype> // std::toupper
#include <iostream>
#include <vector>
#include <functional> // std::plus c++14 int test_transform1()
{
std::string s("Hello");
std::transform(s.begin(), s.end(), s.begin(),
[](unsigned char c) { return std::toupper(c); });
std::cout << s << std::endl; // HELLO std::transform(s.begin(), s.end(), s.begin(), ::tolower);
std::cout << s << std::endl; // hello ////////////////////////////////
std::vector<int> arr{ 1, 3, 5 };
std::vector<int> arr2{ 1, 3, 5 };
std::vector<int> arr3{ 1, 3, 5 }; std::transform(arr.begin(), arr.end(), arr.begin(),
[](int d) -> int {return d * 5; }); // for_each
for (auto value : arr) {
std::cout << value << " "; // 5 15 25
}
std::cout<<std::endl; std::for_each(arr2.begin(), arr2.end(), [](int& a) {a *= 5; });
for (auto value : arr2) {
std::cout << value << " "; // 5 15 25
}
std::cout << std::endl; for (auto& value : arr3) {
value *= 5;
}
for (auto value : arr3) {
std::cout << value << " "; // 5 15 25
}
std::cout << std::endl; std::vector<std::string> names = { "hi", "test", "foo" };
std::vector<std::size_t> name_sizes; ///////////////////////////
std::transform(names.begin(), names.end(), std::back_inserter(name_sizes),
[](std::string name) { return name.size(); });
for (auto value : name_sizes) {
std::cout << value << " "; // 2 4 3
}
std::cout << std::endl; std::for_each(name_sizes.begin(), name_sizes.end(), [](std::size_t name_size) {
std::cout << name_size << " "; // 2 4 3
});
std::cout << std::endl; return 0;
} /////////////////////////////////////////////////////////
// reference: http://www.cplusplus.com/reference/algorithm/transform/
static int op_increase(int i) { return ++i; } int test_transform2()
{
std::vector<int> foo;
std::vector<int> bar; // set some values:
for (int i = 1; i<6; i++)
foo.push_back(i * 10); // foo: 10 20 30 40 50 bar.resize(foo.size()); // allocate space std::transform(foo.begin(), foo.end(), bar.begin(), op_increase);
// bar: 11 21 31 41 51 // std::plus adds together its two arguments:
std::transform(foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>());
// foo: 21 41 61 81 101 std::cout << "foo contains:";
for (std::vector<int>::iterator it = foo.begin(); it != foo.end(); ++it)
std::cout << ' ' << *it; // 21 41 61 81 101
std::cout << '\n'; return 0;
}

GitHub:https://github.com/fengbingchun/Messy_Test

Demo00的更多相关文章

  1. jBox使用方法

    1.引入jquery文件 2.引入css和jBox文件 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml& ...

  2. 《Java从入门到精通》src9-25

    find . -name *.java |xargs  -i sh -c "echo {};cat {}" > ../all.java[op@TIM src]$ cat al ...

  3. java基础-day31

    第08天 JDBC 今日内容介绍 u JDBC的概述及入门案例 u JDBC的API详解 u JDBC预处理对象 第1章   JDBC的概述及入门案例 1.1  JDBC概述和原理 1.1.1 JDB ...

  4. synchronized的四种用法

    一 修饰方法  Synchronized修饰一个方法很简单,就是在方法的前面加synchronized,synchronized修饰方法和修饰一个代码块类似,只是作用范围不一样,修饰代码块是大括号括起 ...

  5. css-知识总结

    是什么 CSS通常称为CSS样式或层叠样式表,主要用于设置HTML页面中的文本内容(字体,大小,对其方式等),图片的外形 (高宽.边框样式.边距等)以及版面的布局等外观显示样式. CSS可以是HTML ...

  6. css display:table圣杯布局

    圣杯布局指的是一个网页由页眉,3等高列(2个固定侧栏和中心内容主体)和贴在页面底部的页脚组成. 主要思路是对整个容器使用地上diaplay:table 的css规则,然后分别对页眉页脚使用displa ...

  7. css浮动布局小技巧

    父元素如何围住浮动的子元素的三种办法: 一.为父元素应用overflow:hidden. overflow真正用途是防止包含元素被大的内容撑开,设定了宽度之后,包含元素将超过容器的内容减掉:而它还有另 ...

  8. java synchronized的四种用法

    一 修饰方法 Synchronized修饰一个方法很简单,就是在方法的前面加synchronized,synchronized修饰方法和修饰一个代码块类似,只是作用范围不一样,修饰代码块是大括号括起来 ...

  9. java中接口的简单运用&java中的一些异常(运用myeclipse)

    package test;//创建一个名为test的包 public class A4paper implements Paper { public String getSize(){ return& ...

随机推荐

  1. go中的关键字-select

    1. select的使用 定义:在golang里头select的功能与epoll(nginx)/poll/select的功能类似,都是坚挺IO操作,当IO操作发生的时候,触发相应的动作. 1.1 一些 ...

  2. nyoj 16-矩形嵌套(贪心 + 动态规划DP)

    16-矩形嵌套 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:13 submit:28 题目描述: 有n个矩形,每个矩形可以用a,b来描述,表示长和 ...

  3. hdu 1024 Max Sum Plus Plus (动态规划)

    Max Sum Plus PlusTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. API网关在API安全性中的作用

    从单一应用程序切换到微服务时,客户端的行为不能与客户端具有该应用程序的一个入口点的行为相同.简单来说就是微服务上的某一部分功能与单独实现该应用程序时存在不同. 目前在使用微服务时,客户端必须处理微服务 ...

  5. react-router重定向

    ① ②通过this.props重定向

  6. 【Oracle】Oracle数据库基本指标查看

    目录 1.查看表空间 2.查看用户 3.查看数据库内存 4.查看数据库版本 5.oracle归档情况 6.查看redo log日志位置 7.查看数据库的控制文件 8.查看RMAN的备份情况 9.FRA ...

  7. Stream系列(一)Filter方法使用

    Filter 是过滤器,也可以当查询方法使用 EmployeeTestCase.java package com.example.demo; import lombok.extern.log4j.Lo ...

  8. .NET Core 3.0之深入源码理解HealthCheck(一)

    写在前面 我们的系统可能因为正在部署.服务异常终止或者其他问题导致系统处于非健康状态,这个时候我们需要知道系统的健康状况,而健康检查可以帮助我们快速确定系统是否处于正常状态.一般情况下,我们会提供公开 ...

  9. 聚类-K-Means

    1.什么是K-Means? K均值算法聚类 关键词:K个种子,均值聚类的概念:一种无监督的学习,事先不知道类别,自动将相似的对象归到同一个簇中 K-Means算法是一种聚类分析(cluster ana ...

  10. zabbix企业微信报警实现

    企业微信配置 # 注册 企业微信注册地址:https://work.weixin.qq.com 笔者注册的企业微信名称为 5iik # 配置 # 在主干5iik(企业名称)下添加子部门(监控组),并将 ...