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. 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?
"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*>.
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
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
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)
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
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
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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........
|
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
|
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>
|
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; }
| |
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?
|
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;
|
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(){
|
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;
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |