cplusplus.com
C++ : Reference : <string> : string : clear
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Reference
C library:
(assert.h)
(ctype.h)
(errno.h)
(fenv.h)
(float.h)
(inttypes.h)
(iso646.h)
C library
(limits.h)
(locale.h)
(math.h)
(setjmp.h)
(signal.h)
(stdarg.h)
(stdbool.h)
(stddef.h)
(stdint.h)
(stdio.h)
(stdlib.h)
(string.h)
(tgmath.h)
(time.h)
(uchar.h)
(wchar.h)
(wctype.h)
Containers:
Containers
Input/Output:
Input/Output
Other:
Other
class templates:
basic_string
char_traits
classes:
string
u16string
u32string
wstring
functions:
stod
stof
stoi
stol
stold
stoll
stoul
stoull
to_string
to_wstring
string
string::string
string::~string
member functions:
string::append
string::assign
string::at
string::back
string::begin
string::capacity
string::cbegin
string::cend
string::clear
string::compare
string::copy
string::crbegin
string::crend
string::c_str
string::data
string::empty
string::end
string::erase
string::find
string::find_first_not_of
string::find_first_of
string::find_last_not_of
string::find_last_of
string::front
string::get_allocator
string::insert
string::length
string::max_size
string::operator+=
string::operator=
string::operator[]
string::pop_back
string::push_back
string::rbegin
string::rend
string::replace
string::reserve
string::resize
string::rfind
string::shrink_to_fit
string::size
string::substr
string::swap
member constants:
string::npos
non-member overloads:
getline (string)
operator+ (string)
operator<< (string)
operator>> (string)
relational operators (string)
swap (string)


string::clear

public member function
<string>
[C++98]
void clear();
[C++11]
void clear() noexcept;

Clear string

Erases the contents of the 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 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