Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does string stream operator<< erases the original value

Tags:

c++

Can someone explain why the following code produce " you!" instead of "hello you!".

int main() {
    std::stringstream ss("hello");
    ss << " you!";
    cout << ss.str() << endl;
}
like image 557
Negative Zero Avatar asked Jun 22 '26 09:06

Negative Zero


1 Answers

openmode must have ate set.

This is what you're looking for:

int main() {
    std::stringstream ss("hello", std::ios_base::out | std::ios_base::ate);
    ss << " you!";
    cout << ss.str() << endl;
}
like image 173
Hen Sapir Avatar answered Jun 24 '26 00:06

Hen Sapir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!