pointer to a map

M

markww

Hi,

I have a class like:

class CStorage {

map<int, vector<CSomething> > m_mData;

CSomething *m_pLastUsed;
};

I'd like to keep a member that points exactly to the 'last used'
element in that m_mData member. I would want to do something like:

void CStorage::Access(int nIndex, int nElement)
{
m_pLastUsed = &m_mData[nIndex][nElement];
}

That is pretty useful but if I want to erase the last used element, how
would I find that in the map to erase it? I can't do something like:

void CStorage::EraseLastUsed()
{
m_mData.erase(m_pLastUsed);
}

Yeah, how can I do something like that? Maybe keep an iterator to the
last element instead of the type pointer to it?

Thanks
 
I

Ian Collins

markww said:
Hi,

I have a class like:

class CStorage {

map<int, vector<CSomething> > m_mData;

CSomething *m_pLastUsed;
};

I'd like to keep a member that points exactly to the 'last used'
element in that m_mData member. I would want to do something like:

void CStorage::Access(int nIndex, int nElement)
{
m_pLastUsed = &m_mData[nIndex][nElement];
}

That is pretty useful but if I want to erase the last used element, how
would I find that in the map to erase it? I can't do something like:
Or what happens if you insert into the map and the memory is reorganised?

Are certain you have a performance bottleneck that requires this
optimisation?
 
J

Jim Langston

markww said:
Hi,

I have a class like:

class CStorage {

map<int, vector<CSomething> > m_mData;

CSomething *m_pLastUsed;
};

I'd like to keep a member that points exactly to the 'last used'
element in that m_mData member. I would want to do something like:

void CStorage::Access(int nIndex, int nElement)
{
m_pLastUsed = &m_mData[nIndex][nElement];
}

That is pretty useful but if I want to erase the last used element, how
would I find that in the map to erase it? I can't do something like:

void CStorage::EraseLastUsed()
{
m_mData.erase(m_pLastUsed);
}

Yeah, how can I do something like that? Maybe keep an iterator to the
last element instead of the type pointer to it?

Thanks

Well, first off you would use an iterator instead of a pointer, which can be
used in the erase. However. There are conditions that can make iterators
invalid in containers. I find it best never to store pointers into
containers or iterators into containers as they are invalid as often as not.
 
A

amirkam1

m_pLastUsed = &m_mData[nIndex][nElement];

As you are using just a map(and not multimap) you can easily store the
key of the last element accessed (which is int in this case). And use
this key to find the element and delete it later. This requires
additional step of finding the element but seems to be safer than
storing the iterator which might give rise to invalid iterator
conditions.

Amir kamerkar
 
K

Kai-Uwe Bux

Jim said:
markww said:
Hi,

I have a class like:

class CStorage {

map<int, vector<CSomething> > m_mData;

CSomething *m_pLastUsed;
};

I'd like to keep a member that points exactly to the 'last used'
element in that m_mData member. I would want to do something like:

void CStorage::Access(int nIndex, int nElement)
{
m_pLastUsed = &m_mData[nIndex][nElement];
}

That is pretty useful but if I want to erase the last used element, how
would I find that in the map to erase it? I can't do something like:

void CStorage::EraseLastUsed()
{
m_mData.erase(m_pLastUsed);
}

Yeah, how can I do something like that? Maybe keep an iterator to the
last element instead of the type pointer to it?

Thanks

Well, first off you would use an iterator instead of a pointer, which can
be
used in the erase. However. There are conditions that can make iterators
invalid in containers. I find it best never to store pointers into
containers or iterators into containers as they are invalid as often as
not.

You just have to know when iterators are invalidated. This is clearly stated
in the standard. The tricky bit is that the various containers behave a
little different in this regard. However, associative containers are really
nice: the only way to invalidate an iterator is to erase the item it refers
to. [23.1.2/8]


Best

Kai-Uwe Bux
 
K

Kai-Uwe Bux

Ian said:
markww said:
Hi,

I have a class like:

class CStorage {

map<int, vector<CSomething> > m_mData;

CSomething *m_pLastUsed;
};

I'd like to keep a member that points exactly to the 'last used'
element in that m_mData member. I would want to do something like:

void CStorage::Access(int nIndex, int nElement)
{
m_pLastUsed = &m_mData[nIndex][nElement];
}

That is pretty useful but if I want to erase the last used element, how
would I find that in the map to erase it? I can't do something like:
Or what happens if you insert into the map and the memory is reorganised?

Nothing [23.1.2/8].
Are certain you have a performance bottleneck that requires this
optimisation?


Best

Kai-Uwe Bux
 
I

Ian Collins

Kai-Uwe Bux said:
Ian Collins wrote:

markww said:
Hi,

I have a class like:

class CStorage {

map<int, vector<CSomething> > m_mData;

CSomething *m_pLastUsed;
};

I'd like to keep a member that points exactly to the 'last used'
element in that m_mData member. I would want to do something like:

void CStorage::Access(int nIndex, int nElement)
{
m_pLastUsed = &m_mData[nIndex][nElement];
}

That is pretty useful but if I want to erase the last used element, how
would I find that in the map to erase it? I can't do something like:

Or what happens if you insert into the map and the memory is reorganised?


Nothing [23.1.2/8].
Oops...
 
M

markww

Ian said:
Kai-Uwe Bux said:
Ian Collins wrote:

markww wrote:

Hi,

I have a class like:

class CStorage {

map<int, vector<CSomething> > m_mData;

CSomething *m_pLastUsed;
};

I'd like to keep a member that points exactly to the 'last used'
element in that m_mData member. I would want to do something like:

void CStorage::Access(int nIndex, int nElement)
{
m_pLastUsed = &m_mData[nIndex][nElement];
}

That is pretty useful but if I want to erase the last used element, how
would I find that in the map to erase it? I can't do something like:


Or what happens if you insert into the map and the memory is reorganised?


Nothing [23.1.2/8].
Oops...

Thanks, for now I just decided to keep two additional members instead,
the map index, then the vector index of the last used item. Using those
it's easy to look up the last element. I just make sure when an element
is deleted I refresh those members.
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top