C ++ STL std :: string of copy-on-write

std :: string is the underlying char array.

Std :: string when making an assignment, not a new piece of memory, but the memory of the newly created object reference the same char *. Wait until you need to modify the object of the time, and then copy it.

The following functions will trigger copy on write:

  • reverse()
  • resize()
  • []
  • assign

std :: string contains a reference count, a new copy of the object will only increase when the reference count. When the reference count is not 1, it means not exclusively, copy, re-apply for a new memory char array.

He published 187 original articles · won praise 14 · views 40000 +

Guess you like

Origin blog.csdn.net/LU_ZHAO/article/details/105011730