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

Order items within multiset AND Find object on multiset

P: n/a
Expand|Select|Wrap|Line Numbers
  1. class CScore
  2. {
  3. public:
  4. int L;
  5. int C;
  6.  
  7. CScore(int l, int c)
  8. {
  9. L = l;
  10. C = c;
  11. }
  12.  
  13. bool operator < (const CScore& score) const
  14. {
  15. // What to do?
  16. }
  17. };
  18.  
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!

Feb 26 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Joćo Correia wrote:
Expand|Select|Wrap|Line Numbers
  1. class CScore
  2. {
  3.     public:
  4.         int L;
  5.         int C;
  6.         CScore(int l, int c)
  7.         {
  8.             L = l;
  9.             C = c;
  10.         }
  11.         bool operator < (const CScore& score) const
  12.         {
  13.             // What to do?
  14.         }
  15. };
  16.  

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
Feb 27 '07 #2

P: n/a
Joćo Correia wrote:
Expand|Select|Wrap|Line Numbers
  1. class CScore
  2. {
  3.     public:
  4.         int L;
  5.         int C;
  6.         CScore(int l, int c)
  7.         {
  8.             L = l;
  9.             C = c;
  10.         }
  11.         bool operator < (const CScore& score) const
  12.         {
  13.             // What to do?
  14.         }
  15. };
  16.  

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::StrictWeakOrderingmyM ultiSet;
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;
}
}
---------------------------------------------
This code prints:
(1,1) in set
(2,2) not in set
Feb 27 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.