std::string length() and size() member functions

195,597

Solution 1

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.

Solution 2

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

Share:
195,597
Kiran Kumar
Author by

Kiran Kumar

Software developer from Bangalore, my area of interest is mainly C++. 33rd to C++ Gold Badge

Updated on October 14, 2020

Comments

  • Kiran Kumar
    Kiran Kumar over 3 years

    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?

  • Marius
    Marius over 13 years
    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.
  • Adrian Ratnapala
    Adrian Ratnapala over 12 years
    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.
  • Boyan Kushlev
    Boyan Kushlev about 8 years
    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?
  • Todd Gamblin
    Todd Gamblin about 8 years
    No. They are the same function; they even share documentation: en.cppreference.com/w/cpp/string/basic_string/size.
  • Todd Gamblin
    Todd Gamblin about 8 years
    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.
  • Sheen Tian
    Sheen Tian over 7 years
    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.
  • Stefan de Kok
    Stefan de Kok about 7 years
    incorrect answer. See documentation linked in the accepted answer.
  • locka
    locka over 5 years
    I would avoid length() because the concept is utterly meaningless without knowing what the encoding is. I might have string of length 10 but that does not mean that there are 10 printable characters inside of it.
  • Sam Mason
    Sam Mason over 3 years
    looks pretty similar to me! godbolt.org/z/xMao9f