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

- 58,354
- 15
- 89
- 96
-
13Agreed. 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 '10 at 15:50
-
8Doesn'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? – Boyan Kushlev Jan 30 '16 at 16:05
-
8No. They are the same function; they even share documentation: http://en.cppreference.com/w/cpp/string/basic_string/size. – Todd Gamblin Jan 31 '16 at 03:03
-
7They'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. – Todd Gamblin Jan 31 '16 at 03:09
-
12You 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
count of char` `std::wstring => std::basic_string – Sheen Tian Dec 02 '16 at 12:06count of wchar_t.` -
3I 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. – locka Dec 13 '18 at 09:34
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;-).

- 854,459
- 170
- 1,222
- 1,395
-
12In 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. – Adrian Ratnapala Jan 08 '12 at 09:56
When using coding practice tools(LeetCode) it seems that size() is quicker than length() (although basically negligible)

- 33
- 1
- 11
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
-
11incorrect answer. See documentation linked in the accepted answer. – Stefan de Kok Apr 05 '17 at 02:49