#include#include #include using namespace std; int main() { stringstream ss; ss << "Hello World"; cout << "Before clear: " << ss.str() << endl; ss.clear(); cout << "After clear: " << ss.str() << endl; return 0; }
Before clear: Hello World After clear:
#include#include #include using namespace std; int main() { stringstream ss("1234"); int num; ss >> num; cout << "Extracted number: " << num << endl; ss.clear(); ss << "5678"; ss >> num; cout << "Extracted number after clear: " << num << endl; return 0; }
Extracted number: 1234 Extracted number after clear: 5678This example demonstrates that the clear function cleared the state flags of the stringstream object, allowing us to extract a different value from it after inserting a new string. The std stringstream clear function is a part of the C++ Standard Library.