如何调整结构的二维向量 [英] How to resize a 2D vector of struct

查看:38
本文介绍了如何调整结构的二维向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C++ 制作扫雷游戏,但在调整矢量大小时遇到​​问题.

I'm working on making a minesweeper game using C++ but I'm having trouble re sizing a vector.

这是我所拥有的:整数向量:

Here's what I have: A vector of ints:

vector<vector<int> > mineField;

结构向量:

struct cell{
     int value;                //(-1 mine, 0 no surrounding, # > 0 number of surrounding mines)
     int state;            //( 0 hidden, 1 revealed, 2 marked) 
     bool isMine;
};

vector<vector<cell> > mineField;

cell 的向量位于扫雷艇类的单独 .cpp 文件中.我想要做的是重新调整单元格向量的大小,使其与整数向量具有相同的维度.并将结构变量 value 初始化为整数向量和结构中的值变量 state 为 0.

The vector of cell is located in a separate .cpp file of a minesweeper class. What I want to do is re size the vector of cell to have the same dimensions as the vector of ints. and initialize the struct variable value to the values in the vector of ints and the struct variable state to 0.

这是我迄今为止尝试过的:

This is what I have tried so far:

this->mineField.resize(rowNum, vector<cell>(colNum));

    for(int i = 0; i < rowNum; i++){
        for(int j = 0; j < colNum; j++){

            this->mineField[i][j].state = 0;

            this->mineField[i][j].value = mineField[i][j];
        }
    }

尝试运行时,我只能拥有 5 行 x 5 列的尺寸(我不知道为什么).任何其他维度退出程序,netbeans 告诉我运行失败.

When attempt to run this I am only able to have the dimensions 5 rows x 5 columns (I cannot figure out why). Any other dimensions exits the program and netbeans tells me the run has failed.

我也试过:

this->mineField.clear();
    for (int i = 0; i < rowNum; i++){

        this->mineField.push_back(vector<cell>(colNum, 0));

    }

    for(int i = 0; i < rowNum; i++){
        for(int j = 0; j < colNum; j++){

            this->mineField[i][j].state = 0;

            this->mineField[i][j].value = mineField[i][j];
        } 
    }

尝试以这种方式调整大小时,没有任何效果.

When trying to resize this way, nothing works.

这样:

this->mineField.resize(rowNum);

    for(int i = 0; i < rowNum; i++){

        this->mineField[i].resize(colNum);

    }

    for(int i = 0; i < rowNum; i++){
        for(int j = 0; j < colNum; j++){

            this->mineField[i][j].state = 0;

            this->mineField[i][j].value = mineField[i][j];
        } 
    }

尝试这样做可以让程序运行,但不适用于任何维度组合.

Attempting this lets the program run but doesn't work for any dimensions combination.

非常感谢任何帮助,谢谢.

Any help is greatly appreciated, thank you.

推荐答案

据我所知,这应该可行.

As I understand you question, this should work.

vector<vector<cell > > mineField;
vector< vector<int> > mineFeildInt;

int size=mineFeildInt.size();//get the size of the vector for vector of integers
minefield.resize(size);//resize the vector for vector of cells

for(int i=0;i<size;i++)
{
   int inner_size=mineFeildInt[i].size();//get the size of the vector of integers
   minefield[i].resize(inner_size);//resize the vector of cells
   for(int j=0;j<inner_size;j++)
   {
      minefield[i][j].state=0;
      minefield[i][j].value=mineFeildInt[i][j];//assign the integer value from the vector of integer to the value member of the structure
   }
}

这篇关于如何调整结构的二维向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