在C++中,可以使用std::string
类来表示字符串,并使用size()
成员函数来获取字符串的长度。例如:
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
int length = str.size();
std::cout << "The length of the string is: " << length << std::endl;
return 0;
}
在上面的例子中,str.size()
返回字符串str
的长度,并将其存储在变量length
中。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:c++怎么计算字符串长度