在 C++ 中,使用 length() 函数获取字符串长度,它返回字符串中字符的数量。例如,字符串 "Hello, world!" 的 length() 为 13。

C++ 中获取字符串长度
在 C++ 中,您可以使用 length() 函数来获取字符串的长度。length() 函数返回一个无符号整数,表示字符串中字符的数量。
以下是一个使用 length() 函数获取字符串长度的示例:
#include#include using namespace std; int main() { string my_string = "Hello, world!"; // 使用 length() 函数获取字符串长度 int length = my_string.length(); // 打印字符串长度 cout << "The length of the string is: " << length << endl; return 0; }
输出:
立即学习“C++免费学习笔记(深入)”;
The length of the string is: 13











