Example 1 — Basic clear()
Clear a string and check empty.
#include <iostream>
#include <string>
int main() {
std::string s = "hello";
s.clear();
std::cout << std::boolalpha << s.empty() << '\n';
std::cout << "size: " << s.size() << '\n';
return 0;
} How It Works
- The string starts with five characters.
clear()removes them all.empty()is true and size is 0.

