这是排序ptr_vector的正确方法吗? [英] Is this the correct way to sort ptr_vector?
问题描述
我试图使用boost的指针容器,并应用STL算法。我写了一段代码来对 ptr_vector< Point>
进行排序,其中 Point
是成员 int x,y
。代码如下:
#include< iostream>
#include< iterator>
#include< algorithm>
#include< boost / ptr_container / ptr_list.hpp>
#include< boost / ptr_container / ptr_vector.hpp>
using namespace std;
struct Point {
int x,y;
Point(int xx,int yy):x(xx),y(yy){
cout< Point:<< x<< < y < endl;
}
〜Point(){
cout<< 〜Point:<< x<< < y < this:<这种<< endl;
}
};
struct ComparePoint {
bool operator()(const Point& p1,const Point& p2){
return(p1.x + p1.y& p2.y);
}
};
struct PrintPoint {
bool operator()(const Point& p){
cout< p.x<< < p<< endl;
}
};
int main(){
boost :: ptr_vector< Point> v;
v.push_back(new Point(1,3));
v.push_back(new Point(2,0));
v.push_back(new Point(3,4));
v.push_back(new Point(4,1));
// sort(v.begin(),v.end(),ComparePoint());
for_each(v.begin(),v.end(),PrintPoint());
return 0;
}
您可能注意到我注释行sort(v.begin v.end(),ComparePoint()),在这种情况下,输出(cout)看起来正常,如下所示。
点:1 3
,ComparePoint()),cout输出如下:
点数:2 0
点数:3 4
点数:4 1
1 3
2 0
3 4
4 1
〜Point:1 3 this:0x1d3f010
〜Point:2 0 this:0x1d3f050
〜Point:3 4 this:0x1d3f030
〜Point:4 1 this:0x1d3f070但是,当我取消注释行sort(v.begin(),v.end())时,会出现这样的情况:
点:1 3
点:2 0
Point:3 4
Point:4 1
〜Point:2 0 this:0x7fff3f723970
〜Point:3 4 this:0x7fff3f723960
〜Point:3 4 this :0x7fff3f723970
〜Point:4 1 this:0x7fff3f723960
〜Point:4 1 this:0x7fff3f723970
2 0
1 3
4 1
3 4
〜Point:2 0 this:0x1e45010
〜Point:1 3 this:0x1e45050
〜Point:4 1 this:0x1e45030
〜Point:3 4 this:0x1e45070
根据输出,排序是确定的,但是有额外的5次析构函数调用。这是从哪里来的?更有趣的是,如果我改变sort到stable_sort,输出如下:
点:1 3
Point:2 0
Point:3 4
Point:4 1
〜Point:2 0 this:0x7fffcbe85140
〜Point:4 1 this:0x7fffcbe85140
〜Point :2 0 this:0x26010c0
〜Point:1 3 this:0x26010c8
〜Point:1 3 this:0x26010d0
〜Point:1 3 this:0x26010d8
2 0
1 3
4 1
3 4
〜Point:2 0 this:0x2601010
〜Point:1 3 this:0x2601050
〜Point:4 1 this :0x2601030
〜Point:3 4 this:0x2601070
的Point从堆栈释放,其他4从堆释放。我恐怕使用指针容器的算法因为这个奇怪的行为?你知道如何解释这是或这是正确的方式排序ptr_vector或其他顺序指针容器?
解决方案似乎你使用
std :: sort
这将交换元素,从而引起对析构函数的调用。boost :: ptr_containers
documentation 状态不幸的是,使用标准库中的变异算法不可能使用指针容器,但最有用的是作为成员函数提供: 。如果你调用ptr_vector.sort()
,而我怀疑你会看到没有调用析构函数。I am trying to use boost's pointer container, and apply STL algorithms to it. I wrote a piece of code to sort a
ptr_vector<Point>
in whichPoint
is a class with membersint x, y
. The code is as follows:#include <iostream> #include <iterator> #include <algorithm> #include <boost/ptr_container/ptr_list.hpp> #include <boost/ptr_container/ptr_vector.hpp> using namespace std; struct Point { int x, y; Point(int xx, int yy) : x(xx), y(yy) { cout << "Point: " << x << " " << y << endl; } ~Point() { cout << "~Point: " << x << " " << y << " this: " << this << endl; } }; struct ComparePoint{ bool operator() (const Point& p1, const Point& p2) { return (p1.x + p1.y < p2.x + p2.y); } }; struct PrintPoint{ bool operator() (const Point& p) { cout << p.x << " " << p.y << endl; } }; int main() { boost::ptr_vector<Point> v; v.push_back(new Point(1,3)); v.push_back(new Point(2,0)); v.push_back(new Point(3,4)); v.push_back(new Point(4,1)); //sort(v.begin(), v.end(), ComparePoint()); for_each(v.begin(), v.end(), PrintPoint()); return 0; }
You may notice that I comment line "sort(v.begin(), v.end(), ComparePoint())", in this case the output (cout) looks normal, as follows.
Point: 1 3 Point: 2 0 Point: 3 4 Point: 4 1 1 3 2 0 3 4 4 1 ~Point: 1 3 this: 0x1d3f010 ~Point: 2 0 this: 0x1d3f050 ~Point: 3 4 this: 0x1d3f030 ~Point: 4 1 this: 0x1d3f070
However, when I uncomment the line "sort(v.begin(), v.end(), ComparePoint())", the cout output is as follows:
Point: 1 3 Point: 2 0 Point: 3 4 Point: 4 1 ~Point: 2 0 this: 0x7fff3f723970 ~Point: 3 4 this: 0x7fff3f723960 ~Point: 3 4 this: 0x7fff3f723970 ~Point: 4 1 this: 0x7fff3f723960 ~Point: 4 1 this: 0x7fff3f723970 2 0 1 3 4 1 3 4 ~Point: 2 0 this: 0x1e45010 ~Point: 1 3 this: 0x1e45050 ~Point: 4 1 this: 0x1e45030 ~Point: 3 4 this: 0x1e45070
According to the output, the sort is ok, but there are extra 5 times of destructor calls. Where is that from? More interesting thing is, if I change sort to stable_sort, the output is as follows:
Point: 1 3 Point: 2 0 Point: 3 4 Point: 4 1 ~Point: 2 0 this: 0x7fffcbe85140 ~Point: 4 1 this: 0x7fffcbe85140 ~Point: 2 0 this: 0x26010c0 ~Point: 1 3 this: 0x26010c8 ~Point: 1 3 this: 0x26010d0 ~Point: 1 3 this: 0x26010d8 2 0 1 3 4 1 3 4 ~Point: 2 0 this: 0x2601010 ~Point: 1 3 this: 0x2601050 ~Point: 4 1 this: 0x2601030 ~Point: 3 4 this: 0x2601070
From the output it seems two instances of Point are released from stack, other 4 from heap. I am afraid to use algorithm on pointer container because of this strange behavior? do you know how to explain this or is this the right way to sort ptr_vector or other sequential pointer containers?
解决方案It seems that you are using
std::sort
for sorting which would swap elements and thus cause calls to destructors.boost::ptr_containers
documentation states "Unfortunately it is not possible to use pointer containers with mutating algorithms from the standard library. However, the most useful ones are instead provided as member functions:". If you invokeptr_vector.sort()
instead I suspect you would see no calls to destructors.这篇关于这是排序ptr_vector的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!