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.
-
12Agreed. 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 uselength()
when working with plain strings.– MariusNov 11, 2010 at 15:50 -
7Doesn'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
-
8No. They are the same function; they even share documentation: en.cppreference.com/w/cpp/string/basic_string/size. Jan 31, 2016 at 3: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. Jan 31, 2016 at 3:09
-
11You 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.
Dec 2, 2016 at 12:06
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;-).
-
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. Jan 8, 2012 at 9:56
When using coding practice tools(LeetCode) it seems that size() is quicker than length() (although basically negligible)
-
5
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. Apr 5, 2017 at 2:49