vector内数据的深拷贝和浅拷贝

结论:vector内数据使用结构体的话是深拷贝,vector内的数据会拷贝一份保存,vector内数据不会丢失。如果vector内数据是指针的话是进行浅拷贝,数据超出作用域后会自动析构,vector内所指向的数据会被更改和丢失,所以vector如果作为全局变量,不应该使用指针。

#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;


struct OtherDevice{
	map<string,string> datas;
};

vector<OtherDevice> 
otherDevices;//全局变量  vector内数据进行值得深拷贝 如果是指针的话进行浅拷贝,数据超出作用域后会自动析构,所以vector如果作为全局变量,不应该使用指针。
//otherDevices能够保存数据
int main( int argc, char* argv[] )
{
	for (int j=0;j<10;j++)
	{	
		char buffer[1024];
		sprintf(buffer,"hehe_%d",j);
		OtherDevice tmpDevice;
		tmpDevice.datas.insert(std::pair<std::string,std::string>("device_id",string(buffer)));
		otherDevices.push_back(tmpDevice);
	}
	
	//tmpDevice.datas.clear(); 不能清空,否则全局变量otherDevices数据丢失
	printf("otherDevices.size=%d ",otherDevices.size());
	if(otherDevices.size()>0)
	{
		printf("otherDevices.datas.size=%d ",otherDevices.at(0).datas.size());
	}else{
		printf("otherDevices.datas.size=0 ");
	}
	otherDevices.clear();

	return 0;
}
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;


vector<map<string,string>> otherDevices;//全局变量  vector内数据进行值得深拷贝 如果是指针的话进行浅拷贝,数据超出作用域后会自动析构,所以vector如果作为全局变量,不应该使用指针。
//otherDevices能够保存全局数据
int main( int argc, char* argv[] )
{
	for (int j=0;j<10;j++)
	{		
		char buffer[1024];
		sprintf(buffer,"hehe_%d",j);
		map<string,string> datas;
		datas.insert(std::pair<std::string,std::string>("device_id",string(buffer)));
		otherDevices.push_back(datas);
	}	
	printf("otherDevices.size=%d ",otherDevices.size());
	if(otherDevices.size()>0)
	{
		printf("otherDevices.datas.size=%d ",otherDevices.at(0).size());
	}else{
		printf("otherDevices.datas.size=0 ");
	}
	otherDevices.clear();

	return 0;
}

#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;

struct OtherDevice{
	map<string,string> datas;
};
static vector<OtherDevice*> otherDevices;//static全局变量  防止vector内数据超过作用域自动析构
//otherDevices不能够保存数据
int main( int argc, char* argv[] )
{
	for (int j=0;j<10;j++)
	{		
		char buffer[1024];
		sprintf(buffer,"hehe_%d",j);
		OtherDevice tmpDevice;
		tmpDevice.datas.insert(std::pair<std::string,std::string>("device_id",string(buffer)));
		otherDevices.push_back(&tmpDevice);
	}	
	//tmpDevice.datas.clear(); 不能清空,否则全局变量otherDevices数据丢失
	printf("otherDevices.size=%d ",otherDevices.size());
	if(otherDevices.size()>0)
	{
		printf("otherDevices.datas.size=%d ",otherDevices.at(0)->datas.size());
	}else{
		printf("otherDevices.datas.size=0 ");
	}
	otherDevices.clear();
	return 0;
}
#include <iostream>
#include <vector>
#include <map>
#include <string>
using namespace std;

vector<map<string,string>*> otherDevices;
//otherDevices不能够保存数据
int main( int argc, char* argv[] )
{	
	for (int j=0;j<10;j++)
	{	
		char buffer[1024];
		sprintf(buffer,"hehe_%d",j);
		map<string,string> datas;
		datas.insert(std::pair<std::string,std::string>("device_id",string(buffer)));
		otherDevices.push_back(&datas);
	}
	printf("otherDevices.size=%d ",otherDevices.size());
	if(otherDevices.size()>0)
	{
		printf("otherDevices.datas.size=%d ",otherDevices.at(0)->size());
	}else{
		printf("otherDevices.datas.size=0 ");
	}
	otherDevices.clear();

	return 0;
}




  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值