设为首页 加入收藏

TOP

使用 C++ 的 StringBuilder 提升 4350% 的性能(二)
2014-11-24 02:50:49 来源: 作者: 【 】 浏览:4
Tags:使用 StringBuilder 提升 4350% 性能
delim.empty()) {
return ToString();
}
string_t result;
if (m_Data.empty()) {
return result;
}
// Hope we don't overflow the size type.
size_type st = (delim.size() * (m_Data.size() - 1)) + m_totalSize + 1;
result.reserve(st);
// If you need reasons to love C++11, here is one.
struct adder {
string_t m_Joiner;
adder(const string_t &s): m_Joiner(s) {
// This constructor is NOT empty.
}
// This functor runs under accumulate() without reallocations, if 'l' has reserved enough memory.
string_t operator()(string_t &l, const string_t &r) {
l += m_Joiner;
l += r;
return l;
}
} adr(delim);
auto iter = m_Data.begin();
// Skip the delimiter before the first element in the container.
result += *iter;
return std::accumulate(++iter, m_Data.end(), result, adr);
}


}; // class StringBuilder


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C/C++中char* 与char []定义的区别 下一篇Linux-0.11抠代码-bootsect

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: