Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old February 26th, 2007, 11:55 PM
=?iso-8859-1?q?Jo=E3o_Correia?=
Guest
 
Posts: n/a
Default Order items within multiset AND Find object on multiset

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!

  #2  
Old February 27th, 2007, 12:05 AM
Mark P
Guest
 
Posts: n/a
Default Re: Order items within multiset AND Find object on multiset

Joćo Correia wrote:
Quote:
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.
Show us what you've tried.
Quote:
>
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
  #3  
Old February 27th, 2007, 01:15 AM
Piyo
Guest
 
Posts: n/a
Default Re: Order items within multiset AND Find object on multiset

Joćo Correia wrote:
Quote:
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!
>
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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles