C++ String size()
C++ String size()
此函数用于返回以字节为单位的字符串长度。它定义了符合字符串对象内容的实际字节数,不一定等于容量。
语法
请考虑一个名为'str'的字符串对象。要计算此字符串对象的大小,其语法应为:
参数
此函数不包含任何参数。
返回值
此函数返回字符串对象中存在的字符数。
示例1
#include<iostream>
using namespace std;
int main()
{
string str ="Welcome to the lidihuo tutorial";
int size = str.size();
cout << "length of the string is :" <<size;
return 0;
}
输出:
length of the string is : 34
在此示例中,str是一个字符串对象,包含值" Welcome to the lidihuo tutorial"。我们使用'size'函数计算字符串的长度。