由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Is it safe to use unique_ptr with STL container?
相关主题
underlying sort algorithm for SET in STL?Leetcode上面的"Search in rotated sorted array II"
容器中放置智能指针一问两行quicksort,不难些吧
A STL sorting algorithm problem请教unique_ptr vs auto_ptr
std::map 为什么没有排序呢Question on C++ Access Control (protected)
auto_ptr, algorithm 混用问题,大侠们救我。头疼死了!How to sort a map in C++ STL based on Value, instead of Key
我写的quick sort修改map的键值
c++里面有什么Container插入是最快的?c++ template question:
Forward declaration with unique_ptr问一个有关C++里面list的问题。
相关话题的讨论汇总
话题: ptr话题: stl话题: unique话题: container话题: safe
进入Programming版参与讨论
1 (共1页)
p****n
发帖数: 69
1
It is understood that auto_ptr should not be used with STL container.
For example, it is seriously wrong to call sort from header to
sort a vector>, if the implementation uses quick sort and a
pivot, a copy of some element, is used.
Unique_ptr has the same feature of taking ownership when copying as auto_ptr
. I know that unique_ptr's assignment operator uses rvalue reference, but
the same problem in the above example remains: the pivot takes ownership of
some element, which is subsequently destroyed.
Then the question is: is it safe to use unique_ptr with STL container?
I did some search, the answer seems to be yes, but I don't know why.
t****t
发帖数: 6806
2
yes, unique_ptr<> is container safe. the point is, if T is not movable, (e.g
. price of moving is too big), then you use a pointer for that.

ptr
of

【在 p****n 的大作中提到】
: It is understood that auto_ptr should not be used with STL container.
: For example, it is seriously wrong to call sort from header to
: sort a vector>, if the implementation uses quick sort and a
: pivot, a copy of some element, is used.
: Unique_ptr has the same feature of taking ownership when copying as auto_ptr
: . I know that unique_ptr's assignment operator uses rvalue reference, but
: the same problem in the above example remains: the pivot takes ownership of
: some element, which is subsequently destroyed.
: Then the question is: is it safe to use unique_ptr with STL container?
: I did some search, the answer seems to be yes, but I don't know why.

1 (共1页)
进入Programming版参与讨论
相关主题
问一个有关C++里面list的问题。auto_ptr, algorithm 混用问题,大侠们救我。头疼死了!
请问delete的问题我写的quick sort
STL 一问c++里面有什么Container插入是最快的?
anything like STL in C?Forward declaration with unique_ptr
underlying sort algorithm for SET in STL?Leetcode上面的"Search in rotated sorted array II"
容器中放置智能指针一问两行quicksort,不难些吧
A STL sorting algorithm problem请教unique_ptr vs auto_ptr
std::map 为什么没有排序呢Question on C++ Access Control (protected)
相关话题的讨论汇总
话题: ptr话题: stl话题: unique话题: container话题: safe