set::insert 是否保存副本或指针 C++
函数 set::insert
是否保存指向该元素或其副本的指针。意思是,我可以执行以下代码,还是必须确保指针没有被删除?
int *a;
*a=new int(1);
set<int> _set;
_set.insert (*a);
delete a;
*a=new int(2);
_set.insert (*a);
delete a;
我用 int
给出了示例,但我的实际程序使用我创建的类。
does the function set::insert
saves a pointer to the element or a copy of it. meaning, can I do the following code, or I have to make sure that the pointers are not deleted?
int *a;
*a=new int(1);
set<int> _set;
_set.insert (*a);
delete a;
*a=new int(2);
_set.insert (*a);
delete a;
I gave the example with int
, but my real program uses classes that I created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所有 STL 容器都存储插入数据的副本。请参阅此处第三段中的“描述”部分:容器(以及 < code>std::set 建模一个容器)拥有它的元素。有关更多详细信息,请参阅以下脚注 [1]。特别是对于
std::set
,请查看此处在“类型要求”部分下。Key
必须是可分配的。除此之外,您可以轻松测试这一点:
您将始终看到
复制构造!
。如果您确实想存储诸如对象引用之类的内容,您可以存储指向这些对象的指针:
但在这种情况下,您必须正确删除对象,这有时非常困难。例如,异常可能会极大地改变执行路径,并且您永远无法到达正确的
删除
。或者您可以使用像boost::shared_ptr这样的智能指针:
现在智能指针会照顾您并在正确的时间删除其引用的对象。如果您复制了插入的智能指针之一并将其传输到其他地方,则在引用该对象的最后一个智能指针超出范围之前,通常引用的对象将不会被删除。
哦,顺便说一句:永远不要使用 std::auto_ptr 作为标准容器中的元素。它们奇怪的复制语义与容器存储和管理数据的方式以及标准算法操作它们的方式不兼容。我确信 StackOverflow 上有很多关于这个不稳定问题的问题。
All STL containers store a copy of the inserted data. Look here in section "Description" in the third paragraph: A Container (and
std::set
models a Container) owns its elements. And for more details look at the following footnote [1]. In particular for thestd::set
look here under the section "Type requirements". TheKey
must be Assignable.Apart from that you can test this easily:
You'll always see
Copy construction!
.If you really want to store something like a reference to an object you either can store pointers to these objects:
But in this case you have to take care of deleting the objects correctly and this is sometimes very difficult. For example exceptions can change the path of execution dramatically and you never reach the right
delete
.Or you can use smart pointers like
boost::shared_ptr
:Now the smart pointers takes care for you and delete their referenced objects at the right time. If you copied one of the inserted smart pointers and transfered it somewhere else the commonly referenced object won't be delete until the last smart pointer referencing this object goes out of scope.
Oh and by the way: Never use
std::auto_ptr
s as elements in the standard containers. Their strange copy semantics aren't compatible with the way the containers are storing and managing their data and how the standard algorithms are manipulating them. I'm sure there are many questions here on StackOverflow concerning this precarious issue.std::set
将复制您插入的元素。std::set
will copy the element you insert.您正在将指针保存到集合中。
指针所指向的对象不会被复制。
因此,调用delete后,集合中的指针无效。
注意:您可能只想保存整数。
其他注意事项:
指针:
You are saving pointers into the set.
The object pointed at by the pointer is not copied.
Thus after calling delete the pointer in the set is invalid.
Note: You probably want to just save integers.
Couple of other notes:
Ptr:
此代码是错误的,因为您尝试使用存储在地址
a
中的值,该值是垃圾。而且,每个 stl 都包含
copy
元素,除非您使用带有 insert() 和 push_back() 的move
语义来获取 C++0x 中的右值引用。This code is wrong because you try to use the value stored at address
a
which is a garbage.And, every stl containers
copy
elements unless you usemove
semantics with insert() and push_back() taking rvalue references in C++0x.