Stupid C++ question...(should be easy to solve!)

scootermaster

Platinum Member
Nov 29, 2005
2,411
0
0
Simple version:

I want to map a bunch of pointers to a vector of integers.

You'd think something like this would work:

vector<int> lotsOfInts;

map<thingee *, nodes> iLoveMaps;

But either it doesn't, or I'm retarded.


Complicated Version:

I want to map a bunch of symbols to the nodes in a DFG that use that symbol. I could so a multimap, and have an entry for each mapping (i.e. if symbol x appears in nodes 2,3,6, have a mapping x->2, x->3 and x->6...but it seems more elegant to have x-> <2,3,6>)

I can use integers because they'll represent the node ID in the graph, saving me from using the actual pointers and having to dereference them. And yes, I know I should be using boost for the graph. I'm a bad person. I admit it.

Any help? Thanks!
 

scootermaster

Platinum Member
Nov 29, 2005
2,411
0
0
Okay, I actually got the declaration to compile (who knew that STL declarations were so white-space sensitive?).

But now here's the better question...how do I "add" to the map?

So at some point I have something like:

symbol * mySymbol;

And I want to map that to nodes, say, 4,5 and 10.

Would I need to "load up" the int vector first (wit 4,5 and 10) and then add it to the map, with mySymbol as the key? How would I do that? This is confusing to me because it sure looks like the int vector is anonymous.

Help?
 

Templeton

Senior member
Oct 9, 1999
467
0
0
like this?

typedef std::vector<int> IntVec;
typedef std::map<MyKey, IntVec> MyMap;


MyMap map;

IntVec vec1 = ...//create/fill vector
map.insert(std::make_pair(someKey, vec1));

...
MyMap::iterator it = map.find(someKey);
IntVec& vec2 = it->second;
vec2.push_back(newInt);
 

scootermaster

Platinum Member
Nov 29, 2005
2,411
0
0
Originally posted by: Templeton
like this?

typedef std::vector<int> IntVec;
typedef std::map<MyKey, IntVec> MyMap;


MyMap map;

IntVec vec1 = ...//create/fill vector
map.insert(std::make_pair(someKey, vec1));

...
MyMap::iterator it = map.find(someKey);
IntVec& vec2 = it->second;
vec2.push_back(newInt);


First off, thanks SO MUCH for the reply. I didn't think to use type defs, and that will help with the fact that the vector seems to only compile within the map when it's anonymous.

The only part I don't understand is the makepair. Why do you need that? I guess it's because the assignment operator isn't defined for the myMap typedef, eh? Could you overload it? (Probably more effort than it's worth). But still...I'm not sure what the make_pair is doing there. The definition of MyMap isn't enough to format an insert without the pair?

Thanks again!