#include <iostream>
#include <string>
using namespace std;
int main () {
  string str = "Hello";
  str = "Hello World";
  cout<<str<<endl;
}
How is the memory handled by the string?
#include <iostream>
#include <string>
using namespace std;
int main () {
  string str = "Hello";
  str = "Hello World";
  cout<<str<<endl;
}
How is the memory handled by the string?
 
    
    Re:
” How is the memory handled by the string?
Automagically.
That means, among other things, that there's no way to give a std::string, an externally created buffer. So it's a bit inefficient. On the bright side, the swap requirements for std::string (as opposed to std::vector) means that it can use the small buffer optimization, where short strings are stored without dynamic allocation, which helps to improve efficiency.
