Problem with sorting objects in vector (STL)

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)

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#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 very much,
tommyz
Last edited on
Why does Edge1::weight() return double? Shouldn't it return int?
Last edited on
Thanks Kevin, Edge1::weight() should return int. I have changed this, but the error still exists.
Last edited on
What kind of error are you getting? Is the list just sorted incorrectly? Or is it something crazier?
The error is default windows error: "There appeared error with Test.exe application and it will be shut down. Sorry for problems"
hello..sry for not being able so help you,because i am a beginner and don't now all the things you wrote.(clas,public,functions from algorithm library )and what they do..but for me in situations like this it is a big help to try to debug it..now i see that u use a different ide from what i use..for example i use visual studio,and when i debug,i use f10 to run every line in the code manually step by step..and i watch on the console application(for example) what goes right and what not.and that gives me a small clue where,and what goes wrong..so try to make something like that with you're ide..hope u find the bug :)
vector<Edge1*> v;

Try removing the star on that line and see if it works.
I have debugged this code. The error is in the line 54. sort(v.begin(), v.end(),sortingFunction). In the last call of sortingFunction, one pointer is not valid, when e2->weight() was called this causes error. But the question is, why the pointer is not valid (this pointer is not NULL) ?
Maybe try calling sort(v.begin(), v.end()-1, sortingFunction)?
> Try removing the star on that line and see if it works.

It does not help. I need to have an vector of pointers to Edge1 class, and I want to sort this vector by weight.
> sort(v.begin(), v.end()-1, sortingFunction)?

This sorts all elements without last one. I want to sort all elements.
Last edited on
The function sortingFunction is not correct defined.

Correctly:
1
2
3
bool sortingFunction(const Edge1 *e1, const Edge1 *e2) {
	return e1->weight() < e2->weight();
}


And algorithm works good.

Thanks all!
Last edited on
Topic archived. No new replies allowed.