map whose key_type is the pointer to an object

C

cesco

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;
}
}
 
M

Mark P

cesco said:
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::eek: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;
}
}
 
E

Earl Purple

cesco said:
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.
 
C

cesco

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
 
M

Mark P

cesco said:
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?
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top