在 C++ 中,你可以使用 类型的 或 成员函数来获取字符串的长度。这两个函数都返回一个 澳门免费资料十香港诗 婆家一肖一码100准资料大全类型的值,表示字符串的长度。
以下是示例代码:
#include <iostream> #include <string> int main() { std::string str = "Hello, world!"; 2020澳门彩资料 std::cout << "Length of the string: " << str.length() << std::endl; // 或者使用 size() 函数 // std::cout << "Length of the string: " << str.size() << std::endl; return 0; }
在上面的代码中,我们创建了一个名为 的 对象,并使用 函数来获取它的长度。输出将是字符串的长度,即 。
请注意, 和 函数在功能上是等效的,你可以根据个人喜好选择使用其中之一。
评论列表