0

I have a question in my mind. Lets say I have two vectors called vector1 and vector2.

vector <int> vector1{1,2};
vector <int> vector2{3,4};

Now I want to create a 2-D vector called vector_2d and assign these two vectors into my new 2-D vector using push_back function.

vector <vector <int>> vector_2d;
vector_2d.push_back(vector1);
vector_2d.push_back(vector2);

How C++ decides to assign the vector2 to the second row of the vector_2d? Why it didn't add these two vectors back to back?

Also I tried to add the vector1 multiple times to a new 2-D vector called new_vector. But it seems to be add vector1 only once. Why is this the case? Why it didn't add multiple vector1 to new rows or back to back?

vector <vector <int>> new_vector;
new_vector.push_back(vector1);
new_vector.push_back(vector1);
new_vector.push_back(vector1);
new_vector.push_back(vector1);
efe373
  • 151
  • 2
  • 11
  • 3
    How do you know that it only added vector1 once? How did you measure that? – user253751 Jul 15 '20 at 13:17
  • 5
    `push_back` == push onto the back. – NathanOliver Jul 15 '20 at 13:17
  • @user253751 I tried to print new_vector.at(1).at(0) or new_vector.at(0).at(2), and both gave me error. – efe373 Jul 15 '20 at 13:18
  • 1
    "Why it didn't add multiple vector1 to new rows or back to back?" You do exactly that. You add a copy of the vector1 to the back of new_vector – Klaus Jul 15 '20 at 13:18
  • 1
    I dont understand the question – Sebastian Hoffmann Jul 15 '20 at 13:19
  • @NathanOliver So why the vector2 added to the second row instead of the first row back to the vector1? – efe373 Jul 15 '20 at 13:19
  • 2
    Please run your program under a debugger and inspect `new_vector`. It will give you a better understanding of the resulting object structure than printing ever can. – Botje Jul 15 '20 at 13:19
  • 1
    @EfeBerkayYitim If you are told to go to the back of the line, do you go to the beginning or the end? You go to the end, the last position. So vector1 was added first, so it is first, then you add vector 2 to the back, so now it is at the end and is the second element. – NathanOliver Jul 15 '20 at 13:21
  • " new_vector.at(0).at(2), " there is no "2" in the vector as you have only two elements in it which means the last valid entry is 1 – Klaus Jul 15 '20 at 13:21
  • @Klaus but also there isnt "new_vector.at(1).at(0)", so where did my other 3 assignments go? – efe373 Jul 15 '20 at 13:22
  • *"Why it didn't add these two vectors back to back?"* Can you please elaborate on what you think that would look like in the final result and how that differs from what you observe? – François Andrieux Jul 15 '20 at 13:22
  • "but also there isnt "new_vector.at(1).at(0)"" You are wrong, the element is there! So simply complete your code example, post it here and also the results you get. On my machine your code works as expected. – Klaus Jul 15 '20 at 13:24
  • @Klaus Thank you, you are right. I was getting the error at somewhere else in my code, my bad. – efe373 Jul 15 '20 at 13:27
  • 1
    Voting to close as "can no longer be reproduced". – DevSolar Jul 15 '20 at 13:28

1 Answers1

5

How C++ decides to assign the vector2 to the second row of the vector_2d?

By reading your code.

You're adding two "inner" vectors to your outer vector, resulting in two elements.

That's what happens when you call push_back on the outer vector.

Why it didn't add these two vectors back to back?

Because you didn't tell it to.

That would be:

vector <vector <int>> vector_2d;
vector_2d.push_back(vector1);
std::copy(std::begin(vector2), std::end(vector2), std::back_inserter(vector_2d[0]));

Also I tried to add the vector1 multiple times to a new 2-D vector called new_vector. But it seems to be add vector1 only once. Why is this the case?

It isn't.

Why it didn't add multiple vector1 to new rows or back to back?

It did. You must have observed it wrong somehow.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35