c++, how do I copy a vector into a vector of vectors?











up vote
1
down vote

favorite












I figured the following code would work, Here's an abridged version of my code:



#include <iostream>
#include <vector>

int main()
{
int xCoordMovement, yCoordMovement;
int rows, cols;
char point = '*';

std::cout << "enter number of rows";
std::cin >> rows;
cols = rows;

std::vector<std::vector<char>> grid(rows, std::vector<char>(cols, '0'));
std::vector<char> flattenedGrid;

for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
std::cout << grid[x][y];
std::cout << std::endl;
}
std::cout << "input for x coord: ";
std::cin >> xCoordMovement;
std::cout << "input for y coord: ";
std::cin >> yCoordMovement;

for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
flattenedGrid.push_back(grid[x][y]);

flattenedGrid[((cols * yCoordMovement) - (rows - xCoordMovement)) - 1] = point;

std::cout << flattenedGrid.size() << std::endl;

for(int i = 0; i < flattenedGrid.size(); i++)
std::cout << flattenedGrid[i];

std::cout << std::endl;

for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];


for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
std::cout << grid[x][y];
std::cout << std::endl;
}

std::cin.ignore();
std::cin.get();

return 0;
}


However it does not seem to change the values of the grid, it should now have a value that is a star at one of it's coordinates, but alas, all grid contains is it's original values.



relevant output:



00000 
00000
00000
00000
00000


desired output:



00000
000*0
00000
00000
00000


Here is the bit I was hoping would assign the values of my vector into my vector of vectors:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];









share|improve this question
























  • Welcome to StackOverflow. Be sure to read about the Minimal, Complete, Verifiable Example. If at all possible, your code should have a main() and be standalone compilable, such as in an online compiler. Anything that's not central to your question should be removed. You should be able to show the output that you get, and explain the output that you wanted. (Saying things like "it should now have a value that's a star at one of it's coordinates" when there is no * in your code isn't actionable.)
    – HostileFork
    2 days ago












  • grid[x][y] = flattenedGrid[y] is just overwriting every row with the first cols entries of flattenedGrid. Also, your linear index ((cols * yCoordMovement) - (rows - xCoordMovement)) - 1 looks very suspicious -- you should check that it's calculating the value you expect. It's not clear why you need to flatten the whole grid just to do this one operation. Why not set the value directly into grid?
    – paddy
    2 days ago












  • I put up my whole program. I tried figuring out how to user cin coordinates for x and y, essentially allowing them to pick points on a coordinate plane. I could not think of a way of doing it without first flattening the grid and adjusting the index for the entered xCoord and yCoord.
    – Ben Jenney
    2 days ago















up vote
1
down vote

favorite












I figured the following code would work, Here's an abridged version of my code:



#include <iostream>
#include <vector>

int main()
{
int xCoordMovement, yCoordMovement;
int rows, cols;
char point = '*';

std::cout << "enter number of rows";
std::cin >> rows;
cols = rows;

std::vector<std::vector<char>> grid(rows, std::vector<char>(cols, '0'));
std::vector<char> flattenedGrid;

for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
std::cout << grid[x][y];
std::cout << std::endl;
}
std::cout << "input for x coord: ";
std::cin >> xCoordMovement;
std::cout << "input for y coord: ";
std::cin >> yCoordMovement;

for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
flattenedGrid.push_back(grid[x][y]);

flattenedGrid[((cols * yCoordMovement) - (rows - xCoordMovement)) - 1] = point;

std::cout << flattenedGrid.size() << std::endl;

for(int i = 0; i < flattenedGrid.size(); i++)
std::cout << flattenedGrid[i];

std::cout << std::endl;

for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];


for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
std::cout << grid[x][y];
std::cout << std::endl;
}

std::cin.ignore();
std::cin.get();

return 0;
}


However it does not seem to change the values of the grid, it should now have a value that is a star at one of it's coordinates, but alas, all grid contains is it's original values.



relevant output:



00000 
00000
00000
00000
00000


desired output:



00000
000*0
00000
00000
00000


Here is the bit I was hoping would assign the values of my vector into my vector of vectors:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];









