multiset example

G

gacek

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
 
A

amit.arora.nd

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top