C++之string长度
(1)采用string的成员方法length()获取字符串长度
(2)采用string的成员方法size()获取字符串长度
(3)用strlen获取字符串长度
(4)函数实现获取字符串长度的功能
int GetStringWords( string strWord )
{
int nWords(0);
int nLen = strWord.length();
int i(0);
while (i<nLen)
{
//如果是汉字则移到下一个
if (strWord[i]&0x80)
{
i++;
}
nWords++;
i++;
}
return nWords;
}
总结:(1)、(2)、(3)如果有中文,代表两个字符,(4)不区分中英文,根据实际个数进行计算字符串长度。

博客介绍了在C++中获取字符串长度的方法,包括使用string的length()和size()成员方法、strlen函数,还提及函数实现获取长度功能。同时指出前三种方法遇到中文时一个中文算两个字符,函数实现则按实际个数计算。
36万+

被折叠的 条评论
为什么被折叠?



