Avatar ·

How to properly clear stringstream in C++ and prepare it for reuse?

Hello everyone! I'm new to C++ and encountered a problem while working with stringstream. I need to form strings using stringstream in a loop, but after the first iteration it doesn't clear, and data accumulates. Please advise how to properly reset the stringstream state? Here's my code: ```cpp #include #include #include int main() { std::stringstream ss; for (int i = 0; i < 3; ++i) { ss << "Number: " << i; std::string result = ss.str(); std::cout << "Iteration " << i << ": " << result << std::endl; // Here I'm trying to clear ss, but I don't know how to do it properly // ss.str(""); // Tried this - doesn't work } return 0; } ``` Output of this code: ``` Iteration 0: Number: 0 Iteration 1: ```