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?