C++ String Library - empty



Description

It returns whether the string is empty (i.e. whether its length is 0).

Declaration

Following is the declaration for std::string::empty.

bool empty() const;

C++11

bool empty() const noexcept;

Parameters

none

Return Value

it returns true if the string length is 0, false otherwise.

Exceptions

if an exception is thrown, there are no changes in the string.

Example

In below example for std::string::empty.

#include <iostream>
#include <string>

int main () {
   std::string content;
   std::string line;
   std::cout << "Please introduce a text. Enter an empty line to finish:\n";
   do {
      getline(std::cin,line);
      content += line + '\n';
   } while (!line.empty());
   std::cout << "The text you introduced above was:\n" << content;
   return 0;
}

The sample output should be like this −

Please introduce a text. Enter an empty line to finish:
sairamrkshna mammahe

The text you introduced above was:
sairamrkshna mammahe
string.htm
Advertisements