Archived

This topic is now archived and is closed to further replies.

C++ Sort problems

This topic is 5016 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I have this piece of code:
#include "stdafx.h"
#include <iostream.h>
#include <vector>
#include <algorithm>

using namespace std;

class meinComparator
{
public:
	bool operator() ( int x, int y)
	{
		return true;
	}
};

int main(int argc, char* argv[])
{
	vector v;
	for(int i=0;i<100;i++)
	{
		v.push_back(i);
	}
	meinComparator comp;
	std::sort(v.begin(), v.end(), comp);
	for(i=0;i<10;i++)
	{
		cout << v.at(i) << endl;
	}
	return 0;
}
 
Can someone explain to me why this crashes Inside sort ? Thx, Wintermute

Share this post


Link to post
Share on other sites
I''d guess it''s being confused by your comparator. The comparison function is assumed to obey strict weak ordering. Try changing it to something reasonable and see what happens.

Share this post


Link to post
Share on other sites
You''re right, but find this bug if your comparator is a little more complex !! I searched 6 hours, because I thought it was my failure (that comperator didn''t only return true).

Those guys at HP are really lazy.

And why ain''t there a big remark "BE CAREFUL WHILE CREATING THE COMPARATOR...." ???

ARGL, I''m really upset with that.

Wintermute

Share this post


Link to post
Share on other sites