C++ 参考手册

位置:首页 > C++ 参考手册 >字符串库 >std::basic_string > std::basic_string<CharT,Traits,Allocator>::empty

bool empty() const;
(C++11 前)
bool empty() const noexcept;
(C++11 起)
(C++20 前)
[[nodiscard]] constexpr bool empty() const noexcept;
(C++20 起)

检查 string 是否无字符,即是否 begin() == end()

参数

(无)

返回值

若 string 为空则为 true ,否则为 false

复杂度

常数。

示例

#include <iostream>
#include <string>
 
int main()
{
    std::string s;
    std::boolalpha(std::cout);
    std::cout << "s.empty():" << s.empty() << "\t s:'" << s << "'\n";
 
    s = "Exemplar";
    std::cout << "s.empty():" << s.empty() << "\t s:'" << s << "'\n";
 
    s = "";
    std::cout << "s.empty():" << s.empty() << "\t s:'" << s << "'\n";
}

输出:

s.empty():true	 s:''
s.empty():false	 s:'Exemplar'
s.empty():true	 s:''

参阅

返回字符数
(公开成员函数)