17

length() returns the number of characters in the string and size() returns a size_t which is also the same but used to make it consistent with other STL containers.

For computing length(), the string iterates through all the characters and counts the length. So, O(n) time.

Is size() also the same ?

Or can size of a variable be computed directly in O(1) time ?

So, my question is, are they the same in terms of speed (as in how they are calculated) or is size computed in O(1) time ?

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
ronilp
  • 455
  • 2
  • 9
  • 25
  • Try running a loop over a set of datas(And calculate the time) you will know which one is fast. –  Jul 25 '15 at 17:01
  • 5
    What makes you think `length()` is O(n)? – Oliver Charlesworth Jul 25 '15 at 17:02
  • 2
    O(n) in size of input ? because it will have to iterate over all the characters of the string ? – ronilp Jul 25 '15 at 17:03
  • It doesn't have to iterate. – Oliver Charlesworth Jul 25 '15 at 17:05
  • So, it just uses the number of bytes required to count the length ? – ronilp Jul 25 '15 at 17:08
  • "For computing length(), the string iterates through all the characters and counts the length" - where did you get that idea??? `std::string` is designed as a string with *stored* length. It does not have to iterate. – AnT stands with Russia Jul 25 '15 at 17:12
  • I thought that might be because in other STL containers like vector etc. that is what has to be done.... – ronilp Jul 25 '15 at 17:14
  • `vector` doesn't need to iterate either (nor do most (all?) of the standard containers...) – Oliver Charlesworth Jul 25 '15 at 17:17
  • 2
    His idea is not so weird. Properly support of `UTF-8` in `std::string` which - oh, my! - does not exist (while `char*` on *nix platforms is by default `utf8`-encoded), would indeed require iterating. @ronilp You can think of `std::string` simply as of `std::vector`. `length()` has `O(1)` and simply returns `m_Length` or something like that. – Mateusz Grzejek Jul 25 '15 at 17:17
  • @Oliver: since C++11, `size()` is guaranteed to be `O(1)`, so yes all containers. This required a change to the glibc implementation of `std::list`. – Steve Jessop Jul 25 '15 at 17:42
  • It is faster to type 'size' than 'length' since it has 2 less characters. – jacknad May 30 '23 at 14:09

4 Answers4

35

Both have the same complexity: Constant.

From the N4431 working draft, §21.4.4

size_type size() const noexcept;

Returns: A count of the number of char-like objects currently in the string. Complexity: Constant time.

And

size_type length() const noexcept;

Returns: size().


[...] iterates through all the characters and counts the length [...]

That's C strings you're thinking of.

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
18

If you take a look at documentation here it says that length and size are the same.

Both string::size and string::length are synonyms and return the same value.

Also if you take a look at the code, length is cached, so the complexity is O(1). (Code from MS implementation but I'm sure other libraries are done the same way.)

size_type length() const _NOEXCEPT
    {   // return length of sequence
    return (this->_Mysize);
    }

size_type size() const _NOEXCEPT
    {   // return length of sequence
    return (this->_Mysize);
    }
Jaka Konda
  • 1,385
  • 3
  • 13
  • 35
2

Both the functions for a string in C++ are exactly the same and Returns the number of characters in the string, not including any null-termination.

source

Source: basic_string.h

Sanjeev Sharma
  • 547
  • 1
  • 5
  • 13
0

They are equivalent. Also, strings don't count characters when returning size, that's char arrays, if strings always counted characters then they would be too slow.