share|improve this question
























  • Welcome to StackOverflow. Be sure to read about the Minimal, Complete, Verifiable Example. If at all possible, your code should have a main() and be standalone compilable, such as in an online compiler. Anything that's not central to your question should be removed. You should be able to show the output that you get, and explain the output that you wanted. (Saying things like "it should now have a value that's a star at one of it's coordinates" when there is no * in your code isn't actionable.)
    – HostileFork
    2 days ago












  • grid[x][y] = flattenedGrid[y] is just overwriting every row with the first cols entries of flattenedGrid. Also, your linear index ((cols * yCoordMovement) - (rows - xCoordMovement)) - 1 looks very suspicious -- you should check that it's calculating the value you expect. It's not clear why you need to flatten the whole grid just to do this one operation. Why not set the value directly into grid?
    – paddy
    2 days ago












  • I put up my whole program. I tried figuring out how to user cin coordinates for x and y, essentially allowing them to pick points on a coordinate plane. I could not think of a way of doing it without first flattening the grid and adjusting the index for the entered xCoord and yCoord.
    – Ben Jenney
    2 days ago













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I figured the following code would work, Here's an abridged version of my code:



#include <iostream>
#include <vector>

int main()
{
int xCoordMovement, yCoordMovement;
int rows, cols;
char point = '*';

std::cout << "enter number of rows";
std::cin >> rows;
cols = rows;

std::vector<std::vector<char>> grid(rows, std::vector<char>(cols, '0'));
std::vector<char> flattenedGrid;

for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
std::cout << grid[x][y];
std::cout << std::endl;
}
std::cout << "input for x coord: ";
std::cin >> xCoordMovement;
std::cout << "input for y coord: ";
std::cin >> yCoordMovement;

for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
flattenedGrid.push_back(grid[x][y]);

flattenedGrid[((cols * yCoordMovement) - (rows - xCoordMovement)) - 1] = point;

std::cout << flattenedGrid.size() << std::endl;

for(int i = 0; i < flattenedGrid.size(); i++)
std::cout << flattenedGrid[i];

std::cout << std::endl;

for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];


for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
std::cout << grid[x][y];
std::cout << std::endl;
}

std::cin.ignore();
std::cin.get();

return 0;
}


However it does not seem to change the values of the grid, it should now have a value that is a star at one of it's coordinates, but alas, all grid contains is it's original values.



relevant output:



00000 
00000
00000
00000
00000


desired output:



00000
000*0
00000
00000
00000


Here is the bit I was hoping would assign the values of my vector into my vector of vectors:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];









share|improve this question















I figured the following code would work, Here's an abridged version of my code:



#include <iostream>
#include <vector>

int main()
{
int xCoordMovement, yCoordMovement;
int rows, cols;
char point = '*';

std::cout << "enter number of rows";
std::cin >> rows;
cols = rows;

std::vector<std::vector<char>> grid(rows, std::vector<char>(cols, '0'));
std::vector<char> flattenedGrid;

for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
std::cout << grid[x][y];
std::cout << std::endl;
}
std::cout << "input for x coord: ";
std::cin >> xCoordMovement;
std::cout << "input for y coord: ";
std::cin >> yCoordMovement;

for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
flattenedGrid.push_back(grid[x][y]);

flattenedGrid[((cols * yCoordMovement) - (rows - xCoordMovement)) - 1] = point;

std::cout << flattenedGrid.size() << std::endl;

for(int i = 0; i < flattenedGrid.size(); i++)
std::cout << flattenedGrid[i];

std::cout << std::endl;

for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];


for (int x = 0; x < rows; x++)
{
for (int y = 0; y < cols; y++)
std::cout << grid[x][y];
std::cout << std::endl;
}

std::cin.ignore();
std::cin.get();

return 0;
}


However it does not seem to change the values of the grid, it should now have a value that is a star at one of it's coordinates, but alas, all grid contains is it's original values.



relevant output:



00000 
00000
00000
00000
00000


desired output:



00000
000*0
00000
00000
00000


Here is the bit I was hoping would assign the values of my vector into my vector of vectors:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];






c++ vector copy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









Shrikanth N

504210




504210










asked 2 days ago









Ben Jenney

154




