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

C++ 中获取字符串长度
在 C++ 中,您可以使用 length() 函数来获取字符串的长度。length() 函数返回一个无符号整数,表示字符串中字符的数量。
以下是一个使用 length() 函数获取字符串长度的示例:
<code class="cpp">#include <iostream>
#include <string>
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;
}</code>输出:
立即学习“C++免费学习笔记(深入)”;
<code>The length of the string is: 13</code>
以上就是c++++中字符串的长度怎么看的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号