Namespaces
Variants

std::basic_string_view<CharT,Traits>:: size, std::basic_string_view<CharT,Traits>:: length

From cppreference.net
constexpr size_type size ( ) const noexcept ;
(C++17以降)
constexpr size_type length ( ) const noexcept ;
(C++17以降)

ビューの CharT 要素数を返します。すなわち std:: distance ( begin ( ) , end ( ) ) です。

目次

パラメータ

(なし)

戻り値

ビュー内の CharT 要素の数。

計算量

定数。

#include <iostream>
#include <string_view>
// シングルクォートで囲まれた文字列、その長さ、
// および空と見なされるかどうかを出力する。
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

関連項目

ビューが空かどうかをチェックする
(公開メンバ関数)
最大文字数を返す
(公開メンバ関数)
文字数を返す
( std::basic_string<CharT,Traits,Allocator> の公開メンバ関数)