map whose key_type is the pointer to an object

Discussion in 'C++' started by cesco, Jan 17, 2006.

  1. cesco

    cesco Guest

    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;
    }
    }
     
    cesco, Jan 17, 2006
    #1
    1. Advertising

  2. cesco

    Mark P Guest

    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::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;
    > }
    > }
    >
     
    Mark P, Jan 17, 2006
    #2
    1. Advertising

  3. cesco

    Earl Purple Guest

    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.
     
    Earl Purple, Jan 17, 2006
    #3
  4. cesco

    cesco Guest

    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
     
    cesco, Jan 17, 2006
    #4
  5. cesco

    Mark P Guest

    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?
     
    Mark P, Jan 17, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    707
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    846
    Jordan Abel
    Mar 28, 2006
  3. Lambda
    Replies:
    5
    Views:
    5,795
    Lambda
    Jun 24, 2008
  4. Replies:
    4
    Views:
    1,276
    Fred Zwarts
    Jul 2, 2009
  5. A
    Replies:
    7
    Views:
    640
Loading...

Share This Page