Order items within multiset AND Find object on multiset

  • Thread starter =?iso-8859-1?q?Jo=E3o_Correia?=
  • Start date
?

=?iso-8859-1?q?Jo=E3o_Correia?=

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!
 
M

Mark P

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.

Show us what you've tried.
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.

Checking if find() returns end() will work, but you need to have a
working operator<() first. There's no point overloading operator==()
since the map /set family doesn't use this. From a map or set's point
of view, two keys are equal if neither is less than the other. That is,
A equals B if "A < B" and "B < A" both return false.

Mark
 
P

Piyo

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;
}
}
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top