c++中length函数怎样使用
在C++中,length函数通经常使用于获得字符串的长度。例如,我们可使用length函数来获得一个字符串变量的长度,示例以下:
#include <iostream>
#include <string>
int main() {
std::string str = "Hello World";
int len = str.length();
std::cout << "Length of the string: " << len << std::endl;
return 0;
}
在上面的示例中,我们定义了一个字符串变量str,并使用length函数获得了该字符串的长度,然后将结果打印到控制台上。因此,length函数可以帮助我们获得字符串的长度,从而进行相应的处理或操作。
TOP