C++ 中用 length() 函数求字符串长度,返回字符数量(包括空格)。语法:size_t length() const。

C++ 中求字符串长度的函数
在 C++ 中,使用 length() 函数可以求字符串的长度。该函数返回字符串中字符的数量,包括空白字符。
length() 函数的使用语法如下:
size_t length() const;
返回值:
立即学习“C++免费学习笔记(深入)”;
- 返回一个
size_t类型的值,表示字符串中的字符数量。
示例:
以下代码示例演示如何使用 length() 函数:
#include#include int main() { std::string str = "Hello, world!"; std::cout << "The length of the string is: " << str.length() << std::endl; return 0; }
输出:
The length of the string is: 13











