473,804 Members | 3,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting a map<T*>

I have a map<T*that stores pointers to objects. How can I tell map<T*to
use the objects' operator<() and not the value of the pointers for sorting?

If that's not feasible, what alternatives do I have to sort a set of objects
that cannot and should not be copied (i.e. I can use only pointers to these
objects)?

Note: I am aware of the method of creating a new class that wraps the
pointers of the objects and then provides its own operator<(). However, I
am trying to avoid creating new classes.

May 7 '07
16 2008
On Tue, 08 May 2007 02:24:56 +0200, Sylvester Hesp wrote:
"Markus Schoder" <a3************ *@yahoo.dewrote in message
news:pa******** *************@y ahoo.de...
>On Mon, 07 May 2007 19:45:00 -0400, Pete Becker wrote:
>>Markus Schoder wrote:
Actually you need a class with an appropriate operator(). A plain
function won't do here since you cannot pass it as a template
parameter.
Sure you can. Try it.

No you cannot. I guess you are confusing this with algorithms like
std::lower_bou nd where you can specify a predicate as a _function_
parameter. As a template parameter for std::set or std::map this does
not work. Think about it -- it needs to be a type.

And at what point in time did functions stop having types? :)

bool mycompfunc(int, int);
std::set<int, bool(*)(int, int)myset(mycom pfunc);
Ah, I did not realise that the set is actually initialised with the
compare object. For some reason I thought it does Compare()(x, y) with
the comparison type.

--
Markus
May 8 '07 #11
Markus Schoder wrote:
On Mon, 07 May 2007 19:45:00 -0400, Pete Becker wrote:
>Markus Schoder wrote:
>>On Mon, 07 May 2007 16:11:52 -0700, Michael wrote:
On May 7, 3:01 pm, "barcarolle r" <barcarol...@mu sic.netwrote:
I have a set<T*that stores pointers to objects. How can I tell
set<T*to use the objects' operator<() and not the value of the
pointers for sorting?
There's a way to create a set with two parameters: set<MyType,
key_comp>, where
key_comp is a key comparison function you define. For example,
set<int, less<int.

So then you'd just need to provide the appropriate comparison function
that uses
the objects' operator<(), suitably wrapped.
Actually you need a class with an appropriate operator(). A plain
function won't do here since you cannot pass it as a template
parameter.

Sure you can. Try it.

No you cannot. I guess you are confusing this with algorithms like
std::lower_boun d where you can specify a predicate as a _function_
parameter. As a template parameter for std::set or std::map this does not
work. Think about it -- it needs to be a type.
You're right. Spoke too quickly.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
May 8 '07 #12
Greg Herlihy wrote:
>

On 5/7/07 5:01 PM, in article pa************* ********@yahoo. de, "Markus
Schoder" <a3************ *@yahoo.dewrote :
>On Mon, 07 May 2007 19:45:00 -0400, Pete Becker wrote:
>>Markus Schoder wrote:
On Mon, 07 May 2007 16:11:52 -0700, Michael wrote:
On May 7, 3:01 pm, "barcarolle r" <barcarol...@mu sic.netwrote:
>I have a set<T*that stores pointers to objects. How can I tell
>set<T*to use the objects' operator<() and not the value of the
>pointers for sorting?
There's a way to create a set with two parameters: set<MyType,
key_comp> , where
key_comp is a key comparison function you define. For example,
set<int, less<int.
>
So then you'd just need to provide the appropriate comparison function
that uses
the objects' operator<(), suitably wrapped.
Actually you need a class with an appropriate operator(). A plain
function won't do here since you cannot pass it as a template
parameter.
Sure you can. Try it.
No you cannot. I guess you are confusing this with algorithms like
std::lower_bou nd where you can specify a predicate as a _function_
parameter. As a template parameter for std::set or std::map this does not
work. Think about it -- it needs to be a type.

But std::set's Compare type may be a function type - it does not need to be
a class type that has an overloaded operator() defined. So Pete's objection
is correct - and here is some code to demonstrate:

#include <set>

bool MyComp(int *a, int *b)
{
return *a < *b;
}

int main()
{
std::set<int*, bool (*)(int*, int*)s(MyComp);

s.insert( new int(1) );
s.insert( new int(2) );
...
}
You're right. Spoke too quickly.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
May 8 '07 #13

barcaroller дµÀ£º
I have a map<T*that stores pointers to objects. How can I tell map<T*>to
use the objects' operator<() and not the value of the pointers for sorting?

If that's not feasible, what alternatives do I have to sort a set of objects
that cannot and should not be copied (i.e. I can use only pointers to these
objects)?

Note: I am aware of the method of creating a new class that wraps the
pointers of the objects and then provides its own operator<(). However, I
am trying to avoid creating new classes.
template <typename _Key, typename _Tp, typename _Compare =
less<_Key>,
typename _Alloc = allocator<pair< const _Key, _Tp >class
map{
.....
};
you only need to assign _Compare to the functor which is used to
compare key as you like

