Possible Duplicate:
C++ string::find complexity
What is the time complexity of the find operation that comes built-in with the string library in STL?
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.
The Standard, §21.4.7.2, doesn’t give any guarantees as to the complexity.
You can reasonably assume
std::basic_string::findtakes linear time in the length of the string being searched in, though, as even the naïve algorithm (check each substring for equality) has that complexity, and it’s unlikely that thestd::stringconstructor will build a fancy index structure to enable anything faster than that.The complexity in terms of the pattern being searched for may reasonably vary between linear and constant, depending on the implementation.