震惊!std::string operator + 的复杂度居然是……

刚刚在打洛谷月赛,B 卡了很久莫名 TLE.

读了 C++ Reference 发现,operator += 的复杂度是这样的

Unspecified, but generally up to linear in the new string length.

什么!居然是长度!

包括 push_back() 的复杂度也居然是

Unspecified; Generally amortized constant, but up to linear in the new string length.

未指定,通常是常数 最坏是线性
什么鬼!

在那题中,因为添加的所有东西都一样,所以可以用 std::string::append,这玩意儿的复杂度也是线性。

所以遇到什么对复杂度有要求的还是去用 char* 吧!

posted @ 2021-08-13 15:51  Acfboy  阅读(314)  评论(0编辑  收藏  举报