473,748 Members | 10,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2180
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 StrictWeakOrder ing :
public std::binary_fun ction<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<C Score,CScore::S trictWeakOrderi ngmyMultiSet;
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
2101
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 way .cpp files #include headers. My question is this: Is there a standard order in which heders should be included? I tend to #include STL headers first, and my own headers later, but I can't really think of a logical justification for this.
1
1512
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
7210
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
2424
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 wrote a method LocationSort(). I can't seem to arrive upon the correct syntax for defining a multiset that will use LocationSort. multiset<DotObject,DotObject::LocationSort> mySet; fails.
16
2449
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? -> array2 is assigned to array1 ....???? the reason being it is
2
2405
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 collection and print the documents that have been requested. The contents of the ListView are never sorted or manipulated in any way. Occasionally the documents print out of order. Is it possible that: For Each li as ListViewItem in...
5
3743
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 : ' Assume there is an 'items' array that contains information from which I draw ' Also assume I have a valid Graphics object named g Dim pts(5) As Point
2
1990
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< (const tools& lhs, const tools& rhs) { cout<<"lhs="<<lhs.company<<"."<<lhs.name <<" rhs="<<rhs.company<<"."<<rhs.name<<endl;
4
2416
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 Sort CLASS objects based on a class variable value in it . i.e. multiset <Event, less<Event> > _queue;
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.