Is std::string size() a O(1) operation?
The implementation of STL I’m using is the one built into VC++
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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: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 constantstring::size().