vector内数据的深拷贝和浅拷贝
来源:互联网 发布:win7 查看 端口占用 编辑:程序博客网 时间:2024/06/13 20:57
结论: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 0
- vector内数据的深拷贝和浅拷贝
- c++中 拷贝构造函数的深拷贝和浅拷贝--“浅拷贝”与“深拷贝”
- Python对象的拷贝,浅拷贝和深拷贝。
- Java的深拷贝和浅拷贝
- Java的深拷贝和浅拷贝
- C++的浅拷贝和深拷贝
- C++的浅拷贝和深拷贝
- Java的深拷贝和浅拷贝
- Java的浅拷贝和深拷贝
- python 的深拷贝和浅拷贝
- Java的深拷贝和浅拷贝
- Java的深拷贝和浅拷贝
- Java的深拷贝和浅拷贝
- Java的深拷贝和浅拷贝
- Java的深拷贝和浅拷贝
- Python的浅拷贝和深拷贝
- Java的深拷贝和浅拷贝
- 深拷贝和浅拷贝的区别
- Bootstrap学习笔记01
- 清空mysql 查询缓存的可行方法
- 2015.3cvte一面及笔试--长沙校招实习生
- SSL TLS 学习之一:对称加密和非对称加密
- 规则 2.4:缩进统一为4个空格,不允许使用TAB键。
- vector内数据的深拷贝和浅拷贝
- oracle优化器
- MySQL 时间函数小总结
- 关于有道云笔记
- TCP协议三次握手过程分析(转载)
- ipvsadm/keepalived 安装
- 规则 2.5:对于insert into…values,insert into…select语句,在字段不多的情况下,尽量一行只写一个字段。
- C语言各种变量存储方式
- 【转载】上海北京天津历年GDP和人均GDP一览(1978—2011)