By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

multiset example

P: n/a
I can't find a clear example showing me how to use multiset with a
custom compare function.
(like here: http://www.codeproject.com/vcpp/stl/setandmap.asp - I have
no idea why they have the 'struct gtrst', and why the set is than
initialized 'set<char gtstr*,> setString2')...

My problem is simple - I have a simple class

class CMember
{
int fitness;
int somedata[1000];
....
};

I want to have a multiset that keeps members sorted according to
fitness. How to do it in a simple, self-explaining way?
Jacek
Nov 8 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hope this helps...
class CMember
{
private:
int fitness;
public:
CMember(int n)
{
fitness = n;
}

bool operator < (const CMember& refParam) const
{
return (this->fitness < refParam.fitness);
}
};
int main()
{
CMember o1(2), o2(3), o3(4);

multiset<CMember> myset;
myset.insert(o1);
myset.insert(o2);
myset.insert(o3);
}

the trick is to override the < operator, which is used by stl for
comparing contents of a container

Nov 8 '05 #2

P: n/a
am***********@gmail.com wrote:
Hope this helps... the trick is to override the < operator, which is used by stl for
comparing contents of a container


Thanks, thats exactly what I was looking for.
Jacek
Nov 10 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.