C++ 矩阵类输入运算符重载>&燃气轮机;在C++;
我创建了一个矩阵类,其中包含一个点对象的向量向量(我创建的另一个类)。矩阵中的每个点都可行走或不可行走(矩阵实际上是一个迷宫)。1是可行走的,0不是。 我想得到以下形式的矩阵: 4//迷宫的大小(矩阵) 1 0 1 0(输入) 110(输入) 0 1 1 0(输入) 1 0 1 1(输入) 我试图一行一行地获取数据,然后用流字符串将点(1和0)分开。这是我的代码:C++ 矩阵类输入运算符重载>&燃气轮机;在C++;,c++,matrix,input,overloading,operator-keyword,C++,Matrix,Input,Overloading,Operator Keyword,我创建了一个矩阵类,其中包含一个点对象的向量向量(我创建的另一个类)。矩阵中的每个点都可行走或不可行走(矩阵实际上是一个迷宫)。1是可行走的,0不是。 我想得到以下形式的矩阵: 4//迷宫的大小(矩阵) 1 0 1 0(输入) 110(输入) 0 1 1 0(输入) 1 0 1 1(输入) 我试图一行一行地获取数据,然后用流字符串将点(1和0)分开。这是我的代码: istream & operator >> (istream& input, maze inMa
istream & operator >> (istream& input, maze inMaze) {
string rowStream;
string tmpWord;
int num, colCounter; //num = 1 or 0, colCounter = col index
for (int rowIndex = 0; rowIndex < inMaze.rowsSize; rowIndex++){ //over the rows
int colIndex = 0;
bool isWalkable;
input >> rowStream; //input to string
stringstream seperateWord(rowStream); //string to stream string
while (seperateWord >> tmpWord) { //sstring seperate space bars in string, reprasant a row
if (tmpWord == "0") isWalkable = false; //in maze matrix, zero means not a path
else if (tmpWord == "1") isWalkable = true; //else 1 = a path
else throw "invalid input"; //wrong input (num in matrix not 0 nor 1)
inMaze.getMaze[rowIndex][colIndex].setPoint(rowIndex, colIndex, isWalkable); //set point in maze
colIndex++; //next col
} //done filling a row, to next row
}
istream&operator>>(istream&inMaze输入){
字符串行流;
字符串tmpWord;
int num,colCounter;//num=1或0,colCounter=col索引
对于(int-rowIndex=0;rowIndex>行流;//输入到字符串
stringstream separateword(rowStream);//字符串到流字符串
(separateword>>tmpWord){//s在字符串中分隔空格,表示一行
如果(tmpWord==“0”)isWalkable=false;//在迷宫矩阵中,零表示不是路径
else if(tmpWord==“1”)isWalkable=true;//else 1=a路径
else抛出“无效输入”;//输入错误(矩阵中的num不是0也不是1)
inMaze.getMaze[rowIndex][colIndex].设定点(rowIndex,colIndex,isWalkable);//迷宫中的设定点
colIndex++;//下一列
}//填充完一行,转到下一行
}
}
它不起作用。它总是在第一行之后得到输入,并用1填充所有内容。
我做错了什么
谢谢你的帮助!对不起,我的英语很差……-) 行
输入>>行流中的问题代码>将其替换为getline(输入,行)代码>和行
是字符串
类型
file take one参数的运算符>
以空格结尾
比如:00111
,第一个参数是00
,第二个参数是1
在您的情况下,使用stringstream
,这意味着您必须使用整行
比如:0101
,使用getline获取它,然后使用stringstream
获得正确的输入<代码>0
然后1
…行输入>>行流中的问题代码>将其替换为getline(输入,行)代码>和行
是字符串
类型
file take one参数的运算符>
以空格结尾
比如:00111
,第一个参数是00
,第二个参数是1
在您的情况下,使用stringstream
,这意味着您必须使用整行
比如:0101
,使用getline获取它,然后使用stringstream
获得正确的输入<代码>0
然后1
…您应该通过引用而不是值复制来获取矩阵参数:istream&operator>>(istream&input,maze&inMaze){
下次在这里询问时,请注意准确解释什么不起作用,并根据站点策略中的要求提供一个。您应该通过引用而不是值复制来获取矩阵参数:istream&operator>>(istream&input,maze&inMaze){
下次在这里询问时,请注意准确解释什么不起作用,并根据站点策略中的要求提供一个。