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

map with more than one key type

Let me preface this with: I do have a good STL book on order from amazon as
we speak. $70..ouch, but as much as I ask STL questions on here...

In the meantime, I need a collection that can store an object that can be
looked up by two differant key types. Is there an existing class or
combination of classes that I can do this with?

Requirements:
I need a collection of elements with the following association:
MyClass * - guarenteed to point to a unique MyClass object on the heap
void * - optional, NULL or any object the user desires
std::string - optional, can be empty or a name of the association

Now MyClass is unique, so it is a canidate for being a key, but it is really
the thing I want to store so it is more of a value.
The other two are optional, so they can be keys, using a multimap it wouldnt
matter if they are NULL or empty or not.
The purpose of the collection is not to provide lookup, but rather to act
upon multiple MyClass's at once.

I came up with the following

MyClassSet
{
public:
..
someError Insert(MyClass * myclass, void * associated object = NULL,
std::string name = std::string("unnamed");

// Arg, I really need to be able to remove by these two!
someError Remove(MyClass *);
someError Remove(std::string);

// Perform an operation on ALL the MyClasses in the set.
void DoSomething();

// Get some kind of iterator for MyClassSet...this will be a seperate
newsgroup question I am sure
someIteratorThing GetNextElement();

// get the MyClass * and the void * associated with the string name
INeedToReturnTwoThings! GetMyClassByName(std::string)

private:
std::multimap<std::string, MyClass *>
std::multimap<MyClass *, void *>
}

The above has alot of problems I am sure you can see.
I was also thinking of using typdef and struct to make some type that has
all three types that are associated, but then what am I going to use for the
lookup?

I also thought about using templates to get rid of the void *, but the user
needs to have differant object types in the same MyClassSet, because MyClass
* can be associated with anything.

Can you help me get my head around this?
Mar 26 '07 #1
3 2523

"christopher" <so*****@somewhere.netwrote in message
news:46***********************@roadrunner.com...
Let me preface this with: I do have a good STL book on order from amazon
as we speak. $70..ouch, but as much as I ask STL questions on here...
Out of curiosity, which book?
>
In the meantime, I need a collection that can store an object that can be
looked up by two differant key types. Is there an existing class or
combination of classes that I can do this with?
std::set (and multiset) and std::map (and multimap)
can be used to quickly locate an object via a 'primary' key.
Both containers can also be searched with std::find.
>
Requirements:
I need a collection of elements with the following association:
MyClass * - guarenteed to point to a unique MyClass object on the heap
How do you plan to guarantee uniqueness of your objects?
void * - optional, NULL or any object the user desires
std::string - optional, can be empty or a name of the association

Now MyClass is unique, so it is a canidate for being a key, but it is
really the thing I want to store so it is more of a value.
The other two are optional, so they can be keys, using a multimap it
wouldnt matter if they are NULL or empty or not.
The purpose of the collection is not to provide lookup, but rather to act
upon multiple MyClass's at once.
What exactly are you trying to do? What do you mean by 'act upon
multiple objects at once'?

-Mike
Mar 26 '07 #2
"Mike Wahler" <mk******@mkwahler.netwrote in message
news:_I*****************@newsread3.news.pas.earthl ink.net...
>
"christopher" <so*****@somewhere.netwrote in message
news:46***********************@roadrunner.com...
>Let me preface this with: I do have a good STL book on order from amazon
as we speak. $70..ouch, but as much as I ask STL questions on here...

Out of curiosity, which book?
>>
In the meantime, I need a collection that can store an object that can be
looked up by two differant key types. Is there an existing class or
combination of classes that I can do this with?

std::set (and multiset) and std::map (and multimap)
can be used to quickly locate an object via a 'primary' key.
Both containers can also be searched with std::find.
>>
Requirements:
I need a collection of elements with the following association:
MyClass * - guarenteed to point to a unique MyClass object on the heap

How do you plan to guarantee uniqueness of your objects?
> void * - optional, NULL or any object the user desires
std::string - optional, can be empty or a name of the association

Now MyClass is unique, so it is a canidate for being a key, but it is
really the thing I want to store so it is more of a value.
The other two are optional, so they can be keys, using a multimap it
wouldnt matter if they are NULL or empty or not.
The purpose of the collection is not to provide lookup, but rather to act
upon multiple MyClass's at once.

What exactly are you trying to do? What do you mean by 'act upon
multiple objects at once'?

-Mike
The C++ Standard Library: A Tutorial and reference (Hardcover)
by Nicolai Josuttis

It seemed to be the one recommended most on this group.

I can't really go into exactly what I am trying to do, because this is for
work and I would get in deep poopy.

Let's say MyClass controls a monitor and has methods for turning it on and
off. I implemented MyClass. The user will most likely want to associate
MyClass with a PC object of thier own. I am sure you are thinking that
MyClass should be an aggregate of PC object at this point, but I am required
to implement a collection of MyClasses anyway, rather than require the user
to implement a collection of PC objects that contain MyClass as a member.

So, I have a collection of MyClasses that can turn on and off monitors, but
the user has PC objects that can do more things and have additional data. I
simply want to make a class that serves as a collection of MyClasses that
provides a single method to turn off all monitors in the collection at once.
I also want to allow the user to name the Monitor and PC association, if
there is one, and be able to look it up that way if they wish.

So I've got a collection of MyClasses
It may or may not have associated PC objets
and it may or may not have a string to look it up
but it will surely have one method to act upon all the MyClasses it
contains.

So I've got three values, instead of the traditional two for a key value
pair. Additonally, two of them may not be unique. The void * can point to
NULL for many elements and the string could be empty for many elements. My
problem is how to store an element of this nature in some STL container.

You also asked how I would guarentee uniqueness. Well, if the allocated an
object somewhere, the address is going to be unique. If they try to insert
the same thing twice I return an error or ignore it.
I'd rather do that than make some windows like handle...I've never really
understood why to use a handle instead of the pointer itself really...but I
don't claim to be the most knowledgable guy on the block either.




Mar 27 '07 #3
On Mon, 26 Mar 2007 19:08:35 -0500 in comp.lang.c++, "chris"
<so*****@somewhere.netwrote,
>I can't really go into exactly what I am trying to do, because this is for
work and I would get in deep poopy.
Some things to consider:

You can have as many map<keytype, object*as you want, all pointing to
the same pool of objects.

If you put something into set<objectit stays put, and pointers to the
set element remain valid, as long as it lives. Not true for vector<>!

If you must manage a pool of new'ed objects, tr1::shared_ptr or
boost::shared_ptr and friends will help save whatever is left of your
sanity.

Mar 27 '07 #4

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

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
4
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get...
0
by: Joe Kraft | last post by:
I'm hoping someone can help me out. We're running a website that uses XML/XSLT transformations, VB.Net and an Oracle database. Currently the site cannot support more than 6-7 users at a time...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
6
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
3
by: Water Cooler v2 | last post by:
Questions: 1. Can there be more than a single script block in a given HEAD tag? 2. Can there be more than a single script block in a given BODY tag? To test, I tried the following code. None...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
1
by: Red Daly | last post by:
Hello group, I have been using JSON for a while and it has made many things a breeze. However, JSON does not natively describe certain things like pointers and custom types. I created a simple...
23
by: Tony Johansson | last post by:
Hello! I just wonder what is the point of having the reader variable declared as TextReader in the snippet below.. Is it because of using the polymorfism on the reader variable perhaps. using...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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,...
0
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...
0
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...
0
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...

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.