C++中string.empty的用法是什么

   2025-02-13 8700
核心提示:在C++中,string.empty()是一个成员函数,用于检查一个字符串是否为空。它返回一个bool值,如果字符串为空则返回true,否则返回f

在C++中,string.empty()是一个成员函数,用于检查一个字符串是否为空。它返回一个bool值,如果字符串为空则返回true,否则返回false。

使用示例:

#include <iostream>#include <string>int main() {    std::string str1 = "";    std::string str2 = "Hello";    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 emptystr2 is not empty

在上面的示例中,我们使用empty()函数检查了两个字符串的状态。str1为空字符串,所以empty()函数返回true,而str2不为空,所以empty()函数返回false。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言