【发布时间】:2018-01-06 01:23:32
【问题描述】:
我在 C++ 中搜索了连接字符串的不同方法,我发现 std::stringstream 和 std::string 是可能的选项。但是,我找不到字符串流和字符串的串联操作的时间复杂度。 C ++中字符串连接的时间复杂度是多少。此外,在 C++ 中的 std::stringstream 和 std::string 中转换为字符串的时间复杂度是多少。是否可以在 C++ 中以 0(1) 时间附加字符串?感谢您的宝贵时间。
【问题讨论】:
-
“时间复杂度”具有相同的 O(N) 阶。但是将两个
std::string对象加在一起会比使用std::stringstream快很多。 -
想一想:你不能因为需要复制而去亚线性;它一次一个字符。您也不能使用超线性,因为在连接期间没有嵌套循环。因此,O(n+m) 是唯一剩下的。
-
std::stringstream 但是在转换为字符串方面要好得多,对吧?
-
将字符串转换为字符串?
-
不,如果我有一个 int,那么 std::stringstream 是否更适合将 int 转换为字符串?
标签: c++ string time-complexity stringstream stdstring