【发布时间】:2012-11-13 13:02:48
【问题描述】:
考虑:
std::string s_a, s_b;
std::stringstream ss_1, ss_2;
// at this stage:
// ss_1 and ss_2 have been used and are now in some strange state
// s_a and s_b contain non-white space words
ss_1.str( std::string() );
ss_1.clear();
ss_1 << s_a;
ss_1 << s_b;
// ss_1.str().c_str() is now the concatenation of s_a and s_b,
// <strike>with</strike> without space between them
ss_2.str( s_a );
ss_2.clear();
// ss_2.str().c_str() is now s_a
ss_2 << s_b; // line ***
// ss_2.str().c_str() the value of s_a is over-written by s_b
//
// Replacing line *** above with "ss_2 << ss_2.str() << " " << s_b;"
// results in ss_2 having the same content as ss_1.
问题:
stringstream.str(a_value); 有什么区别和 字符串流
为什么 ss_1 会自动获得 s_a 和 s_b 之间的空白,但是 我们是否需要在可能的行中显式添加空格 替换行***:ss_2 << ss_2.str() << " " << s_b;?
【问题讨论】:
-
在您的示例中,您的字符串为空。
automatically get white-space between s_a and s_b是什么意思? -
@JesseGood
// s_a and s_b contain non-white space words
标签: c++ initialization concatenation stringstream