std::vector<MYSTRUCT>vecMyDyArray;
MYSTRUCT object;
... intialize here....
vecMyDyArray.pushbach(object);
MYSTRYCT *pointer_to_a_element = &vecMyDyArray.back();
Will pointer_to_a_element point to the last element in the array? And the second last element when I add another object to the array?
Zeblar Nagrim, Lord of Chaos
std::vector
Is this possible (I know stupid question...):
std::vector::back() returns the last element in the array. std::vector retains the order of its elements unless you tell it to change (i.e., std::sort, std::remove_if, etc.). If you add another element then the previous value returned by std::vector::back() will indeed be the next-to-last element.
You must be careful when taking the addresses of elements stored in standard containers. The standard containers store by value (as opposed to storing by reference), which means that they can freely create a destroy objects in memory as long as the objects'' values remain the same. This will cause the addresses of the stored elements to change.
If you are using Microsoft Visual C++, then look in the VC++ docs under "vector" in the section "C++ Languages and Libraries" or something like that, and take note of which member functions may cause "reallocation" to occur.
You must be careful when taking the addresses of elements stored in standard containers. The standard containers store by value (as opposed to storing by reference), which means that they can freely create a destroy objects in memory as long as the objects'' values remain the same. This will cause the addresses of the stored elements to change.
If you are using Microsoft Visual C++, then look in the VC++ docs under "vector" in the section "C++ Languages and Libraries" or something like that, and take note of which member functions may cause "reallocation" to occur.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement