如何定义一个智能指针指向一个vector,以存int 为例

toadzw 2008-11-06 04:37:18
如何定义一个智能指针指向一个vector,以存int 为例
...全文
359 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fenghuijun135 2008-11-06
  • 打赏
  • 举报
回复
auto_ptr< vector<int> > av(new vector<int>());
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 glacier3d 的回复:]
引用 1 楼 jack_van 的回复:
vector <int> ivec;
ivec.push_back(5);
.....
auto_ptr < vector <int> > ap(&ivec);


auto_ptr 跟智能指针是两马事,像boost库里的share_ptr才是智能指针
[/Quote]
auto_ptr就是智能指针
jack_van 2008-11-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 glacier3d 的回复:]
引用 1 楼 jack_van 的回复:
vector <int> ivec;
ivec.push_back(5);
.....
auto_ptr < vector <int> > ap(&ivec);


auto_ptr 跟智能指针是两马事,像boost库里的share_ptr才是智能指针
[/Quote]

Thanks.
xuedaoli 2008-11-06
  • 打赏
  • 举报
回复
不建议auto_ptr和容器一起使用
因为auto_ptr管理的对象,存在着所有权转移问题。
auto_ptr<T> s = auto_ptr<T> t;
上面的这句就把t对象的所有权转移到了t身上,你如果再去用t就是错误的。
Oversense 2008-11-06
  • 打赏
  • 举报
回复
typedef vector<int> vector_int;

shared_ptr<vector_int>
liumingrong 2008-11-06
  • 打赏
  • 举报
回复
auto_ptr< vector<int> > av(new vector<int>());
av->push_back(1);
av->push_back(2);
glacier3d 2008-11-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jack_van 的回复:]
vector <int> ivec;
ivec.push_back(5);
.....
auto_ptr < vector <int> > ap(&ivec);
[/Quote]

auto_ptr 跟智能指针是两马事,像boost库里的share_ptr才是智能指针
liumingrong 2008-11-06
  • 打赏
  • 举报
回复
code=C/C++]auto_ptr< vector<int> > av(new vector<int>());
av->push_back(1);
av->push_back(2);[[/code]
jack_van 2008-11-06
  • 打赏
  • 举报
回复
vector<int> ivec;
ivec.push_back(5);
.....
auto_ptr< vector<int> > ap(&ivec);

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