CLEAR()

This member function of std:string is used to remove all characters from a string, effectively making it an empty string while keeping the string object itself valid. After calling clear(), the string’s size becomes zero, but the capacity may remain unchanged, allowing efficient reuse without reallocating memory. This method is commonly used when resetting a string for reuse, freeing logical content, or preparing a string for new input, making it a simple and efficient way to manage dynamic string contents

string_variable.clear();
#include <iostream>
using namespace std;

int main() 
{
  string message = "Temporary text";  // Initialize string with content

  message.clear();  // Remove all characters

  cout << "Message: " << message << endl; 

  return 0;
}

Last updated