加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 有效积累

发布时间:2020-12-16 04:56:23 所属栏目:百科 来源:网络整理
导读:假设我有字符串向量,我想通过std :: accumulate连接它们. 如果我使用以下代码: std::vectorstd::string foo{"foo","bar"};string res=""; res=std::accumulate(foo.begin(),foo.end(),res,[](string rs,string arg){ return rs+arg; }); 我可以肯定会有临时
假设我有字符串向量,我想通过std :: accumulate连接它们.

如果我使用以下代码:

std::vector<std::string> foo{"foo","bar"};
string res=""; 
res=std::accumulate(foo.begin(),foo.end(),res,[](string &rs,string &arg){ return rs+arg; });

我可以肯定会有临时对象构造.

在this回答他们说std :: accumulate的效果是这样指定的:

Computes its result by initializing the accumulator acc with the
initial value init and then modifies it with acc = acc + *i or acc =
binary_op(acc,*i) for every iterator i in the range [first,last) in
order.

所以我想知道这样做的正确方法是什么,以避免不必要的临时对象构造.

一个想法是以这种方式改变lambda:

[](string &rs,string &arg){ rs+=arg; return rs; }

在这种情况下,我认为我强制有效串联字符串并帮助编译器(我知道我shouldn’t)省略了不必要的副本,因为这应该等同于(伪代码):

accum = [](& accum,& arg){ ...; return accum; }

因此

accum = & accum;

另一个想法是使用

accum = [](& accum,& arg){ ...; return std::move(accum); }

但这可能会导致类似于:

accum = std::move(& accum);

这看起来很可疑.

写这个的正确方法是什么,以尽量减少不必要的临时对象创建的风险?我不只是对std :: string感兴趣,我很乐意有一个解决方案,这可能适用于任何具有复制和移动构造函数/赋值的对象.

解决方法

请尝试以下方法
res=std::accumulate(foo.begin(),const string &arg) -> string & { return rs+=arg; });

在此通话之前可能会有一个电话

std::string::size_type n = std::accumulate( foo.begin(),std::string::size_type( 0 ),[] ( std::string_size_type n,const std::string &s ) { return ( n += s.size() ); } );

res.reserve( n );

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读