鱼C论坛

 找回密码
 立即注册
查看: 2049|回复: 4

[已解决]c++string类的疑惑

[复制链接]
发表于 2018-2-19 15:17:14 | 显示全部楼层 |阅读模式
7鱼币
c++string类创建的字符串可以修改大小,大小修改过,后面的内存中的内容就不会覆盖吗,还是它是重新申请一块大的内存转移过去,亦或是在申请一块内存然后像链表一样链接起来,求教
最佳答案
2018-2-19 15:17:15
code:
#include<iostream>
#include<string>
using namespace std;
void main()
{
        string str1 = "this is str1", str2 = "str2";
        cout<<"str1:"<<&str1<<"  "<<"str2:"<<&str2<<endl;
        str2 = str1;
        cout<<"str1:"<<&str1<<"  "<<"str2:"<<&str2<<endl;
        str2 = "this is str2";
        cout<<"str1:"<<&str1<<"  "<<"str2:"<<&str2<<endl;
       
        system("pause");
        return;
}

//str1:00D3FD80  str2:00D3FD58
//str1:00D3FD80  str2:00D3FD58
//str1:00D3FD80  str2:00D3FD58

地址没有改变

最佳答案

查看完整内容

code: #include #include using namespace std; void main() { string str1 = "this is str1", str2 = "str2"; cout
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-19 15:17:15 | 显示全部楼层    本楼为最佳答案   
code:
#include<iostream>
#include<string>
using namespace std;
void main()
{
        string str1 = "this is str1", str2 = "str2";
        cout<<"str1:"<<&str1<<"  "<<"str2:"<<&str2<<endl;
        str2 = str1;
        cout<<"str1:"<<&str1<<"  "<<"str2:"<<&str2<<endl;
        str2 = "this is str2";
        cout<<"str1:"<<&str1<<"  "<<"str2:"<<&str2<<endl;
       
        system("pause");
        return;
}

//str1:00D3FD80  str2:00D3FD58
//str1:00D3FD80  str2:00D3FD58
//str1:00D3FD80  str2:00D3FD58

地址没有改变
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-19 16:09:05 | 显示全部楼层
能否用代码举个例子?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-21 16:30:50 | 显示全部楼层
自己去看string类的实现
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-7-15 16:34:36 | 显示全部楼层
Dude 发表于 2018-7-15 11:09
code:
#include
#include

谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 11:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表