迷宫程序中的字符串下标错误

时间:2014-05-01 00:55:11

标签: c++ string vector subscript file-io

我需要为类创建一个程序,将Maze的文件输入到二维向量中,这样计算机就可以将向量返回到类Maze。我目前正致力于将迷宫输入加载到矢量中的功能。每次运行程序时,它都会在第一个条件语句处停止,该条件语句用于检查每一行是否相等。它" couts"错误,然后从MVS工作室得到中止错误,说字符串下标超出范围。我一直在网上搜索可能的错误但没有任何帮助。有人可以告诉我我做错了吗?

以下是代码:

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

struct Location  {
    friend std::ostream &operator <<(std::ostream &os, const Location &location) {
        os << "(" << location.x << ", " << location.y << ")";
        return os;
    }
    //bool operator ==(const Location &rhs) const {return x == rhs.x && y == rhs.y;}
    //bool operator !=(const Location &rhs) const {return !(*this == rhs);}
    //operator bool() const {return x >= 0;}
    Location(int x=-1, int y=-1) : x(x), y(y) {}
    int x, y;
};

int main()
{

    ifstream infile;
    infile.open("Maze.txt");

    vector<vector<bool> > mazeSpec;   //row vector
    vector<bool> column;
    string top,currlin;
    int row=0, col=0;
    Location start, finish;

    getline(infile, top);
    int size= top.length();
    for(int i=0; i<size; i++){
        column.push_back(false);
    }
    cout<<top<<endl;
    cout<<top.length();
    //getline(infile,currlin);
    //cout<<(currlin);
    //cout<<currlin.length();

    mazeSpec.push_back(column);
    column.clear();
    row++;

    while (getline(infile,currlin)){

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

            if ((currlin.length()) != size){
                    //throw "Error! Line of Maze is too short!"; //check to see if rows of equal length
                cout<<"Error!";}
                    //break;}

            else if (currlin[i] == 'S'){
                    column.push_back(true);
                    start= (row, i);}

            else if (currlin[i] == 'F'){
                    column.push_back(true);
                    finish= (row, i);}

            else if (currlin[i] == ' ' || '+' || '|'){
                    column.push_back(false);
            }

            else if(currlin[i]== '*'){
                column.push_back(true);}
        }
            row++;
            currlin.empty();
            mazeSpec.push_back(column);
            column.clear();
    }





    infile.close();

    int x;
    cin>>x;
    cin.ignore();

    return 0;
}

以下是迷宫文字:

+----------------------------------------+
|*                           F           |
|*      ***** *********      *           |
|****   *   * *       *      *           |
|   *   *   * *       ********           |
| ***   *   * *       *  *               |
| *     *   * *       *  *               |
| *******   * *       *                  |
|   *    **** *       *   *         *****|
|   *    *    *       *   *             *|
|   *    ******       ********          *|
|   *                        *          *|
|   S                 *******************|
+----------------------------------------+

谢谢!

0 个答案:

没有答案