可以使用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函数可能会导致未定义的行为。

c++ 获取char的长度

原文地址: https://www.cveoy.top/t/topic/gc3A 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录