I was reading the answers for this question and found that there is actually a method called length()
for std::string
(I always used size()
). Is there any specific reason for having this method in std::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. 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.