std::strlen
来自 cppreference.cn
定义于头文件 <cstring> |
||
std::size_t strlen( const char* str ); |
||
返回给定字节字符串的长度,即字符数组中字符的数量,该字符数组的第一个元素由 str 指向,直到但不包括第一个空字符。 如果由 str 指向的字符数组中没有空字符,则行为未定义。
目录 |
[编辑] 参数
str | - | 指向要检查的空终止字节字符串的指针 |
[编辑] 返回值
空终止字符串 str 的长度。
[编辑] 可能的实现
std::size_t strlen(const char* start) { // NB: start is not checked for nullptr! const char* end = start; while (*end != '\0') ++end; return end - start; } |
[编辑] 示例
运行此代码
#include <cstring> #include <iostream> int main() { const char str[] = "dog cat\0mouse"; std::cout << "without null character: " << std::strlen(str) << '\n' << "with null character: " << sizeof str << '\n'; }
输出
without null character: 7 with null character: 14
[编辑] 参见
返回宽字符串的长度 (函数) | |
返回下一个多字节字符中的字节数 (函数) | |
C 文档 for strlen
|