João Correia said:
Code:
class CScore
{
public:
int L;
int C;
CScore(int l, int c)
{
L = l;
C = c;
}
bool operator < (const CScore& score) const
{
// What to do?
}
};
1st Question:
I've this object to insert on a multiset and I want it ordered
descendent by L and after that by C. I know I've to override operator
< but my tries aren't working to keep items listed by that order.
2nd Question:
Before insert I want to check if CScore exists on multiset.
(multiset.find(score) != multiset.end()) doesn't seem to work should I
overload == operator ?
They're equal when L and C match.
Waiting answers. Thanks in advanced!
This will help you understand how to use multiset
in your context.
HTH
-------------------------------------------------------
#include <iostream>
#include <utility>
#include <set>
using namespace std;
class CScore
{
public:
int L;
int C;
CScore(int l, int c)
{
L = l;
C = c;
}
// the ordering should be a functor not an operator<
// bool operator < (const CScore& score) const
// {
// // What to do?
// }
class StrictWeakOrdering :
public std::binary_function<CScore, CScore, bool>
{
public:
result_type operator()( first_argument_type const & a,
second_argument_type const & b )
{
if( a.L < b.L )
{
return true;
}
else if( a.L == b.L )
{
if( a.C < b.C )
{
return true;
}
}
return false;
}
};
};
int
main()
{
// note you need to explicitly pass in the ordering here
typedef std::multiset<CScore,CScore::StrictWeakOrdering> myMultiSet;
myMultiSet set;
set.insert( CScore( 1, 1 ) );
set.insert( CScore( 1, 1 ) );
set.insert( CScore( 1, 2 ) );
set.insert( CScore( 2, 1 ) );
if( set.find( CScore(1,1) ) == set.end() )
{
cerr << "(1,1) not in set" << endl;
}
else
{
cerr << "(1,1) in set" << endl;
}
if( set.find( CScore(2,2) ) == set.end() )
{
cerr << "(2,2) not in set" << endl;
}
else
{
cerr << "(2,2) in set" << endl;
}
}