map with more than one key type

C

christopher

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?
 
M

Mike Wahler

christopher said:
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
 
C

chris

Mike Wahler said:
Out of curiosity, which book?


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.


How do you plan to guarantee uniqueness of your objects?


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.
 
D

David Harmon

On Mon, 26 Mar 2007 19:08:35 -0500 in comp.lang.c++, "chris"
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<object> it 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top