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

出自cppreference.com
 
 
 
 
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 << "' 有 " << ref.size()
              << " 个字符;是否为空: " << ref.empty() << '\n';
}
 
int main(int argc, char **argv)
{
    // 空字符串
    check_string("");
 
    // 几乎始终非空:argv[0]
    if (argc > 0)
        check_string(argv[0]);
}

可能的輸出:

'' 有 0 个字符;是否为空: true
'./a.out' 有 7 个字符;是否为空: false

[編輯] 參閱

檢查視圖是否為空
(公開成員函數) [編輯]
返回最大字元數
(公開成員函數) [編輯]
返回字元數
(std::basic_string<CharT,Traits,Allocator> 的公開成員函數) [編輯]