For example: if I use a variable, which references an element of a vector, and then the capacity of the vector changes, my reference becomes an invalid reference.
#include <vector>
#include <iostream>
std::vector<int> v = {1, 2, 3};
int main() {
int &r = v[0];
std::cout << r << std::endl;
v.reserve(256);
std::cout << r << std::endl;
std::cout << v[0] << std::endl;
}
Any way I can avoid this? Or just don't refer to a vector element?