References to elements of a vector
When using references it's important to note that if the reference is pointing to a place in a vector that that reference may get invalidated if the vector has to reallocate, see this:
#include <iostream>
#include <vector>
struct MyStruct {
int value;
};
int main() {
std::vector<mystruct> vec;
// add one element
vec.push_back({42});
// get a reference to the first element
MyStruct& ref = vec[0];
std::cout << "Before adding more elements, ref.value = " << ref.value << std::endl;
// add more elements, likely causing reallocation
for (int i = 0; i < 1000; ++i) {
vec.push_back({i});
}
// the reference may now be invalid!
std::cout << "After adding more elements, ref.value = " << ref.value << " (undefined behavior!)" << std::endl;
return 0;
}
On my system the result of the above code is this:
Before adding more elements, ref.value = 42
After adding more elements, ref.value = -1170156367 (undefined behavior!)