473,763 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL Multiset with Class Objects sorted based on its member variable

Digital Don
19 New Member
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;

where Event is a class with the variables in it

"processID
Time
typeofEvent"

How do I specify that the class objects in the multiset be sorted based on the TIME value of the class object?

Thank You in Advance.
Mar 5 '08 #1
4 2418
Laharl
849 Recognized Expert Contributor
You can overload operator< for your class. That is, you write a function with the signature
Expand|Select|Wrap|Line Numbers
  1. bool operator<(const Event& other)
that returns based on whether the caller object's time value (use the 'this' pointer if you want) is less than the passed in object's time value.
Mar 5 '08 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
You can also create your multiset using a 3rd template parameter for the address of the function you want to use to order your objects.

This avoids implementing a class operator< when you don't really need it.
Mar 5 '08 #3
Digital Don
19 New Member
Can u explain or provide a link to the 3rd template of Multiset?

Thank You.


You can also create your multiset using a 3rd template parameter for the address of the function you want to use to order your objects.

This avoids implementing a class operator< when you don't really need it.
Mar 7 '08 #4
Laharl
849 Recognized Expert Contributor
The STL declaration of multiset is
Expand|Select|Wrap|Line Numbers
  1. multiset<class Key, class Comparator = less<Key>, class Allocator = Allocator<Key> >
Thus, it's the second parameter you're interested in here, not the third. This works because the name of a function acts as a pointer to that function.

Expand|Select|Wrap|Line Numbers
  1. class foo{
  2.  private
  3.   int bar;
  4.  //...
  5. }
  6.  
  7. bool myfunc(const foo& one, const foo& two){
  8.  return one.bar<two.bar;
  9. }
  10.  
  11. multiset<foo, myfunc> myset;
  12.  
Mar 7 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1864
by: Robert Mark Bram | last post by:
Hi All! Instead of calling an object definition a class in JavaScript, should we say that it is a type or definition or something like this? So I might say "The object type is defined by its constructor." Thanks for any advice! Rob
42
3205
by: Dan | last post by:
Hello, I have trouble with class calling. I am calling getvolume() with succes in the function CreateCircle but it do not want to call it in ShowCircle() function. I am staying in the same class. I thought that was the point of encapsulation. When the function ShowCircle() is called I get very large number -1.07374e+008 can anyone help me ?
10
2922
by: Arthur J. O'Dwyer | last post by:
I'm seeing a bug at the moment that I can't track down. It's part of a moderately large program, but here is a small program that exhibits the bug on gcc. (The program code follows at the bottom of the message.) The symptom is this: % g++ --version g++ (GCC) 3.2.1 % g++ -W -Wall -pedantic test.cpp
2
12497
by: gacek | last post by:
I can't find a clear example showing me how to use multiset with a custom compare function. (like here: http://www.codeproject.com/vcpp/stl/setandmap.asp - I have no idea why they have the 'struct gtrst', and why the set is than initialized 'set<char gtstr*,> setString2')... My problem is simple - I have a simple class class CMember {
5
2425
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.
37
2607
by: Joergen Bech | last post by:
(Slightly religious question): Suppose I have the following class: ---snip--- Public Class MyClass Private _MyVariable As Integer Public Property MyVariable() As Integer Get
9
3388
by: neil.johnston | last post by:
I have a cut down example program that uses multiset to order some data. The data arrives from various sources and has a time stamp, data with identical timestamps can arrive and due to fifo's and block sizes data from one source with a later time stamp may arrive before data from another source with and earlier time stamp. Looking at the STL i thought multiset was just the job for this. The idea was to set a window of x seconds and read...
3
2171
by: mweltin | last post by:
I have a derived class that extends a base class by adding a float member. The strange part is when I use the STL sort algorithm on a vector of derived classes, only hte float member is sorted. For example assume a vector containg three derived class objects called blah. blah { ... int one = 7 , int two = 5, int three =8 , float five = 1} blah { ... int one = 6 , int two = 6, int three =4 , float five = 0} blah { ... int one = 5 , int...
2
1992
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;
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10145
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...
1
9938
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
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
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...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
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
3523
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
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.