std::string::clear in C++ Last Updated : 06 Jul, 2017 Comments Improve Suggest changes Like Article Like Report 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 // 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; } Output: Before clear : Hello World! After clear : Related Article: std::string::erase If you like GeeksforGeeks (We know you do!) and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to [email protected]. Comment More infoAdvertise with us Next Article std::string::clear in C++ K kartik Improve Article Tags : C++ STL cpp-strings-library Practice Tags : CPPSTL Similar Reads std::string class in C++ C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character.String vs Character ArrayStringChar 8 min read std::string::data() in C++ The data() function writes the characters of the string into an array. It returns a pointer to the array, obtained from conversion of string to the array. Its Return type is not a valid C-string as no '\0' character gets appended at the end of array. Syntax: const char* data() const; char* is the po 2 min read set::clear in C++ STL Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::clear() clear() 2 min read list::clear() in C++ STL Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::clear()clear() function is used to remove a 1 min read Vector clear() in C++ STL In C++, vector clear() is a built-in method used to remove all elements from a vector, making it empty. In this article, we will learn about the vector clear() method in C++.Letâs take a look at an example that illustrates the vector clear() method:C++#include <bits/stdc++.h> using namespace s 2 min read Like