std::basic_string_view<CharT,Traits>::empty - cppreference.com

std::basic_string_view<CharT,Traits>::empty

提供: cppreference.com
 
 
 
 
<tbody> </tbody> <tbody class="t-dcl-rev "> </tbody><tbody> </tbody>
constexpr bool empty() const noexcept;
(C++17以上)
(C++20未満)
[[nodiscard]] constexpr bool empty() const noexcept;
(C++20以上)

ビューが文字を持っていない、すなわち size() == 0 かどうかを調べます。

引数

(なし)

戻り値

ビューが空であれば true、そうでなければ false

計算量

一定。

#include <string_view>
#include <iostream>

void check_string(std::string_view ref)
{
        // シングルクォートで囲まれた文字列、その長さ、
        // および空かどうかを表示します。
        std::cout << std::boolalpha
                  << "'" << ref << "' has " << ref.size()
                  << " character(s); emptiness: " << ref.empty() << '\n';
}

int main(int argc, char **argv)
{
        // 空文字列。
        check_string("");

        // ほとんど常に空でない: argv[0]
        if (argc > 0)
                check_string(argv[0]);
}

出力例:

'' has 0 character(s); emptiness: true
'./a.out' has 7 character(s); emptiness: false

関連項目