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

时间:2021-08-27 19:52:53

结论: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;}