154












  • Welcome to StackOverflow. Be sure to read about the Minimal, Complete, Verifiable Example. If at all possible, your code should have a main() and be standalone compilable, such as in an online compiler. Anything that's not central to your question should be removed. You should be able to show the output that you get, and explain the output that you wanted. (Saying things like "it should now have a value that's a star at one of it's coordinates" when there is no * in your code isn't actionable.)
    – HostileFork
    2 days ago












  • grid[x][y] = flattenedGrid[y] is just overwriting every row with the first cols entries of flattenedGrid. Also, your linear index ((cols * yCoordMovement) - (rows - xCoordMovement)) - 1 looks very suspicious -- you should check that it's calculating the value you expect. It's not clear why you need to flatten the whole grid just to do this one operation. Why not set the value directly into grid?
    – paddy
    2 days ago












  • I put up my whole program. I tried figuring out how to user cin coordinates for x and y, essentially allowing them to pick points on a coordinate plane. I could not think of a way of doing it without first flattening the grid and adjusting the index for the entered xCoord and yCoord.
    – Ben Jenney
    2 days ago


















  • Welcome to StackOverflow. Be sure to read about the Minimal, Complete, Verifiable Example. If at all possible, your code should have a main() and be standalone compilable, such as in an online compiler. Anything that's not central to your question should be removed. You should be able to show the output that you get, and explain the output that you wanted. (Saying things like "it should now have a value that's a star at one of it's coordinates" when there is no * in your code isn't actionable.)
    – HostileFork
    2 days ago












  • grid[x][y] = flattenedGrid[y] is just overwriting every row with the first cols entries of flattenedGrid. Also, your linear index ((cols * yCoordMovement) - (rows - xCoordMovement)) - 1 looks very suspicious -- you should check that it's calculating the value you expect. It's not clear why you need to flatten the whole grid just to do this one operation. Why not set the value directly into grid?
    – paddy
    2 days ago












  • I put up my whole program. I tried figuring out how to user cin coordinates for x and y, essentially allowing them to pick points on a coordinate plane. I could not think of a way of doing it without first flattening the grid and adjusting the index for the entered xCoord and yCoord.
    – Ben Jenney
    2 days ago
















Welcome to StackOverflow. Be sure to read about the Minimal, Complete, Verifiable Example. If at all possible, your code should have a main() and be standalone compilable, such as in an online compiler. Anything that's not central to your question should be removed. You should be able to show the output that you get, and explain the output that you wanted. (Saying things like "it should now have a value that's a star at one of it's coordinates" when there is no * in your code isn't actionable.)
– HostileFork
2 days ago






Welcome to StackOverflow. Be sure to read about the Minimal, Complete, Verifiable Example. If at all possible, your code should have a main() and be standalone compilable, such as in an online compiler. Anything that's not central to your question should be removed. You should be able to show the output that you get, and explain the output that you wanted. (Saying things like "it should now have a value that's a star at one of it's coordinates" when there is no * in your code isn't actionable.)
– HostileFork
2 days ago














grid[x][y] = flattenedGrid[y] is just overwriting every row with the first cols entries of flattenedGrid. Also, your linear index ((cols * yCoordMovement) - (rows - xCoordMovement)) - 1 looks very suspicious -- you should check that it's calculating the value you expect. It's not clear why you need to flatten the whole grid just to do this one operation. Why not set the value directly into grid?
– paddy
2 days ago






grid[x][y] = flattenedGrid[y] is just overwriting every row with the first cols entries of flattenedGrid. Also, your linear index ((cols * yCoordMovement) - (rows - xCoordMovement)) - 1 looks very suspicious -- you should check that it's calculating the value you expect. It's not clear why you need to flatten the whole grid just to do this one operation. Why not set the value directly into grid?
– paddy
2 days ago














I put up my whole program. I tried figuring out how to user cin coordinates for x and y, essentially allowing them to pick points on a coordinate plane. I could not think of a way of doing it without first flattening the grid and adjusting the index for the entered xCoord and yCoord.
– Ben Jenney
2 days ago




I put up my whole program. I tried figuring out how to user cin coordinates for x and y, essentially allowing them to pick points on a coordinate plane. I could not think of a way of doing it without first flattening the grid and adjusting the index for the entered xCoord and yCoord.
– Ben Jenney
2 days ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










