假设我有
string sentence{"Hello how are you."}
我希望将字符串中的“Hello”替换为“你好”,得到的字符串仍然包含“how are you”。应该如何实现?
我尝试了以下代码:
stringstream ss(sentence);
ss>> string junkWord;//to get rid of first word
但是当我这样做时:
cout<<sentence;//still prints out "Hello how are you"
很明显,
stringstream
不会改变实际的字符串。我也尝试使用 strtok
但它与 string
不兼容。
std::getline
:http://ideone.com/IxZRAt 。不过在任何情况下我都不会使用这种方法... - MatthiasB