335

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?

4 Answers 4

453

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.

6
  • 12
    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.
    – Marius
    Nov 11, 2010 at 15:50
  • 7
    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? Jan 30, 2016 at 16:05
  • 8
    No. They are the same function; they even share documentation: en.cppreference.com/w/cpp/string/basic_string/size. Jan 31, 2016 at 3:03
  • 7
    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. Jan 31, 2016 at 3:09
  • 11
    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.
    – Sheen Tian
    Dec 2, 2016 at 12:06
19

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;-).

1
  • 12
    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. Jan 8, 2012 at 9:56
-6

When using coding practice tools(LeetCode) it seems that size() is quicker than length() (although basically negligible)

1
-20

length of string ==how many bits that string having, size==size of those bits, In strings both are same if the editor allocates size of character is 1 byte

1
  • 11
    incorrect answer. See documentation linked in the accepted answer. Apr 5, 2017 at 2:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.