There are a couple of changes you have to make to the code:




  • First, when you assign point to an element of flattenedGrid


Change:



 flattenedGrid[((cols * yCoordMovement) - (rows - xCoordMovement)) - 1] = point;


to:



flattenedGrid[(yCoordMovement) + (rows * xCoordMovement)] = point;



  • Second, when you refill grid with elements of flattenedGrid


Change:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];


to:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
grid[x][y] = flattenedGrid[rows * x + y];





share|improve this answer























  • Thank you! this is definitely superior to what I had.
    – Ben Jenney
    22 hours ago










  • @BenJenney: You are welcome. :)
    – P.W
    22 hours ago











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367282%2fc-how-do-i-copy-a-vector-into-a-vector-of-vectors%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










There are a couple of changes you have to make to the code:




  • First, when you assign point to an element of flattenedGrid


Change:



 flattenedGrid[((cols * yCoordMovement) - (rows - xCoordMovement)) - 1] = point;


to:



flattenedGrid[(yCoordMovement) + (rows * xCoordMovement)] = point;



  • Second, when you refill grid with elements of flattenedGrid


Change:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];


to:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
grid[x][y] = flattenedGrid[rows * x + y];





share|improve this answer























  • Thank you! this is definitely superior to what I had.
    – Ben Jenney
    22 hours ago










  • @BenJenney: You are welcome. :)
    – P.W
    22 hours ago















up vote
0
down vote



accepted










There are a couple of changes you have to make to the code:




  • First, when you assign point to an element of flattenedGrid


Change:



 flattenedGrid[((cols * yCoordMovement) - (rows - xCoordMovement)) - 1] = point;


to:



flattenedGrid[(yCoordMovement) + (rows * xCoordMovement)] = point;



  • Second, when you refill grid with elements of flattenedGrid


Change:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];


to:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
grid[x][y] = flattenedGrid[rows * x + y];





share|improve this answer























  • Thank you! this is definitely superior to what I had.
    – Ben Jenney
    22 hours ago










  • @BenJenney: You are welcome. :)
    – P.W
    22 hours ago













up vote
0
down vote



accepted







up vote
0
down vote



accepted






There are a couple of changes you have to make to the code:




  • First, when you assign point to an element of flattenedGrid


Change:



 flattenedGrid[((cols * yCoordMovement) - (rows - xCoordMovement)) - 1] = point;


to:



flattenedGrid[(yCoordMovement) + (rows * xCoordMovement)] = point;



  • Second, when you refill grid with elements of flattenedGrid


Change:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];


to:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
grid[x][y] = flattenedGrid[rows * x + y];





share|improve this answer














There are a couple of changes you have to make to the code:




  • First, when you assign point to an element of flattenedGrid


Change:



 flattenedGrid[((cols * yCoordMovement) - (rows - xCoordMovement)) - 1] = point;


to:



flattenedGrid[(yCoordMovement) + (rows * xCoordMovement)] = point;



  • Second, when you refill grid with elements of flattenedGrid


Change:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
for (int i = 0; i < flattenedGrid.size(); i++)
grid[x][y] = flattenedGrid[i];


to:



for (int x = 0; x < rows; x++)
for (int y = 0; y < cols; y++)
grid[x][y] = flattenedGrid[rows * x + y];






share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered 2 days ago









P.W

7,6602438




7,6602438












  • Thank you! this is definitely superior to what I had.
    – Ben Jenney
    22 hours ago










  • @BenJenney: You are welcome. :)
    – P.W
    22 hours ago


















  • Thank you! this is definitely superior to what I had.
    – Ben Jenney
    22 hours ago










  • @BenJenney: You are welcome. :)
    – P.W
    22 hours ago
















Thank you! this is definitely superior to what I had.
– Ben Jenney
22 hours ago




Thank you! this is definitely superior to what I had.
– Ben Jenney
22 hours ago












@BenJenney: You are welcome. :)
– P.W
22 hours ago




@BenJenney: You are welcome. :)
– P.W
22 hours ago


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367282%2fc-how-do-i-copy-a-vector-into-a-vector-of-vectors%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'