can I re reference items in a map

W

Whybother

I have class CMyClass to which I create objects of and store a count of them
in a stl map. (stlport)

like so

typedef map<CMyClass, unsigned, myclass_less> Results_Map;
....
Results_Map m_Map;
....
....
CMyClass mc(...);
m_Map[mc]++;
....


How can I get access to the same mc object I just added to the map? In other
words I'm trying to do something like

CMyClass& mcRef = *m_Map[mc];

If thats possible.

thanks
 
V

Victor Bazarov

Whybother said:
I have class CMyClass to which I create objects of and store a count of them
in a stl map. (stlport)

like so

typedef map<CMyClass, unsigned, myclass_less> Results_Map;
...
Results_Map m_Map;
...
...
CMyClass mc(...);
m_Map[mc]++;
...


How can I get access to the same mc object I just added to the map? In other
words I'm trying to do something like

CMyClass& mcRef = *m_Map[mc];

If thats possible.

I think you're confused with how 'map' works. You store _unsigned_ values
there. You make your objects the _keys_ for those unsigned values. Keys
get _copied_. If you need to find what key a particular unsigned value
has, you can _search_ for that value and then look at the key through the
iterator you get back...

Indexing by an object will get you the 'unsigned' part of the map. For
example

m_Map[mc]

results in a reference to the unsigned value corresponding to the counter
of the objects that have the same "value" as 'mc'. But if you have 'mc',
why do you need a reference to it?

IOW, based on what you wrote here and the questions you asked, I have no
clues as to what you're trying to do. What is it you're attempting to
accomplish? Perhaps 'map' is not the best solution, or perhaps you need
to define it differently...

V
 
M

Mike Wahler

Whybother said:
I have class CMyClass to which I create objects of and store a count of
them in a stl map. (stlport)

like so

typedef map<CMyClass, unsigned, myclass_less> Results_Map;
...
Results_Map m_Map;
...
...
CMyClass mc(...);
m_Map[mc]++;

Have you defined an operator++() for type 'CMyClass'?
...


How can I get access to the same mc object I just added to the map? In
other words I'm trying to do something like

CMyClass& mcRef = *m_Map[mc];
If thats possible.

CMyClass& mcRef = m_Map.find(mc)->first;

