什么时候应该使用字符串而不是stringstream?

时间:2021-08-10 16:06:19

When should I use stringstream instead of string::append()? Supposing I'm going to catenate just strings.

什么时候应该使用stringstream而不是string :: append()?假设我将仅仅连接字符串。

stringstream ss;
ss << str1 << "str2" << ...
Write(ss.str());

Or:

string str;
str.reserve(10000);
str.append(str1);
str.append("str2");
...
Write(str);

Which of them is faster?

哪个更快?

2 个解决方案

#1


18  

I don't know which one will be faster, but if I had to guess I'd say your second example is, especially since you've called the reserve member function to allocate a large space for expansion.

我不知道哪一个会更快,但如果我不得不猜测我会说你的第二个例子,特别是因为你已经调用了预留成员函数来为扩展分配一个大空间。

If you're only concatenating strings use string::append (or string::operator+=).

如果你只是连接字符串,请使用string :: append(或string :: operator + =)。

If you're going to convert numbers to their string representation, as well as format them during conversion, and then append the conversion results together, use stringstreams. I mention the formatting part explicitly because if you do not require formatting C++11 offers std::to_string which can be used to convert numeric types to strings.

如果您要将数字转换为字符串表示形式,并在转换期间格式化它们,然后将转换结果附加在一起,请使用字符串流。我明确提到格式化部分,因为如果你不需要格式化,C ++ 11提供了std :: to_string,它可以用来将数字类型转换为字符串。

#2


14  

string.append is much faster. Especially when you reserve.

string.append要快得多。特别是当你保留。

If you are concatenating only strings, I would use string.append. I would only use stringstream when I need to automatically convert non-strings to strings for example:

如果你只连接字符串,我会使用string.append。我只需要在需要自动将非字符串转换为字符串时使用stringstream,例如:

const int x(42);
stringstream ss;
ss << "My favorite number is: " << x << std::endl;

Here stringstream automatically converts x to a string and appends it. I do not need to call atoi. Stringstream will convert all the basic types automatically for you. It is great for that purpose.

这里stringstream自动将x转换为字符串并附加它。我不需要打电话给atoi。 Stringstream将自动为您转换所有基本类型。这个目的很棒。

Also if you are only going to be directing data into the stringstream to convert it to a string later. You can use ostringstream which is for output.

此外,如果您只是将数据导入stringstream,以便稍后将其转换为字符串。您可以使用ostringstream作为输出。

I hope that helps.

我希望有所帮助。

#1


18  

I don't know which one will be faster, but if I had to guess I'd say your second example is, especially since you've called the reserve member function to allocate a large space for expansion.

我不知道哪一个会更快,但如果我不得不猜测我会说你的第二个例子,特别是因为你已经调用了预留成员函数来为扩展分配一个大空间。

If you're only concatenating strings use string::append (or string::operator+=).

如果你只是连接字符串,请使用string :: append(或string :: operator + =)。

If you're going to convert numbers to their string representation, as well as format them during conversion, and then append the conversion results together, use stringstreams. I mention the formatting part explicitly because if you do not require formatting C++11 offers std::to_string which can be used to convert numeric types to strings.

如果您要将数字转换为字符串表示形式,并在转换期间格式化它们,然后将转换结果附加在一起,请使用字符串流。我明确提到格式化部分,因为如果你不需要格式化,C ++ 11提供了std :: to_string,它可以用来将数字类型转换为字符串。

#2


14  

string.append is much faster. Especially when you reserve.

string.append要快得多。特别是当你保留。

If you are concatenating only strings, I would use string.append. I would only use stringstream when I need to automatically convert non-strings to strings for example:

如果你只连接字符串,我会使用string.append。我只需要在需要自动将非字符串转换为字符串时使用stringstream,例如:

const int x(42);
stringstream ss;
ss << "My favorite number is: " << x << std::endl;

Here stringstream automatically converts x to a string and appends it. I do not need to call atoi. Stringstream will convert all the basic types automatically for you. It is great for that purpose.

这里stringstream自动将x转换为字符串并附加它。我不需要打电话给atoi。 Stringstream将自动为您转换所有基本类型。这个目的很棒。

Also if you are only going to be directing data into the stringstream to convert it to a string later. You can use ostringstream which is for output.

此外,如果您只是将数据导入stringstream,以便稍后将其转换为字符串。您可以使用ostringstream作为输出。

I hope that helps.

我希望有所帮助。