阅读量:103
在C++中,可以使用std::string类的size()方法或者length()方法来获取字符串的长度。示例代码如下:
#include
#include
int main() {
std::string str = "Hello, World!";
int length1 = str.size();
int length2 = str.length();
std::cout << "Length of the string is: " << length1 class="hljs-string">"Length of the string is: " << length2 class="hljs-keyword">return 0;
}
以上代码会输出字符串Hello, World!的长度,即13。