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

Your problem is the equals sign (=) in your sort function.
You cannot e.g. this allows

Edge1 A(0,0,1);
Edge1 B(0,0,1); 

// This should not be true to use the algorithm sort.
sortFunction(&A,&B) == sortFunction(&B,&A)

Also please use a managed point with vectors, e.g. boost::shared_ptr.

commented: Good help. +0

Thank you StuXYZ! You are right.
I have changed the method:

bool sortingFunction(const Edge1 *e1, const Edge1 *e2) {
	return e1->weight() < e2->weight();
}

Thanks! :)

weight_ is an integer, but the parameter in the constructor has it as a double. Which is it???

The sortingFunction() function should use < operator, not <=.

[edit]Oops! already answered.

> weight_ is an integer, but the parameter in the constructor has it as a double. Which is it???

This is my mistake, should be int. Thanks Ancient Dragon.

Also whats with pointers? Be smarter about your code and use reference or smart pointers if you have to.