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
[编辑] 另请参阅
检查视图是否为空 (公共成员函数) | |
返回最大字符数 (公共成员函数) | |
返回字符数 ( std::basic_string<CharT,Traits,Allocator> 的公共成员函数) |