温馨提示×

c++中length函数的作用是什么

c++
小亿
121
2024-06-12 18:10:07
栏目: 编程语言

在C++中,length()函数通常用于获取字符串的长度,即字符串中字符的数量。这个函数通常用于标准库中的string类,用来获取字符串的长度。示例代码如下:

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    int len = str.length();
    
    std::cout << "Length of the string is: " << len << std::endl;
    
    return 0;
}

上面的代码将输出字符串"Hello, World!"的长度,即13。在C++中,字符串的长度通常是以字符的数量来表示,而不包括字符串结尾的空字符。

0