After much searching, it seems that a pointer to a vector is not the best thing to do. However, the following code bugs me too much:
1 #include <stdio.h>
2 #include <algorithm>
3 #include <vector>
4
5
6 class Hdr
7 {
8 public:
9 std::vector<long> *order;
10 bool operator()(long i1, long i2) const;
11 Hdr(int N);
12 ~Hdr();
13 };
14
15 Hdr::Hdr(int N)
16 {
17 order = new std::vector<long>(N,0);
18 for(int k=0;k<N;k++) (*order)[k] = -k;
19 };
20
21 Hdr::~Hdr()
22 {
23 order->clear();
24 delete order;
25 };
26
27 bool Hdr::operator()(long i1, long i2) const
28 {
29 return (i1<i2);
30 };
31
32 int main(void)
33 {
34 Hdr mhdr(1000);
35 std::sort(mhdr.order->begin(),mhdr.order->end(),mhdr);
36
37 printf("value at 300 = %d\n",mhdr.order->at(300));
38 };
with gcc 4.3 on Linux, the executable gives "double free or corruption". So I commented out line 24, and it throws 'std::out_of_range'. Apparently, line 35 (sort) messes everything up when a dynamically allocated vector is passed to std::sort. Or I just had a big blunder somewhere. Please help!! If I change line 9 to std::vector order then things seem fine; however I can't keep wondering what went wrong with the pointer to vector.