C++ 获取字符数组长度 (strlen 函数)
可以使用 C++ 标准库中的 'strlen' 函数来获取一个以 null 结尾的字符数组(也就是 C 风格字符串)的长度。示例代码如下:
#include <iostream>
#include <cstring>
int main() {
const char* str = 'hello world';
std::cout << strlen(str) << std::endl; // 输出 11
return 0;
}
注意,字符串的长度不包括 null 结尾符。如果字符串中没有 null 结尾符,'strlen' 函数可能会导致未定义的行为。
原文地址: https://www.cveoy.top/t/topic/ot1y 著作权归作者所有。请勿转载和采集!