This question already has an answer here:
- Nice way to append a vector to itself 4 answers
I have an STL vector that stores the diagonals of a matrix. A certain mathematical rule I'm implementing tells me that I can produce a new matrix of a tensor product's diagonals simply by taking the original vector and concatenating a copy of that vector onto itself (it doubles in size, and the values repeat after 1/2 * size() ).
I wrote the following code:
std::vector<int> aVec;
for (int k = 0; k < aVec.size(); k++) aVec.insert(aVec.end(), aVec[k]);
But I get seg-faults when I try this. If I create a copy of aVec and use that as the insert "value", as well as using it for the size() in the loop args, it will work, but I must do both of these things (otherwise I will still get seg-faults).
Can anyone explain what's going on underneath that makes this implementation not work?