C++ sorting Vector help

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
trixs
Posts: 26
Joined: Fri Jan 11, 2008 5:19 am
Location: Georgia
Contact:

C++ sorting Vector help

Post by trixs »

Hi,

I am having troubles with sort in C++.. Have been trying to get this to work for multiple hours and am getting no where. I do not understand how to actually get the sort algorithm predicate function to work.

Anyway heres an example of what I am trying to do:

Code: Select all


class test {
int a;
int b;
int c;
}


Now I use that class as an object to fill up the a vector. I want to actually sort them by int A but cant figure out the syntax.

Code: Select all


sort ( bleh.begin(), bleh.end(), test);

// Ok - so test would need to be a predicate function

bool test (const test& p1, const test& p2)
{
 
    return p1.a < p2.a;
}

No matter how I try to do it It always fails:
function call missing argument list

Of course it doesnt have an argument list because I dont know that argument as I am trying to sort the vector.

Anyway, I am completely lost here and really could use some help.

Thanks,

trixs
Morgawr
Posts: 95
Joined: Wed Sep 26, 2007 7:04 pm

Re: C++ sorting Vector help

Post by Morgawr »

trixs wrote:Hi,

I am having troubles with sort in C++.. Have been trying to get this to work for multiple hours and am getting no where. I do not understand how to actually get the sort algorithm predicate function to work.

Anyway heres an example of what I am trying to do:

Code: Select all


class test {
int a;
int b;
int c;
}


Now I use that class as an object to fill up the a vector. I want to actually sort them by int A but cant figure out the syntax.

Code: Select all


sort ( bleh.begin(), bleh.end(), test);

// Ok - so test would need to be a predicate function

bool test (const test& p1, const test& p2)
{
 
    return p1.a < p2.a;
}

No matter how I try to do it It always fails:
function call missing argument list

Of course it doesnt have an argument list because I dont know that argument as I am trying to sort the vector.

Anyway, I am completely lost here and really could use some help.

Thanks,

trixs
wait.. should a,b and c declared public? I'm not sure if I'm right (Since I always put private/protected/public tags) but I think that attributes are automatically set to private if there's nothing to identify them...

try
class OBJ{
public:
int a,b,c;
};
trixs
Posts: 26
Joined: Fri Jan 11, 2008 5:19 am
Location: Georgia
Contact:

Post by trixs »

Thanks for reply!

Yes, they should be declared public. I just forgot to put public: when I wrote that example. Could always use a struct and not worry about it though.

Anyway, unfortunately thats not the issue. Although I wish it was!
Ico
Posts: 289
Joined: Tue Aug 22, 2006 11:35 pm

Post by Ico »

You can't use "test" for both the class and the function name (not sure if that's really the case). Also function pointers should be written with an & right before the name.
Saturn
Posts: 418
Joined: Mon Sep 25, 2006 5:58 pm

Post by Saturn »

Code: Select all

struct Test
{
   bool operator()(const test& p1, const test& p2)
   {
      return p1.a < p2.a;
   } 
};

std::sort(bleh.begin(), bleh.end(), Test());
You have to use function objects, which simply are classes with an overloaded operator().
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

You have two ways to do that. Using function objects like Saturn recommended (just I think it needs to be const correct) or overloading the operator<.

Example:

Code: Select all

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

class Foo
{
public:
	Foo(int a_) : a(a_) {}
	bool operator<(const Foo& f) const
	{
		return a < f.a;
	}
	int a;
};

class SortReverse
{
public:
	int operator() ( const Foo& f1, const Foo& f2 ) const
	{
		return f1.a > f2.a;
	}
};

int main()
{
	std::vector<Foo> foos;
	foos.push_back( Foo(1) );
	foos.push_back( Foo(5) );
	foos.push_back( Foo(3) );
	
	sort(foos.begin(), foos.end());
	for (int i=0; i < foos.size(); ++i )
	{
		std::cout << foos[i].a;
	}
	
	sort(foos.begin(), foos.end(), SortReverse() );
	for (int i=0; i < foos.size(); ++i )
	{
		std::cout << foos[i].a;
	}
	return 0;
};

IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
trixs
Posts: 26
Joined: Fri Jan 11, 2008 5:19 am
Location: Georgia
Contact:

Post by trixs »

Thank you for the examples cutealien and saturn. Your examples helped me figure out the correct syntax after about 10 frustrating hours for trying! Hopefully Ill never have to use a sort algo again as I really don't understand the concept and have no more desire!!

Thanks Again,
trixs
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

BTW, you were on the right track with your original function test, and you don't need to explicitly write your own function object adapter. The Standard C++ Library provides function object adapters for ordinary functions [both member and global functions with up to two arguments bound at call time].

Using your original code, this would sort the vector like this...

Code: Select all

std::sort (bleh.begin (), bleh.end (), std::ptr_fun (test));
Travis
Post Reply