Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > map whose key_type is the pointer to an object

Reply
Thread Tools

map whose key_type is the pointer to an object

 
 
cesco
Guest
Posts: n/a
 
      01-17-2006
Hi,

in the following piece of code I need to iterate through a vector to
create a map that has as key type the pointer to an object of the
UserEquipment class and as value type size_t.
The problem I encountered is that even if the address of the pointer to
the UserEquipment (in the code it is pActiveUe) changes inside the
cycle, the key doesn't seem to change because the previous value is
overwritten with the new value instead of a new element being added to
the map. Can anyone explain why?
Thanks.

vector<Connection*>& rpActiveConnections =
mpSector->GetrActiveConnections();
if ( !rpActiveConnections.empty() ) {
// in the header: map<UserEquipment*, size_t> mNumOfFreqSlotsToAdd;
mNumOfFreqSlotsToAdd.clear();
UserEquipment* pActiveUe;
double scalingFactor;
size_t numOfFreqSlotsToRemove;
for (vector<Connection*>::iterator activeConIt =
rpActiveConnections.begin();
activeConIt != rpActiveConnections.end(); ++activeConIt) {
pActiveUe = (*activeConIt)->GetpUserEquipment();
vector<int>& rAllocatedFreqSlotsForUe =
pActiveUe->GetrAllocatedFrequencySlots();
if ( pActiveUe->GetUplinkSinr_l() > mSinrTarget_l +
pActiveUe->GetPowerStep_l() ) {
scalingFactor = pActiveUe->GetUplinkSinr_l() /
mSinrTarget_l;
}
else {
scalingFactor =
pActiveUe->GetMaxTransmitPower_W()/pActiveUe->GetTransmitPower_W();
}
// the following instruction is my problem
mNumOfFreqSlotsToAdd[pActiveUe] =
(size_t)ceil( rAllocatedFreqSlotsForUe.size() *
(scalingFactor-1) );
cout << "mNumOfFreqSlotsToAdd has size "<<
mNumOfFreqSlotsToAdd.size() << endl;
}
}

 
Reply With Quote
 
 
 
 
Mark P
Guest
Posts: n/a
 
      01-17-2006
cesco wrote:
> Hi,
>
> in the following piece of code I need to iterate through a vector to
> create a map that has as key type the pointer to an object of the
> UserEquipment class and as value type size_t.
> The problem I encountered is that even if the address of the pointer to
> the UserEquipment (in the code it is pActiveUe) changes inside the
> cycle, the key doesn't seem to change because the previous value is
> overwritten with the new value instead of a new element being added to
> the map. Can anyone explain why?
> Thanks.
>


You haven't given us enough to run the code. Are you sure that
pActiveUe really is changing? I don't think the problem is with
map:perator[]. Try printing out the value of pActiveUe immediately
before that line.

> vector<Connection*>& rpActiveConnections =
> mpSector->GetrActiveConnections();
> if ( !rpActiveConnections.empty() ) {
> // in the header: map<UserEquipment*, size_t> mNumOfFreqSlotsToAdd;
> mNumOfFreqSlotsToAdd.clear();
> UserEquipment* pActiveUe;
> double scalingFactor;
> size_t numOfFreqSlotsToRemove;
> for (vector<Connection*>::iterator activeConIt =
> rpActiveConnections.begin();
> activeConIt != rpActiveConnections.end(); ++activeConIt) {
> pActiveUe = (*activeConIt)->GetpUserEquipment();
> vector<int>& rAllocatedFreqSlotsForUe =
> pActiveUe->GetrAllocatedFrequencySlots();
> if ( pActiveUe->GetUplinkSinr_l() > mSinrTarget_l +
> pActiveUe->GetPowerStep_l() ) {
> scalingFactor = pActiveUe->GetUplinkSinr_l() /
> mSinrTarget_l;
> }
> else {
> scalingFactor =
> pActiveUe->GetMaxTransmitPower_W()/pActiveUe->GetTransmitPower_W();
> }
> // the following instruction is my problem
> mNumOfFreqSlotsToAdd[pActiveUe] =
> (size_t)ceil( rAllocatedFreqSlotsForUe.size() *
> (scalingFactor-1) );
> cout << "mNumOfFreqSlotsToAdd has size "<<
> mNumOfFreqSlotsToAdd.size() << endl;
> }
> }
>

 
Reply With Quote
 
 
 
 
Earl Purple
Guest
Posts: n/a
 
      01-17-2006

cesco wrote:
> Hi,
>
> // the following instruction is my problem
> mNumOfFreqSlotsToAdd[pActiveUe] =
> (size_t)ceil( rAllocatedFreqSlotsForUe.size() *
> (scalingFactor-1) );
> cout << "mNumOfFreqSlotsToAdd has size "<<
> mNumOfFreqSlotsToAdd.size() << endl;
> }
> }


Well having finally being able to decipher through your indenting style
(don't know why K&R is still so popular but even indenting K&R a bit of
whitespace wouldn't go amiss), it appears you are extending a class
(adding an extra size member) by having a map to all the instances of
your classes and mapping it to such a member. Well maybe not all the
instances.

So really I'm not sure your design is right.

 
Reply With Quote
 
cesco
Guest
Posts: n/a
 
      01-17-2006
Thanks for the reply.

Yes, I'm sure the address is changing. These are the addresses of
pActiveUe I get over four cycles of the same 'for' loop:

pActiveUe has address 006C89C0
pActiveUe has address 006CA8B0
pActiveUe has address 006D1F48
pActiveUe has address 006D4888

Any other idea?

Thanks

 
Reply With Quote
 
Mark P
Guest
Posts: n/a
 
      01-17-2006
cesco wrote:
> Thanks for the reply.
>
> Yes, I'm sure the address is changing. These are the addresses of
> pActiveUe I get over four cycles of the same 'for' loop:
>
> pActiveUe has address 006C89C0
> pActiveUe has address 006CA8B0
> pActiveUe has address 006D1F48
> pActiveUe has address 006D4888
>
> Any other idea?
>
> Thanks
>


Ideally, provide a compilable sample of code which exhibits the problem.

Beyond that, how do you know that the previous value is being
overwritten? Have you tried using the map::insert function whose pair
return value indicates whether a new item was actually inserted?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
How to define a map whose 'key' is a structure? Lambda C++ 5 06-24-2008 07:25 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM



Advertisments