std :: string length()和size()成员函数 [英] std::string length() and size() member functions
问题描述
我正在阅读此问题的答案,发现实际上有一个名为length()的方法,对于字符串(我总是使用size())。在string类中有这个方法有什么特别的原因吗?我读了MSDN和CppRefernce,他们似乎表明,size()和length()之间没有区别。
根据文档,这些只是同义词。 I was reading the answers for this question and found that there is actually method called length() for strings (I always used size()). Is there any specific reason for having this method in string class ? I read both MSDN and CppRefernce and they seem to indicate that there is no difference between size() and length(). If that is so, isn't it making more confusing for the user of the class? As per the documentation, these are just synonyms. 这篇关于std :: string length()和size()成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!,
,
,
和其他STL容器是一致的/ code>,等)和
length()
是与大多数人对字符串的直观概念一致。人们通常谈论一个词,句子或段落的长度,而不是它的大小,因此 length()
p> size()
is there to be consistent with other STL containers (like vector
, map
, etc.) and length()
is to be consistent with most peoples' intuitive notion of character strings. People usually talk about a word, sentence or paragraph's length, not its size, so length()
is there to make things more readable.