cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Strings library : string : string::clear
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Strings library
char_traits
classes:
· string
global functions:
· getline
· operator+
· operator<<
· operator>>
· comparison operators
· swap
string
string::string
member constants:
· string::npos
member functions:
· string::append
· string::assign
· string::at
· string::begin
· string::capacity
· string::clear
· string::compare
· string::copy
· 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::get_allocator
· string::insert
· string::length
· string::max_size
· string::operator+=
· string::operator=
· string::operator[]
· string::push_back
· string::rbegin
· string::rend
· string::replace
· string::reserve
· string::resize
· string::rfind
· string::size
· string::substr
· string::swap

-

string::clear public member function
void clear();

Clear string

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

Example

// string::clear
#include <iostream>
#include <string>
using namespace std;

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

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

Basic template member declaration

( basic_string<charT,traits,Allocator> )
void clear ( );

See also

string::erase Erase characters from string (public member function)
string::resize Resize string (public member function)
string::empty Test if string is empty (public member function)

Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us