溫馨提示×

C++中string.empty怎么使用

小億
217
2023-07-12 11:34:48
欄目: 編程語言

在C++中,string.empty()是用來判斷一個字符串是否為空的函數。它返回一個bool值,如果字符串為空,則返回true,否則返回false。

以下是使用string.empty()的示例:

#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello";
std::string str2 = "";
if (str1.empty()) {
std::cout << "str1 is empty" << std::endl;
} else {
std::cout << "str1 is not empty" << std::endl;
}
if (str2.empty()) {
std::cout << "str2 is empty" << std::endl;
} else {
std::cout << "str2 is not empty" << std::endl;
}
return 0;
}

輸出結果為:

str1 is not empty
str2 is empty

在上面的例子中,str1不為空,所以str1.empty()返回false。而str2為空,所以str2.empty()返回true。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女