1

I'm new to vectors.

I have two vectors, vector1 and vector2, both have two values each. Now, using these two vectors, I have made a 2 dimensional vector, vector_2d whose value(contents) I want to print. I use the below code and everything works fine.

vector<int> vector1;
vector<int> vector2;

vector1.push_back(10);
vector1.push_back(20);

vector2.push_back(100);
vector2.push_back(200);

vector_2d.push_back(vector1);
vector_2d.push_back(vector2);

cout<<"The elements in vector_2d are: "<<vector_2d.at(0).at(0)<<" "<<vector_2d.at(0).at(1)<<" "<<vector_2d.at(1).at(0)<<" "<<vector_2d.at(1).at(1)<<endl;

Now, I want to replace the first value in vector1 (which is 10) with 1000. I do it by a simple assignment operator:

vector1.at(0) = 1000;

Now, I try to print vector1 and vector_2d again. I get the result that I expected with vector1:

cout<<vector1.at(0)<<endl; //1000

But when I print vector_2d, I get the same result as before. The changes done in vector1 are not being reflected in the 2D vector. Why is this happening?

Aditya Prakash
  • 1,549
  • 2
  • 15
  • 31
  • 8
    When you `push_back` into `vector_2d` you are making a copy. It is not the same as the original. – ChrisMM Nov 28 '19 at 11:09
  • @ChrisMM Can you elaborate on that please? What does a "copy" mean in this context? Is it that the compiler is creating a similar vector and and using it to `push_back` into the `vector_2d`? If so, then how can I make sure that both these vectors are in sync with each other, and the value changes (if any) are reflected throughout the code? – Aditya Prakash Nov 28 '19 at 11:14
  • 2
    You can try instead: `vector_2d.push_back(vector()); vector_2d.back().push_back(10); // or vector_2d[0].push_back(10);`. – Scheff's Cat Nov 28 '19 at 11:16
  • 4
    Internally, it is a bit more complex, but a copy of a vector is as much a copy as a copy of an int is: `int x = 10, int y = x; y = 12;` won't modify `x` either... – Aconcagua Nov 28 '19 at 11:16
  • 4
    If you *promise* that the two single vectors live at least as long as the 2d vector, you could place them in a vector of pointers or references (you'll need `std::reference_wrapper` then). If you don't keep the promise, you'll end up with dangling pointers or references, so a potentially dangerous approach. – Aconcagua Nov 28 '19 at 11:18
  • @Aconcagua That makes sense to me. Thanks! – Aditya Prakash Nov 28 '19 at 11:20
  • @Scheff So how exactly is that going to keep the two vectors in sync? If I understand your code correctly, I am pushing value "10" to the first index of `vector_2d`. – Aditya Prakash Nov 28 '19 at 11:22
  • 1
    Well. The first element of `vector_2d` is of type `vector`. Hence, `push_back()` can be applied. There is no option to _keep the two vectors in sync_. For that, you had to use a `std::vector&>`... (but I wouldn't recommend this.) However, I once wrote an answer which might be of interest: [SO: vector of existing objests](https://stackoverflow.com/a/42039844/7478597) – Scheff's Cat Nov 28 '19 at 11:24
  • @Scheff Ahhhh right... sounds a bit like "vector-ception" :P Thanks btw, this helps. – Aditya Prakash Nov 28 '19 at 11:30
  • @Scheff `std::vector` is invalid for all `T`. You probably mean `std::vector>>` – Caleth Nov 28 '19 at 11:45
  • @Caleth You are correct (and my comment a bit sloppy). Actually, what you proposed is just what I did in the link I provided. – Scheff's Cat Nov 28 '19 at 11:46

1 Answers1

1

The value in your 2D vector is not changed, because while pushing back 1D vectors

vector_2d.push_back(vector1);
vector_2d.push_back(vector2);

you made copies of each of them. Therefore, changing values in vector1 will not change the values in its copy stored as element 0 of vector_2d. If you would like to change the values in 2D vector, you can do it directly:

vector_2d.at(0).at(0) = 0;

Another possibilities were mentioned in the comments to your question (like having a vector of pointers or references), but I do not recommend those because of possible memory violations (e.g. if your 2D vector will live longer than 1D vectors that are referenced).

pptaszni
  • 5,591
  • 5
  • 27
  • 43