string小感

 string str1 = "123";
 
string str2 = str1;
  str1 = "456";

  cout << "str1 = " << str1 << endl // 輸出 456
     << "str2 = " << str2 << endl << endl; // 輸出 123


也就是说str1改变,str2并不改变,如果想str1与str2享用同一个字符串空间可以这么改一下:

    string str1 = "123";
    string& str2 = str1;
    str1 = "456";

    cout << "str1 = " << str1 << endl // 輸出 456
        << "str2 = " << str2 << endl << endl; // 輸出 456

就是这样

原文地址:https://www.cnblogs.com/xiangshancuizhu/p/1977832.html