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 ?