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?
C++ – std::string length() and size() member functions
csizestlstring
Related Topic
- C++ – How to convert a std::string to const char* or char*
- C++ – std::wstring VS std::string
- Php – startsWith() and endsWith() functions in PHP
- C++ – Static constant string (class member)
- Python – Random string generation with upper case letters and digits
- C++ – Image Processing: Algorithm Improvement for ‘Coca-Cola Can’ Recognition
- C# – System.Windows.Application.GetResourceStream returns null
Best Answer
As per the documentation, these are just synonyms.
size()
is there to be consistent with other STL containers (likevector
,map
, etc.) andlength()
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, solength()
is there to make things more readable.