(This assumes that the map contains a key with value == mc.
If it's possible that it doesn't, you should compare the return
value of 'm_Map.find()' with 'm_map.end()', and only dereference
the iterator if they're not equal)


-Mike
 
V

Victor Bazarov

Mike said:
I have class CMyClass to which I create objects of and store a count of
them in a stl map. (stlport)

like so

typedef map<CMyClass, unsigned, myclass_less> Results_Map;
...
Results_Map m_Map;
...
...
CMyClass mc(...);
m_Map[mc]++;


Have you defined an operator++() for type 'CMyClass'?

Why would it be needed? 'm_Map[mc]' yields 'unsigned&'. ++ is defined
for the unsigned, isn't it?

V
 
W

Whybother

I'm thinking backwards. I am trying to store a list of reference numbers
within the CMyClass objects. So when I increase the count ala >>
m_Map[mc]++; I'd like to also store the reference number. something like

m_Map[mc]++;
CMyClass& refMyClass = m_Map[mc];
refMyClass.AddReferenceNumber(x);

so later when I dump it out each MyClass object will contain the list of
reference numbers.

I originally tried this

mc.AddReferenceNumber(x);
m_Map[mc]++;

and in the copy constructor I was doing this

void CMyClass::copy (const CMyClass& bs)
{
...
m_mapRefNumbers.assign(bs.m_mapRefNumbers.begin(),
bs.m_mapRefNumbers.end());
...
}

but when I dumped the map each CMyClass objects m_mapRefNumbers only
contained one reference not the culmulative collection of all the ref
numbers.


I know one solution would be to have a struct

typedef struct
{
int iCount;
ReferenceMap m_RefMap;
} MyClassRefMap

then have
but I'm unclear as to why my other solution wasn't correct.

thanks


Victor Bazarov said:
Whybother said:
I have class CMyClass to which I create objects of and store a count of
them in a stl map. (stlport)

like so

typedef map<CMyClass, unsigned, myclass_less> Results_Map;
...
Results_Map m_Map;
...
...
CMyClass mc(...);
m_Map[mc]++;
...


How can I get access to the same mc object I just added to the map? In
other words I'm trying to do something like

CMyClass& mcRef = *m_Map[mc];

If thats possible.

I think you're confused with how 'map' works. You store _unsigned_ values
there. You make your objects the _keys_ for those unsigned values. Keys
get _copied_. If you need to find what key a particular unsigned value
has, you can _search_ for that value and then look at the key through the
iterator you get back...

Indexing by an object will get you the 'unsigned' part of the map. For
example

m_Map[mc]

results in a reference to the unsigned value corresponding to the counter
of the objects that have the same "value" as 'mc'. But if you have 'mc',
why do you need a reference to it?

IOW, based on what you wrote here and the questions you asked, I have no
clues as to what you're trying to do. What is it you're attempting to
accomplish? Perhaps 'map' is not the best solution, or perhaps you need
to define it differently...

V
 
M

Mike Wahler

Victor Bazarov said:
Mike said:
typedef map<CMyClass, unsigned, myclass_less> Results_Map;
...
Results_Map m_Map;
...
...
CMyClass mc(...);
m_Map[mc]++;


Have you defined an operator++() for type 'CMyClass'?

Why would it be needed? 'm_Map[mc]' yields 'unsigned&'. ++ is defined
for the unsigned, isn't it?

Ack! I must have looked at it backwards. Thanks for the
correction.

-Mike
 
D

Daniel T.

"Whybother said:
I'm thinking backwards. I am trying to store a list of reference numbers
within the CMyClass objects. So when I increase the count ala >>
m_Map[mc]++; I'd like to also store the reference number. something like

m_Map[mc]++;
CMyClass& refMyClass = m_Map[mc];
refMyClass.AddReferenceNumber(x);

so later when I dump it out each MyClass object will contain the list of
reference numbers.

I originally tried this

mc.AddReferenceNumber(x);
m_Map[mc]++;

and in the copy constructor I was doing this

void CMyClass::copy (const CMyClass& bs)
{
...
m_mapRefNumbers.assign(bs.m_mapRefNumbers.begin(),
bs.m_mapRefNumbers.end());
...
}

but when I dumped the map each CMyClass objects m_mapRefNumbers only
contained one reference not the culmulative collection of all the ref
numbers.


I know one solution would be to have a struct

typedef struct
{
int iCount;
ReferenceMap m_RefMap;
} MyClassRefMap

then have
but I'm unclear as to why my other solution wasn't correct.

I've read both your post and I'm still not getting it. How about you
post something like "I have this (describe your starting objects,) and I
want to end up with this (describe desired results.)"

It sounds like you have a number of MyClass objects, and you want to add
a list of values to them? But that wouldn't have anything to do with a
map, a vector or list would be fine...
 
M

Mark P

Whybother said:
I have class CMyClass to which I create objects of and store a count of them
in a stl map. (stlport)

like so

typedef map<CMyClass, unsigned, myclass_less> Results_Map;
...
Results_Map m_Map;
...
...
CMyClass mc(...);
m_Map[mc]++;
...


How can I get access to the same mc object I just added to the map? In other
words I'm trying to do something like

CMyClass& mcRef = *m_Map[mc];

If thats possible.

thanks

I think you want to do something like the following:

typedef map <CMyClass, unsigned, myclass_less> Results_Map;
typedef Results_Map::iterator rmIter;
typedef Results_Map:: value_type rmVal;

Results_Map m_Map;
CMyClass mc(...);
rmIter it = m_Map.find(mc);
if (it != m_Map.end()) // key found
++it->second;
else // new key, assign initial data value = 1
it = m_Map.insert(rmVal(mc,1)).first;
const CMyClass& mcRef = it->first;
// do stuff with mcRef

Note that I made mcRef a const& since the keys of a map should be constant.

-Mark
 

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,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top