C++ 字串庫 - 空



描述

它返回字串是否為空(即其長度是否為 0)。

宣告

以下是 std::string::empty 的宣告。

bool empty() const;

C++11

bool empty() const noexcept;

引數

返回值

如果字串長度為 0,則返回 true,否則返回 false。

異常

如果丟擲異常,字串不會發生任何變化。

示例

以下為 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;
}

示例輸出應如下所示:

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

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

© . All rights reserved.