CSTL使用函数对象操作容器元素的foreach和transform的区别
for_each() 和transform()都可以使用一个函数指针或函数对象对一个容器区间的元素施加操作。Algorithm for_each applies a general function to every element in a certain range.
Algorithm transform applies a general function to every element in a certain range and replaces each element with the result of function.
for_each()使用的参数都是input iterator,只是对容器施加读操作,并不更改容器。
transform()使用了更多的参数,可以将结果存储到另一容器(或容器自身)中。
transform()还可以操作两个容器之间的运算,并将结果存储到另一容器(或容器自身)中。1 std::for_eachtemplate Function for_each (InputIterator first, InputIterator last, Function fn);
Apply function to range
Applies function fn to each of the elements in the range [first,last).
The behavior of this template function is equivalent to: template Function for_each(InputIterator first, InputIterator last, Function fn) { while(first!=last) { fn (*first); ++first; } return fn; // or, since C++11: return move(fn); }
demo: #include // std::cout #include // std::for_each #include // std::vector void myfunction (int i) { // function: std::cout << " " << i; } struct myclass { // function object type: void operator() (int i) {std::cout << " " << i;} } myobject; int main () { std::vector myvector; myvector.push_back(10); myvector.push_back(20); myvector.push_back(30); std::cout << "myvector contains:"; for_each (myvector.begin(), myvector.end(), myfunction); std::cout << " "; // or: std::cout << "myvector contains:"; for_each (myvector.begin(), myvector.end(), myobject); std::cout << " "; return 0; } /* output: Output: myvector contains: 10 20 30 myvector contains: 10 20 30 */2 std::transformunary operation(1) template OutputIterator transform (InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperation op); binary operation(2) template OutputIterator transform (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputIterator result, BinaryOperation binary_op);
Transform range
Applies an operation sequentially to the elements of one (1) or two (2) ranges and stores the result in the range that begins at result.
(1) unary operation Applies op to each of the elements in the range [first1,last1) and stores the value returned by each operation in the range that begins at result.
(2) binary operation Calls binary_op using each of the elements in the range [first1,last1) as first argument, and the respective argument in the range that begins at first2 as second argument. The value returned by each call is stored in the range that begins at result.
The behavior of this function template is equivalent to: template OutputIterator transform (InputIterator first1, InputIterator last1, OutputIterator result, UnaryOperator op) { while(first1 != last1) { *result = op(*first1); // or: *result=binary_op(*first1,*first2++); ++result; ++first1; } return result; }
demo:#include // std::cout #include // std::transform #include // std::vector #include // std::plus int op_increase (int i) { return ++i; } int main () { std::vector foo; std::vector 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()); // foo: 21 41 61 81 101 std::cout << "foo contains:"; for(std::vector::iterator it=foo.begin(); it!=foo.end(); ++it) std::cout << " " << *it; std::cout << " "; return 0; } /*Output: foo contains: 21 41 61 81 101 */
ref:
http://www.cplusplus.com/reference/algorithm/for_each/
http://www.cplusplus.com/reference/algorithm/transform/
如何看待遗体被切片的数字人?之前我们采访过中国数字人之父钟世镇,木想到当时做的这么冷门的笔记今天竟然会用上,喔呵呵呵所谓数字人非常神奇,可能很多人都不知道所谓数字人什么鬼。简单来说,我们将一个生前健康的人的遗
小米对第一批手机用户返钱合理合法吗?这个应该是小米公司的一种营销手段,小米向来都是很会做营销,也很会制造热度,既然小米公司敢公开这种返钱说明肯定是合法的,小米也是一个大公司,世界500强的公司,不至于这么明目张胆的非
小米手机十周岁生日临近首批用户已领走超2亿红包8月10日晚,2021雷军年度演讲暨小米秋季新品发布会上,小米集团创始人董事长兼CEO雷军宣布小米将拿出3。7亿元回馈首批购买过第一代小米手机的初代米粉。雷军表示,小米从一家只有1
华为Mate50最新消息曝光,没有自研5G芯片,大家还支持吗?华为Mate50其实现在大家都不清楚什么情况,之前有消息说今年华为Mate50系列不会发布了,要到明年才会发布,而最近的消息称华为Mate50系列,到时候会用上高通骁龙898处理器
华为优质服务击退加拿大北电北电网络是加拿大有名的通信设备制造商。它生产的大型排队机(寻呼台所用的大型呼叫设备)的市场占有率世界第一,它的产品技术成熟,性能稳定,多年来盘踞在中国市场上,稳如泰山。1997年,
如何为老年性耳聋者选配助听器?有很多老年人随便买一个助听器,佩戴后出现一些不好效果,反而对助听器有排斥心理。选配助听器是一种非常严肃问题,必须在专业听力师指导下进行。需要做详细检查如耳镜检查听力检查言语识别率测
机顶盒装哪个软件看电视直播不卡,直播源也稳定?电视直播不卡,直播源稳定的直播软件其实是有很多的。但很多网友却还是一直在推荐电视家,HDP等这些早已烂大街的直播软件,看电视直播节目不卡顿才怪呢?那么到底有哪些直播软件称的上真正的
首发价16999元,小米第二代大师电视明日开售IT之家8月15日消息小米本周发布了第二代大师电视,搭载77英寸120HzOLED屏,将于明日(8月16日)1000正式开售,零售价19999元,首发价16999元。IT之家了解到
大家有什么好的国产数字转盘播放器推荐?从开始折腾PC做音源到现在也有七八年了,最终结论是PC变量因素太多很难控制,彻底放弃用PC当音源了,后面入了仙籁M1T纯数字转盘,仙籁本身就专注于数字音乐解决方案,在HiFi圈子里
一加数字系列手机发展史1。一加12。一加23。一加X4。一加35。一加3T6。一加57。一加5T8。一加69。一加6T10。一加6T迈凯伦定制版11。一加712。一加7Pro13。一加7T14。一加7T
诺基亚亮剑,6100mAh4K屏100倍变焦,实力堪比国产诺基亚可是陪伴80后甚至90后青春的老品牌,它曾经也是全球第一大手机厂商,也是有着辉煌时刻的。但随着苹果三星等智能手机的到来,一个称霸全球手机市场15年的王者就开始走向没落了,它的