从Vectores的向量创建一个板
Create a board from a vector of vectores
我遇到了一些问题,创建了向量的向量
我需要
之类的东西 a b c d e f g h i j
A . . . . . . . . . .
B . . . . . . . . . .
C . . . . . . . . . .
D . . . . . . . . . .
E . . . . . . . . . .
F . . . . . . . . . .
G . . . . . . . . . .
H . . . . . . . . . .
I . . . . . . . . . .
J . . . . . . . . . .
大小可以更改(取决于我已经在构造函数上"提取"的一些TXT信息)。我尝试了这样的事情:
void Board::display() const{
//string filename;
Board b1("configp1.txt");
int upC = 65, lowC = 97; //ascii code for low a and upper A
int icc = 0; //int char coluna
cout << b1.numLines << endl;
cout << b1.numColumns << endl;
cout << "lol" << endl;
for(int i = 0; i < b1.numLines; i++){
for (int j = 0; j < b1.numColumns; j++){
board[i][j] == '.';
}
}
while (icc < b1.numLines){ //o int char coluna vai aumentando consoante as dimensoes do tabuleiro || e.g Se tivermos um tab 10x10
cout << (char)lowC << " "; //teremos um range de 65-75 [a-j] para as colunas e um range 97 -107 [A-J] para as linhas
icc++;
lowC++;
}
cout << endl;
icc = 0;
for (int y = 0; y<b1.numLines; y++)
{
cout << (char)upC << " ";
for (int x = 0; x < b1.numColumns; x++){
cout << board[y][x] << " ";
}
cout << endl;
upC++;
}
董事会构造函数:
Board::Board(const string &filename){
string nome;
unsigned int size;
char simb;
unsigned int cor;
char ori;
PositionChar position;
string tmp;
ifstream config;
config.open(filename.c_str()); //abre o ficheiro config onde estao as informacoes do tabuleiro
if (config.is_open()) {
config >> tmp >> numLines >> tmp >> numColumns;
while (!config.eof()) {
config >> simb >> tmp >> position.lin >> position.col >> tmp >> ori >> tmp >> size >> tmp >> cor;
if(!config.fail()){
ships.push_back(Ship(simb, position, ori, size, cor));
}
}
}
else{
cout << "Ficheiro de config invalido" << endl;
exit(1);
}
config.close();
}
董事会课程:
class Board {
public:
Board(const string &filename);
int putShip(const Ship &s);
void moveShips();
//bool checkLimits()
//bool attack(const Bomb &b);
void display()const;
void show()const;
int getLines();
int getColumns();
private:
int numLines, numColumns;
vector<Ship> ships;
vector <vector <int> > board;
};
,但根本什么也没发生。
有人可以帮我吗?
最好的问候
您的板定义为向量的向量: vector <vector <int> > board;
但是在构造函数中,您不初始化它。您也没有在显示功能中。所以它是空的。在这种情况下,尝试访问board[i][j]
将超出范围。
解决方案:
我建议在构造函数中建议,一旦知道尺寸,就可以用:
调整您的向量大小 board.resize(numLines, vector<int> (numColumns,'.'));
其他问题:
请注意,您打算修改board
时,以下代码不应在display() const
中:
for(int i = 0; i < b1.numLines; i++){
for (int j = 0; j < b1.numColumns; j++){
board[i][j] == '.'; /// <=======OOOPS ! Won't change anything this is comparison
}
}
这可能仅是因为您写了==
,因此将board[i][k]
与'.'
进行比较而不更改。这可以解释为什么什么都没有发生(显示0不会产生任何可见的东西)。
请注意,Board::display()
中还有另一个潜在问题。您可以在其中创建局部变量Board b1
,以便使用b1.numLines
和b1.numColumns
。这很容易出错:您应该摆脱这种无用的b1
,直接参考numLines
和numColumns
,它们都是构件变量,具有确保在其自己的构造函数中为对象设置的值。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 从链接列表c++中删除一个项目
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '