473,791 Members | 2,711 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 #1
16 2007
barcaroller wrote:
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?
There is no map<T*>. map takes (at least) two template parameters --
the key and the value.
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.
Why? If it does the job?
May 7 '07 #2

"red floyd" <no*****@here.d udewrote in message
news:EB******** *********@newss vr23.news.prodi gy.net...
There is no map<T*>. map takes (at least) two template parameters --
the key and the value.
I'm sorry; my mistake. I meant set<T*>.
May 7 '07 #3
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.

Michael

May 7 '07 #4
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.

--
Markus
May 7 '07 #5
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.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
May 7 '07 #6
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.

--
Markus
May 8 '07 #7

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_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.
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) );
...
}

Greg

May 8 '07 #8

"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_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.
And at what point in time did functions stop having types? :)

bool mycompfunc(int, int);
std::set<int, bool(*)(int, int)myset(mycom pfunc);

- Sylvester Hesp
May 8 '07 #9
barcaroller schrieb:
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?
std::map has 4 template parameters. The 3rd is a compare functor. Write a
functor that simply maps to the objects operator<().

In std::set, it's the second template parameter.

--
Thomas
http://www.netmeister.org/news/learn2quote.html
May 8 '07 #10

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

Similar topics

1
1778
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
1437
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
2976
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
2262
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
2943
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
1586
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
5442
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
9515
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
10426
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
10207
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...
0
9029
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
5430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4109
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
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
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.