std::char_traits<char>::length, std::char_traits<wchar_t>::length, std::char_traits<char8_t>::length, std::char_traits<char16_t>::length, std::char_traits<char32_t>::length
來自 cppreference.com
< cpp | string | char traits
static std::size_t length( const char_type* s ); |
(自 C++17 起為 constexpr) | |
返回指向 s 的字元序列的長度,即終止空字元 (char_type()) 的位置。
關於 X::length
的字元特性的一般要求,請參見 CharTraits。
目錄 |
[編輯] 引數
s | - | 指向要返回長度的字元序列的指標 |
[編輯] 返回值
指向 s 的字元序列的長度。
[編輯] 複雜度
線性。
[編輯] 示例
執行此程式碼
#include <iomanip> #include <iostream> #include <string> void print(const char* str) { std::cout << std::quoted(str) << " has length = " << std::char_traits<char>::length(str) << '\n'; } int main() { print("foo"); std::string s{"booo"}; print(s.c_str()); }
輸出
"foo" has length = 3 "booo" has length = 4