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

前端 未结 4 648
你的背包
你的背包 2020-11-29 15:43

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()

相关标签:
4条回答
  • 2020-11-29 16:08

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

    0 讨论(0)
  • 2020-11-29 16:11

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

    0 讨论(0)
  • 2020-11-29 16: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

    0 讨论(0)
  • 2020-11-29 16:22

    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.

    0 讨论(0)
提交回复
热议问题