C++/C++11中std transform的使用

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

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

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

template <class InputIterator, class OutputIterator, class UnaryOperationOutputIterator transform (InputIterator first1, InputIterator last1,                            OutputIterator result, UnaryOperation op); template <class InputIterator1, class InputIterator2,          class OutputIterator, class BinaryOperationOutputIterator transform (InputIterator1 first1, InputIterator1 last1,                            InputIterator2 first2, OutputIterator result,                            BinaryOperation binary_op);

对于一元操作,将op应用于[first1, last1]范围内的每个元素,并将每个操作返回的值存储在以result开头的范围内。给定的op将被连续调用last1-first1+1次。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+1次。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/transformtemplate< 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++14int 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::endlstd::for_each(arr2.begin(), arr2.end(), [](int& a) {a *= 5; }); for (auto value : arr2) {  std::cout << value << "    "; // 5 15 25 } std::cout << std::endlfor (auto& value : arr3) {  value *= 5; } for (auto value : arr3) {  std::cout << value << "    "; // 5 15 25 } std::cout << std::endlstd::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::endlstd::for_each(name_sizes.begin(), name_sizes.end(), [](std::size_t name_size) {  std::cout << name_size << "    "; // 2 4 3 }); std::cout << std::endlreturn 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;}


GitHubhttps://github.com/fengbingchun/Messy_Test

 

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
内容概要:本文围绕可变桨叶四旋翼无人机的规范控制与点对点运动模拟展开,重点研究优化推力分配策略在翻转动作中的应用与性能比较。通过Matlab代码实现,构建了四旋翼动力学模型,并设计了多种控制算法以实现精确的姿态调整与轨迹跟踪。研究对比了不同推力分配方案在执行高机动性翻转动作时的稳定性、能耗效率与响应速度,旨在提升无人机在复杂飞行任务中的动态性能与控制精度。该仿真研究为无人机飞控系统的设计与优化提供了理论依据和技术支持。; 适合人群:具备一定自动控制理论基础和Matlab编程能力,从事无人机控制、飞行器动力学或机器人系统研究的科研人员及研究生。; 使用场景及目标:① 实现四旋翼无人机在三维空间中的精确点对点运动控制;② 对比分析不同推力分配策略在执行翻转等高难度动作时的控制效果与能耗表现,优化飞行性能;③ 为无人机自主飞行、特技飞行及复杂环境下的机动控制提供算法验证平台。; 阅读建议:此资源以Matlab仿真为核心,建议读者结合相关控制理论知识,深入理解代码实现细节,重点关注动力学建模、控制律设计与推力分配模块。在学习过程中,应动手调试参数,复现文中翻转动作的仿真结果,并尝试拓展至其他复杂飞行任务,以加深对无人机控制机理的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值