Is std::string size() a O(1) operation?
The implementation of STL I'm using is the one built into VC++
Is std::string size() a O(1) operation?
The implementation of STL I'm using is the one built into VC++
If you're asking if MSVC's implementation of string::size() has constant complexity, then the answer is yes. But Don Wakefield mentioned Table 65 in 23.1 of the C++ Standard where it says that the complexity of size()
should follow what's said in 'Note A'. Note A says:
Those entries marked ‘‘(Note A)’’ should have constant complexity.
However, that does not mean that those entries shall have constant complexity. Standards use very specific terminology, and "should" means that it is not mandatory.
'Note A' was added to the standard specifically to appease those who believed that size()
should be allowed to have linear complexity so it would not be necessary to keep the size when the containers were modified.
So you can't rely on size()
having constant complexity, but I'm honestly not sure if there are any implementations that do not have a constant string::size()
.
O(1)
is not a requirement to all containers does not mean that it can be O(N)
in the containers, only that there can be at least one container for which it is non-constant, for example std::list<>
. –
Stratton std::string
requirements forbid this however (e.g. this seems to imply array subscripting can no longer be O(1)). –
Harkness std::basic_string<>::size()
it refers to the container requirements (i.e. recommended constant size), in the 11 standard the requirement is explicit: Complexity: constant time. –
Stratton basic_string
conforms to the requirements of a Sequence, as specified in (23.1.1)", and a Sequence is a type of container (23.1.1/1), and a container mandates constant-time begin()
and end()
(Table 65 in 23.1) Glad to hear they made it explicit in C++11. –
Harkness Here's an easy way to answer that question for msvc++.
Write some code in a project:
string happy;
happy.size();
Hilight the .size call, right-click, go to definition.
On my install (vs2005sp1) this sends me to xstring:1635, which looks like this:
size_type __CLR_OR_THIS_CALL size() const
{ // return length of sequence
return (_Mysize);
}
So it looks like the string has a member called _Mysize, and it's just returning that.
In other words, this is a O(1) implementation.
See Table 65 in Section 23.1 of the Standard. "a.size()" is listed as "(Note A)", which says that "Those entries ... should have constant complexity".
Section 21.3 says that strings conform to the requirements of a sequence (23.1), ipso facto, size() is constant time.
For a string, the size()
operation has to be constant for all string implementations that don't use ropes(1). There is no explicit requirement in the standard that requires the operation to be O(1)
, the closest is the generic requirement that size()
should be constant time, but that leaves room for any other complexity measure.
So why must it be O(1)?
This comes from the fact that the size cannot be calculated from the contents of the string itself. While in C you use a NUL terminator to determine the end of the string, in C++ NUL is as valid as any other character in the string. Since the size of the string cannot be calculated from the contents(2), it must be handled externally, independently of the actual size of the string.
(1) C++03 standard allows an implementation to use ropes as the implementation for strings, but the fact is that none of the current implementations of the standard libraries use them.
(2) If the implementation used ropes, the operation could depend on the size by means of the number of blocks from which the rope was constructed if the blocks were linked through a linked list or similar construct, or if they were allowed to have different sizes. But ropes are not used in any standard library implementation that I know of.
Performance is guaranteed by the STL to be at least O(N) for containers, however many containers including std::string can implement this as O(1) and will. Usually it'll either return a simple variable or do something like _End - _Begin and return that.
The complexity of size()
should follow 'Note A'. That means, it should have the constant complexity, ie O(1). Although, I am not sure of the implementation, but the iterators in C++ do have associated operations like begin() and end() that point to the STL containers. These operations have a constant time complexity as they are the container requirements. This implies that begin() - end()
also has constant complexity. That means, size()
is O(1) operation.
size_type __CLR_OR_THIS_CALL size() const
{ // return length of sequence
return (_Mysize);
}
So it eventually might be like this, but you can never be sure.
© 2022 - 2024 — McMap. All rights reserved.
(end() - begin())
. This is guaranteed to be [amortized] O(1) because bothbegin()
andend()
must be O(1) (Container requirements), string iterators are random-access (string requirements), andoperator-
must be O(1) for iterators that support it (iterator requirements). – Roeder