std::basic_string_view<CharT,Traits>::size, std::basic_string_view<CharT,Traits>::length
来自 cppreference.cn
< 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 成员函数) |