租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

c++中length函数的用法是什么,c中length函数的头文件

发布时间:2024-06-12 18:09:10

c++中length函数的用法是甚么

在C++中,length函数通经常使用于获得字符串的长度。它可以用于std::string和C风格的字符串。

对std::string,可使用string的成员函数size()或length()来获得字符串的长度。示例以下:

#include <iostream>
#include <string>

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

对C风格的字符串,可使用strlen函数来获得字符串的长度。示例以下:

#include <iostream>
#include <cstring>

int main() {
    const char *str = "Hello, World!";
    
    std::cout << "Length of the string: " << strlen(str) << std::endl;
    
    return 0;
}

总的来讲,length函数用于获得字符串的长度,可以用于std::string和C风格的字符串。