May 8 '07 #14
On May 8, 2:01 am, Markus Schoder <a3vr6dsg-use...@yahoo.de wrote:
On Mon, 07 May 2007 19:45:00 -0400, Pete Becker wrote:
Markus Schoder wrote:
On Mon, 07 May 2007 16:11:52 -0700, Michael wrote:
On May 7, 3:01 pm, "barcarolle r" <barcarol...@mu sic.netwrote:
I have a set<T*that stores pointers to objects. How can I tell
set<T*to use the objects' operator<() and not the value of the
pointers for sorting?
There's a way to create a set with two parameters: set<MyType,
key_comp>, where
key_comp is a key comparison function you define. For example,
set<int, less<int.
>So then you'd just need to provide the appropriate comparison function
that uses
the objects' operator<(), suitably wrapped.
Actually you need a class with an appropriate operator(). A plain
function won't do here since you cannot pass it as a template
parameter.
Sure you can. Try it.
No you cannot. I guess you are confusing this with algorithms like
std::lower_boun d where you can specify a predicate as a _function_
parameter. As a template parameter for std::set or std::map this does not
work. Think about it -- it needs to be a type.
Now I've seen everything. Someone trying to correct Pete with
regards to the standard library.

Whether you're aware of it or not, pointer to a function *is* a
type, and there's no problem declaring an std::set to take a
pointer to a function, e.g.:

bool
cmp( T const* p1, T const* p2 )
{
return *p1 < *p2 ;
}
std::set< T*, bool (*)( T const*, T const* ) mySet( cmp ) ;

Because the instances of a pointer to function are not
unipotent, it is necessary to pass an argument to the
constructor, to specify which one you want, but this is also
true for functional objects which aren't unipotent.

--
James Kanze (Gabi Software) email: ja*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 8 '07 #15

"Pete Becker" <pe**@versatile coding.comwrote in message
news:L-*************** *************** @giganews.com.. .
Markus Schoder wrote:
>On Mon, 07 May 2007 19:45:00 -0400, Pete Becker wrote:
>>Markus Schoder wrote:
Actually you need a class with an appropriate operator(). A plain
function won't do here since you cannot pass it as a template
parameter.
Sure you can. Try it.

No you cannot. I guess you are confusing this with algorithms like
std::lower_bou nd where you can specify a predicate as a _function_
parameter. As a template parameter for std::set or std::map this does not
work. Think about it -- it needs to be a type.

You're right. Spoke too quickly.
No he's not, and no you didn't ;)

- Sylvester Hesp
May 8 '07 #16
On Mon, 07 May 2007 21:00:20 -0400, Pete Becker wrote:
Markus Schoder wrote:
>On Mon, 07 May 2007 19:45:00 -0400, Pete Becker wrote:
>>Markus Schoder wrote:
On Mon, 07 May 2007 16:11:52 -0700, Michael wrote:
On May 7, 3:01 pm, "barcarolle r" <barcarol...@mu sic.netwrote:
>I have a set<T*that stores pointers to objects. How can I tell
>set<T*to use the objects' operator<() and not the value of the
>pointers for sorting?
There's a way to create a set with two parameters: set<MyType,
key_comp> , where
key_comp is a key comparison function you define. For example,
set<int, less<int.
>
So then you'd just need to provide the appropriate comparison
function that uses
the objects' operator<(), suitably wrapped.
Actually you need a class with an appropriate operator(). A plain
function won't do here since you cannot pass it as a template
parameter.
Sure you can. Try it.

No you cannot. I guess you are confusing this with algorithms like
std::lower_bou nd where you can specify a predicate as a _function_
parameter. As a template parameter for std::set or std::map this does
not work. Think about it -- it needs to be a type.

You're right. Spoke too quickly.
You were right -- I was wrong. Sorry for the confusion. I overlooked the
fact that there is a parameter to the set constructor for the compare
object which of course allows passing a function.

--
Markus Schoder
May 8 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1779
by: sachin_mzn | last post by:
Hi, When we use find method over a STL map. which searching algorithm is used internally. Is it a hash search? Or map implementation internally decide depending on element. -Sachin
3
1438
by: mcassiani | last post by:
Hi, I need use map faster as possible (I store in the map data about open network connections). First a question, this code fragment is from "The C++ Programming........
10
2978
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The constructors and copy constructors in 'A' report when they are called. 'whoami' is just a unique identifier assigned to every object of type 'A'. The output of the program is: constructor 0 constructor 1
2
2265
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It uses map<and heavy overloading. The problem is, the output file differs from input, and for the love of me I can't figure out why ;p #include <iostream> #include <fstream> #include <sstream>
3
2945
by: newbie | last post by:
Same thing g++ complains when using hash_map<>, but is happy with map<--I understand hahs_map is not standardized, but since the compiler didn't complain something like 'hash_map<not defined', I suppose it's supported and should behave well when I used it correctly. BUT it didn't. Here is my code snippet: class MyKey { public: virtual void foo() { return; }
2
1587
by: DaTurk | last post by:
Hi, I'm trying to hold a map of ints,and function pointers in C++ map<int, (*functPtr)(int, int)something I need to hold a list of callbacks. For some reason this syntax is not working. Any ideas?
12
5839
by: jabbah | last post by:
Actually I'm quite sure I've missed something trivial here, but I just can't find it. Seemingly I cannot read from a const map& I try #include <iostream> #include <map> using namespace std;
2
2789
by: jabbah | last post by:
I have some data in a map and I want to sort it. Currently I have implemented it like this: #include <iostream> #include <map> #include <string> using namespace std; int main(){
6
5444
by: Juha Nieminen | last post by:
joseph cook wrote: Not always. By default, yes, but you can specify other comparators, eg: std::map<int, int, std::greaterreversedMap;
0
9704
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
9572
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
10319
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
10303
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
10070
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
9132
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
7608
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
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
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

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.