温馨提示×

c++中如何求字符串长度

c++
小亿
106
2024-03-28 10:23:11
栏目: 编程语言

在C++中,可以使用std::string类的size()方法或者length()方法来获取字符串的长度。示例代码如下:

#include <iostream>
#include <string>

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

以上代码会输出字符串Hello, World!的长度,即13

0