473,395 Members | 1,919 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

pointer to a map

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

Aug 22 '06 #1
7 2111
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?

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

--
Ian Collins.
Aug 22 '06 #2
"markww" <ma****@gmail.comwrote in message
news:11********************@m79g2000cwm.googlegrou ps.com...
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.
Aug 22 '06 #3
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

Aug 22 '06 #4
Jim Langston wrote:
"markww" <ma****@gmail.comwrote in message
news:11********************@m79g2000cwm.googlegrou ps.com...
>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
Aug 22 '06 #5
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].
Are certain you have a performance bottleneck that requires this
optimisation?

Best

Kai-Uwe Bux

Aug 22 '06 #6
Kai-Uwe Bux wrote:
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...

--
Ian Collins.
Aug 22 '06 #7

Ian Collins wrote:
Kai-Uwe Bux wrote:
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...

--
Ian Collins.
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.

Aug 22 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
35
by: tuko | last post by:
Hello kind people. Can someone explain please the following code? /* Create Storage Space For The Texture */ AUX_RGBImageRec *TextureImage; /* Line 1*/ /* Set The Pointer To NULL...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
16
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
8
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.