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

就是这样

相关文章:

  • 2021-11-29
  • 2021-08-27
  • 2021-08-03
  • 2022-12-23
  • 2022-12-23
  • 2022-03-04
  • 2021-04-16
  • 2021-08-07
猜你喜欢
  • 2021-08-11
  • 2022-12-23
  • 2021-11-22
  • 2021-05-24
  • 2021-05-25
  • 2021-12-21
  • 2021-10-12
相关资源
相似解决方案