473,382 Members | 1,445 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,382 software developers and data experts.

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 1971
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.dudewrote in message
news:EB*****************@newssvr23.news.prodigy.ne t...
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, "barcaroller" <barcarol...@music.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, "barcaroller" <barcarol...@music.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, "barcaroller" <barcarol...@music.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, "barcaroller" <barcarol...@music.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_bound 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, "barcaroller" <barcarol...@music.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_bound 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*********************@yahoo.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_bound 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(mycompfunc);

- 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
On Tue, 08 May 2007 02:24:56 +0200, Sylvester Hesp wrote:
"Markus Schoder" <a3*************@yahoo.dewrote in message
news:pa*********************@yahoo.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_bound 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(mycompfunc);
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, "barcaroller" <barcarol...@music.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_bound 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, "barcaroller" <barcarol...@music.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_bound 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.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, "barcaroller" <barcarol...@music.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_bound 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*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 8 '07 #15

"Pete Becker" <pe**@versatilecoding.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_bound 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, "barcaroller" <barcarol...@music.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_bound 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
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
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
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...
2
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...
3
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...
2
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...
12
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.