public member function
<string>

std::basic_string::clear

void clear();
void clear() noexcept;
Clear string
Erases the contents of the basic_string, which becomes an empty string (with a length of 0 characters).

Parameters

none

Return value

none

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// string::clear
#include <iostream>
#include <string>

int main ()
{
  char c;
  std::string str;
  std::cout << "Please type some lines of text. Enter a dot (.) to finish:\n";
  do {
    c = std::cin.get();
    str += c;
    if (c=='\n')
    {
       std::cout << str;
       str.clear();
    }
  } while (c!='.');
  return 0;
}

This program repeats every line introduced by the user until a the line contains a dot ('.'). Every newline character ('\n') triggers the repetition of the line and the clearing of the current string content.

Complexity

Unspecified, but generally constant.

Iterator validity

Any iterators, pointers and references related to this object may be invalidated.

Data races

The object is modified.

Exception safety

No-throw guarantee: this member function never throws exceptions.

See also