std::string::clear in C++
The string content is set to an empty string, erasing any previous content and thus leaving its size at 0 characters.
Parameters: none
Return Value: none
void string ::clear () - Removes all characters (makes string empty) - Doesn't throw any error - Receives no parameters and returns nothing
// CPP code to illustrate // clear() function #include <iostream> #include <string> using namespace std; // Function to demo clear() void clearDemo(string str) { // Deletes all characters in string str.clear(); cout << "After clear : "; cout << str; } // Driver code int main() { string str("Hello World!"); cout << "Before clear : "; cout << str << endl; clearDemo(str); return 0; } |
chevron_right
filter_none
Output:
Before clear : Hello World! After clear :
Related Article: std::string::erase
This article is contributed by Sakshi Tiwari. If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Synchronous Chatting Application using C++ boost::asio
- C/C++ For loop with Examples
- How to input a comma separated string in C++?
- Program to Parse a comma separated string in C++
- Difference between Private and Protected in C++ with Example
- Difference between Inheritance and Polymorphism
- Difference between Single and Multiple Inheritance in C++
- Difference between Abstraction and Encapsulation in C++
- return statement in C/C++ with Examples
- How to implement our own Vector Class in C++?
- Web Programming in C++
- C++ Program to print an Array using Recursion
- Storage Classes in C++ with Examples
- fill_n() function in C++ STL with examples
- fill() function in C++ STL with examples



