std::basic_string_view<CharT,Traits>::size, std::basic_string_view<CharT,Traits>::length
來自 cppreference.com
< cpp | string | basic_string_view
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> // Print a string surrounded by single quotes, its // length and whether it is considered empty. 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) { // An empty string check_string(""); // Almost always not empty: argv[0] if (argc > 0) check_string(argv[0]); }
可能的輸出
'' has 0 character(s); emptiness: true './a.out' has 7 character(s); emptiness: false
[編輯] 參閱
檢查檢視是否為空 (public 成員函式) | |
返回字元的最大數量 (public 成員函式) | |
返回字元數 ( std::basic_string<CharT,Traits,Allocator> 的 public 成員函式) |