#include#include int main() { std::string myString = "Hello World!"; std::cout << "Before clear(): " << myString << std::endl; myString.clear(); std::cout << "After clear(): " << myString << std::endl; return 0; }
#includeIn this example, instead of calling the clear() function, we assign an empty string to the "myString" variable. The output will be the same as in Example 1, where the string is emptied. The std string clear() function is part of the C++ Standard Library.#include int main() { std::string myString = "Hello World!"; std::cout << "Before clear(): " << myString << std::endl; myString = ""; std::cout << "After assigning an empty string: " << myString << std::endl; return 0; }