Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the BigO time complexity of modifying a string in C++?

Tags:

c++

string

big-o

What is the runtime of modifying a std::string? I assume constant time or is there something more going on under the hood in the string class?

#include <string>

int main() {
  std::string str = "Hello World!"
  str[1] =  'a';  // <--- what is the runtime?
}

Cheers

like image 929
Matt Avatar asked Sep 18 '25 06:09

Matt


1 Answers

That's right, it's constant. According to this:

Complexity

Constant.

For C++11 that is, it's unspecified for C++98, but I wouldn't assume that an implenetation with non-constant std::string::operator[] would be commonplace.

like image 121
Blaze Avatar answered Sep 20 '25 19:09

Blaze