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

就是这样

posted @ 2011-03-09 09:06  hailong  阅读(214)  评论(2编辑  收藏  举报