C++ String Length: size() vs length()
In C++, you can determine the length of a string by using the size() or length() methods of a string object. Example:
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
// 使用size()方法获取字符串长度
std::cout << "Length of string (using size()): " << str.size() << std::endl;
// 使用length()方法获取字符串长度
std::cout << "Length of string (using length()): " << str.length() << std::endl;
return 0;
}
The above code will produce:
Length of string (using size()): 13
Length of string (using length()): 13