367

I was reading the answers for How to get the number of characters in a std::string? 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 member function in the std::string class? I've 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?

2 Answers 2

501

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Agreed. When I write template classes and functions I'd rather use size() (In case I ever use non-string classes), but most of the time I use length() when working with plain strings.
Doesn't size() return the size of the string in the memory (in bytes), whereas length() returns the number of characters, which just happen to coincide, since 1 char = 1 byte?
No. They are the same function; they even share documentation: en.cppreference.com/w/cpp/string/basic_string/size.
They're both defined to be equivalent to distance(s.begin(), s.end()), where begin() and end() are iterators over CharT elements. CharT is a template parameter that determines what's in the string. For std::string, CharT is char. For std::wstring, CharT is wchar_t, which is typically 2 or 4 bytes. Even there, both length() and size() will return the number of characters in the string, NOT the number of bytes.
You must forget sometimes people will use std::string to store the UTF8 string. and utf8 string is variable length coding, then you can not use the length() or size() to get the count of the string character. Actually they just return the count of the element: std::string=> std::bacsic_string<char> count of char std::wstring => std::basic_string<wchar_t> count of wchar_t.
|
20

Ruby's just the same, btw, offering both #length and #size as synonyms for the number of items in arrays and hashes (C++ only does it for strings).

Minimalists and people who believe "there ought to be one, and ideally only one, obvious way to do it" (as the Zen of Python recites) will, I guess, mostly agree with your doubts, @Naveen, while fans of Perl's "There's more than one way to do it" (or SQL's syntax with a bazillion optional "noise words" giving umpteen identically equivalent syntactic forms to express one concept) will no doubt be complaining that Ruby, and especially C++, just don't go far enough in offering such synonymical redundancy;-).

2 Comments

In this case it is gratuitous. Perl's grammar and usage let you express things using whichever style you prefer. Having two different words for the same thing merely makes it hard to come up with search-terms in Stackoverflow.
The guy asked a question about C++ and here you are answering about Ruby...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.