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 which `Point` is a class with members `int 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?