我正在编写一个允许输入类似俄罗斯方块形状的程序。我存储在布尔的2-d矢量这些形状所以他们会再看看这样的:访问多维矢量指针向量中的元素
110 | 1 | 111
011 | 1 | 010
| 1 | 111
// Where a 0 denotes "empty space"
我再点到每台2-d载体和这些指针存储在一个名为矢量形状。 我的问题在于访问那些个人的0和1(为了将它们与其他形状进行比较)。
例如,给定:
vector<vector<bool> > Shape;
vector<Shape *> shapes;
其中形状具有指向2-d向量I先前给出了三个元件,我希望能够访问1中的(0,1)位置第一个形状。
我已经试过:
shapes[index]->at(0).at(1);
shapes[index]->at(0)[1];
shapes[index][0][1];
在许多其他事情,但没有这些似乎是给我我想要的东西。我还是比较新的指针,所以我希望我不会错过明显的东西。
预先感谢您!
根据要求,这里是我的代码更大块。
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
typedef vector<vector<bool> > Shape;
class ShapeShifter {
public:
ShapeShifter(int argc, char **argv);
void Apply(int index, int row, int col);
bool FindSolution(int index);
void AddShape(Shape *newShape);
void AddMove(int index, int row, int col);
void PrintMoves();
void PrintGrid();
protected:
vector<vector<bool> > grid;
vector<Shape *> shapes;
vector<string> moves;
};
void ShapeShifter::Apply(int index, int row, int col) {
int i, j, k;
int y = 0, z = 0;
if((row + shapes[index]->size() > grid.size()) || (col + shapes[index]->at(0).size() > grid[0].size())) {
return; // shape won't fit
}
for(i = row; i < (row + shapes[index]->size()); i++) {
for(j = col; j < (col + shapes[index]->at(0).size()); j++) {
if(shapes[index]->at(y)[z] == 1) {
if(grid[i][j] == 0) {
grid[i][j] = 1;
}
else {
grid[i][j] = 0;
}
}
z++;
}
z = 0;
y++;
}
return;
}
在此我的bool的网格和我想要与形状,以掩盖它在给定的索引,其中如果所述形状具有1中的网格的对应元素的布尔将被翻转。
形状矢量填充与标准输入线,像这样:
ShapeShifter sshift(argc, argv);
Shape *newShape;
vector<bool> shapeLine;
int i, j;
string line;
while(getline(cin, line)) {
j = 0;
newShape = new Shape;
for(i = 0; i < line.size(); i++) {
if(line[i] == ' ') {
j++;
}
else {
shapeLine.push_back(line[i] - '0');
}
}
newShape->push_back(shapeLine);
sshift.AddShape(newShape);
line.clear();
}
void ShapeShifter::AddShape(Shape *newShape) {
shapes.push_back(newShape);
}
是什么'给我什么,我want'的意思吗?你在期待什么?你得到了什么? –
发布代码段没有帮助。请发布[mcve]。 –
当您评估最后发布的三个表达式时,您会得到什么值。 –