473,320 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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!

Feb 26 '07 #1
2 2155
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Chris Gordon-Smith | last post by:
I am currently revisiting the code of a C++ application and restructuring the code where appropriate to make it consistent with the overall logical design. As part of this, I am looking at the...
1
by: SUPER_SOCKO | last post by:
I am new to STL. I don't know how to access a multiset of a list. My code is the following: #include <list> #include <iostream> #include <set> using namespace std;
6
by: segue | last post by:
Hi; I need to change the order of a listbox array from a form app where I select order up or down. What's a good way to do that? Regards; Segue
5
by: Dale Marchand | last post by:
I'm trying to use an object in two different multiset containers, each with it's own sort method. For the most frequently used, I overrode the operator< method in the class, and for the second I...
16
by: mdh | last post by:
May I ask the group the following: (Again, alas , from K&R) This is part of a function: while ( ( array1 = array2 ) != '\0' ); /* etc etc */ Is this the order that this is evaluated? ...
2
by: Al Reid | last post by:
I'm using VB 2005. I have a production application where I load a ListView with information from several sources based on user interaction. At some point I need to iterate through the Items...
5
by: ibiza | last post by:
Hi all, I use VB.NET (C# would do the same) and GDI+ to draw on a form. I'd like to know if it is possible to set the z-order when we draw something...Let's consider that simplified case : '...
2
by: parvtb | last post by:
Thanks for your patience to read the entire post to understand my confusion I have the user-defined class "tools": class tools{ public: tools( ......) {} friend bool operator<...
4
Digital Don
by: Digital Don | last post by:
Hi, I was looking at the MultiSet STL structure with some "less" keyword. I was told that we can use the "MultiSet" STL structure to automatically sort the content. Is it possible to store and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.