简体   繁体   中英

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

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];

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM