-1

I have declared array of sets

std::set<md_core::Sample *> _SessionSet[MAX_SESSIONS];

Now I wrote two functions

 void insertIntoTrdSessionSet(unsigned char index, md_core::Sample *sample)
    {
        _SessionSet[index].insert(sample);
    }

    bool removeFromTrdSessionSet(md_core::Sample *sample, unsigned char i = MAX_SESSIONS)
    {
        if(i != MAX_SESSIONS)
        {
            if(_SessionSet[i].erase(sample))
                return true;
        }
        else
        {
            for(i = 0; i < MAX_SESSIONS ; i++)
            {
                if(_SessionSet[i].erase(sample))
                {
                    return true;
                }
            }
        }
        return false;
    }

Now i extract a value from set one by one and try to delete but it shows that value is not present in the struct

  for(i = 0; i < MAX_SESSIONS ; i++)
                {
                    if(i != pMsg->_Session)
                    {
                        std::set<Sample *>::iterator it = pSub->_SessionSet[i].begin();
                        for(;it != pSub->_SessionSet[i].end(); it++)
                        {
                            sample = *it;
//now call delete for the sample

                                if(!pSub->removeFromTrdSessionSet(sample, i))
                                {
                                   logV(MD_WARN_MSG, "No such sample %d to delete from odrders map for  session %u", sample, index);
                                }
                        }
                    }
                }
hivert
  • 10,579
  • 3
  • 31
  • 56
rahul.deshmukhpatil
  • 977
  • 1
  • 16
  • 31

2 Answers2

0

Your set is sorted by pointer value, which is probably pretty useless; in any case it will go by object identity instead of object value. If that is not what you want, you will have to supply your own ordering to std::set (it's a template argument) which sorts by actual value.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • thanks fr ur reply, Actually i want to store pointer to objects in other memory pool only. Anyways those pointers are going to be unique values. – rahul.deshmukhpatil Jun 24 '13 at 13:46
0

You are incrementing the iterator after erasing the set-item it was pointing to. The standard says you should not do that.

IRC all the iterators pointing to a container element are invalidated when that element is erased. On some containers, all iterators are invalidated, no matter what element they are pointing at.

Don't know about sets, but when using list-iterators, it is enough to increment the iterator before deleting the element.