clqrq@yahoo.de wrote:
Quote:
i want to do the following....
>
1) fill a struct (lets call it Data)
2) push it to a vector of Data
3) get a pointer to pushed vector-element.
>
std::vector<DatavData;
>
(lets assume we now filled some elements into the vector, erased some,
and now do the following...)
>
Data x = /* something */ y;
vData.push_back(x);
>
my question is:
>
is there a more simplier way to get a pointer to the new vector-element
than
>
Data* pData = &vData[vData.size() - 1];
>
and IS it the correct pointer? does push always add to the end?
vData[vData.size() - 1] is also called vData.back(), so yes, this
works.
It is the correct pointer. However, the real question is, WILL it be
the correct
pointer? A vector will move the Data objects around when adding new
Data
(not for every Data, that would be too slow though). Can you ensure
pData
is not used after vData changes? If not, you should remember both
&vData
and vData.size(). That also allows you to deal with the situation where
vData shrinks.
HTH,
Michiel Salters