Okay, I've got a serious problem with std::vector, the code I'm working with is huge and it's got vectors left, right and center. The problem is that one of the vectors is having to reallocate itself elsewhere. This is causing the references to point to random things.
For illustrative purposes:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> Vector1;
Vector1.push_back(12);
Vector1.reserve(1);
std::vector<int>::reference Reference = Vector1[0];
cout << Vector1.capacity() << " " << Reference << endl;
vector<int> Vector2;
Vector2.push_back(11);
Vector1.reserve(2);
cout << Vector1.capacity() << " " << Reference << endl;
return 0;
}
Ive tried using int&
, const int&
, std::vector<int>::reference
and the various iteratorts; none of them seem to be able to remain valid through the relocation. I cant think of any means to get around this, what should I do?