Hi,
I spent a lot of hours, why this code is wrong.
This code compiles good, but execution causes errors (during sorting objects).
Environment: (MinGW WinXP or VS 2008)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Edge1 {
int v_,w_,weight_;
public:
Edge1(int v, int w, double weight) {
v_ = v;
w_ = w;
weight_ = weight;
};
~Edge1();
int v() const {
return v_;
};
int w() const {
return w_;
};
int weight() const{
return weight_;
};
};
bool sortingFunction(const Edge1 *e1, const Edge1 *e2) {
return e1->weight() <= e2->weight();
}
int main() {
vector<Edge1*> v;
v.push_back( new Edge1(0,6,3) );
v.push_back( new Edge1(0,1,82) );
v.push_back( new Edge1(1,9,73) );
v.push_back( new Edge1(1,8,91) );
v.push_back( new Edge1(2,3,15) );
v.push_back( new Edge1(2,7,99) );
v.push_back( new Edge1(3,5,90) );
v.push_back( new Edge1(3,0,18) );
v.push_back( new Edge1(4,0,63) );
v.push_back( new Edge1(4,3,36) );
v.push_back( new Edge1(5,1,44) );
v.push_back( new Edge1(5,4,82) );
v.push_back( new Edge1(6,4,15) );
v.push_back( new Edge1(6,7,17) );
v.push_back( new Edge1(8,3,73) );
v.push_back( new Edge1(8,2,13) );
v.push_back( new Edge1(9,4,66) );
v.push_back( new Edge1(9,0,3) );
cout << "Start sorting..." << endl;
sort(v.begin(), v.end(),sortingFunction);
cout << "Stop sorting" << endl; // in this case never happened
return 0;
}
Thank you veru much
tommy