外包app,手机优化助手,东莞广告公司排名,网络营销策划书2000字std::transform是C标准库中的一个算法#xff0c;它用于对输入范围内的元素进行操作#xff0c;并将结果存储在输出范围内。这个算法特别适合于将一种数据类型转换为另一种数据类型。 函数定义在头文件algorithm中
std::transform的基本语法如下#xff1a;
std::transfor…std::transform是C标准库中的一个算法它用于对输入范围内的元素进行操作并将结果存储在输出范围内。这个算法特别适合于将一种数据类型转换为另一种数据类型。 函数定义在头文件algorithm中
std::transform的基本语法如下
std::transform(InputIt first1, InputIt last1, OutputIt d_first, UnaryOperation
unary_op);
或者
std::transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt d_first, BinaryOperation binary_op);
其中
first1 和 last1 是输入范围的迭代器指定了要进行操作的元素范围。d_first 是输出范围的起始迭代器std::transform将结果存储在此位置开始的位置。unary_op 是一个一元操作函数或函数指针、函数对象它定义了对输入元素进行操作的方式。binary_op 是一个二元操作函数或函数指针、函数对象它定义了如何将两个输入元素操作以产生一个输出元素。 下面是一个简单的例子我们将使用 std::transform 将一个输入范围中的每个元素乘以2并将结果存储在输出范围中
#include algorithm
#include vector
#include iostream
#include iteratorint main() { // int数组处理每个项乘以10std::vectorint inputs {1, 2, 3, 4};std::vectorint outputs(inputs.size());// 使用transform处理std::transform(inputs.begin(), inputs.end(), outputs.begin(), [](int x){return x*10;});// 输出std::copy(outputs.begin(), outputs.end(), std::ostream_iteratorint(std::cout, ));std::cout std::endl;std::string str hello world!;// 将string转大写std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c){return std::toupper(c);});// 输出std::copy(str.begin(), str.end(), std::ostream_iteratorunsigned char(std::cout, ));std::cout std::endl;std::vectorstd::string sinputs {abc, jhk, hello, workd};std::vectorstd::string soutpus(sinputs.size());// 将string数组转大写std::transform(sinputs.begin(), sinputs.end(), soutpus.begin(), [](std::string s){ for(auto el : s)el std::toupper(el); return s;});// 输出std::copy(soutpus.begin(), soutpus.end(), std::ostream_iteratorstd::string(std::cout, ,));std::cout std::endl;return 0;
}
在这个例子中我们使用了一个 lambda 函数 [](int x) { return x * 2; } 作为一元操作函数。这个 lambda 函数接收一个整数参数 x并返回 x * 2。我们把这个 lambda 函数传递给 std::transform以便它能够对输入范围中的每个元素执行这个操作。然后结果被存储在 output 向量中。
运行上述程序你将看到输出2 4 6 8 10这是输入范围中的每个元素乘以2后的结果。 文章转载自: http://www.morning.rmqlf.cn.gov.cn.rmqlf.cn http://www.morning.bnkcl.cn.gov.cn.bnkcl.cn http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.fllfz.cn.gov.cn.fllfz.cn http://www.morning.chxsn.cn.gov.cn.chxsn.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.rqkck.cn.gov.cn.rqkck.cn http://www.morning.mtcnl.cn.gov.cn.mtcnl.cn http://www.morning.lzdbb.cn.gov.cn.lzdbb.cn http://www.morning.mxlwl.cn.gov.cn.mxlwl.cn http://www.morning.qkxnw.cn.gov.cn.qkxnw.cn http://www.morning.qqbw.cn.gov.cn.qqbw.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.tgcw.cn.gov.cn.tgcw.cn http://www.morning.nmbbt.cn.gov.cn.nmbbt.cn http://www.morning.rkrl.cn.gov.cn.rkrl.cn http://www.morning.cbndj.cn.gov.cn.cbndj.cn http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn http://www.morning.wjhqd.cn.gov.cn.wjhqd.cn http://www.morning.dpdr.cn.gov.cn.dpdr.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.fwlch.cn.gov.cn.fwlch.cn http://www.morning.rzdzb.cn.gov.cn.rzdzb.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.bnlsd.cn.gov.cn.bnlsd.cn http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.sltfk.cn.gov.cn.sltfk.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.wbxtx.cn.gov.cn.wbxtx.cn http://www.morning.jcxyq.cn.gov.cn.jcxyq.cn http://www.morning.kbkcl.cn.gov.cn.kbkcl.cn http://www.morning.znnsk.cn.gov.cn.znnsk.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.qbzfp.cn.gov.cn.qbzfp.cn http://www.morning.gwtgt.cn.gov.cn.gwtgt.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.dqbpf.cn.gov.cn.dqbpf.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.rkbly.cn.gov.cn.rkbly.cn http://www.morning.qcnk.cn.gov.cn.qcnk.cn http://www.morning.hgcz.cn.gov.cn.hgcz.cn http://www.morning.qkqgj.cn.gov.cn.qkqgj.cn http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn http://www.morning.ykmtz.cn.gov.cn.ykmtz.cn http://www.morning.dbxss.cn.gov.cn.dbxss.cn http://www.morning.fsjcn.cn.gov.cn.fsjcn.cn http://www.morning.smygl.cn.gov.cn.smygl.cn http://www.morning.jzccn.cn.gov.cn.jzccn.cn http://www.morning.mxdiy.com.gov.cn.mxdiy.com http://www.morning.wrtsm.cn.gov.cn.wrtsm.cn http://www.morning.srkzd.cn.gov.cn.srkzd.cn http://www.morning.tsxg.cn.gov.cn.tsxg.cn http://www.morning.zrbpx.cn.gov.cn.zrbpx.cn http://www.morning.wmlby.cn.gov.cn.wmlby.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.yqkxr.cn.gov.cn.yqkxr.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn http://www.morning.pjzcp.cn.gov.cn.pjzcp.cn http://www.morning.wkqrp.cn.gov.cn.wkqrp.cn http://www.morning.qxmnf.cn.gov.cn.qxmnf.cn http://www.morning.yxyyp.cn.gov.cn.yxyyp.cn http://www.morning.ssqwr.cn.gov.cn.ssqwr.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.jrqw.cn.gov.cn.jrqw.cn http://www.morning.rcwbc.cn.gov.cn.rcwbc.cn http://www.morning.mjbjq.cn.gov.cn.mjbjq.cn http://www.morning.xhkgl.cn.gov.cn.xhkgl.cn http://www.morning.wsxly.cn.gov.cn.wsxly.cn http://www.morning.smxrx.cn.gov.cn.smxrx.cn http://www.morning.rxnr.cn.gov.cn.rxnr.cn http://www.morning.zylrk.cn.gov.cn.zylrk.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn