访问存储在Llist

时间:2016-09-15 23:16:48

标签: c++ list vector

我是C ++的新手,我正在寻找一些有关如何在列表中访问数据存储的帮助。我目前正在存储玩家制作的移动,这些移动由列表中的向量矢量表示。

std::vector<std::vector<int> > move1;

std::vector<std::vector<int> > move2;

    std::vector<std::vector<int> > move3;

    std::vector<std::vector<int> > move4;

    std::list<vector<vector<int>>> moveList;

void PuzzleBoard::NextPossibleMove()
{
    //assigns the current board state to a temp hold var
    nextBoard = board;

    //A switch statement to attempt to move in each direction once
    for (int i = 1; i < 5; i++) {
        switch (i) {
        case 1: {
            //attempts to move zero up
            MoveZero(1, userPS);

            //stores the board after the move
            move1 = board;


            //Add the vector of vectors move1 to the list variable moveList
            moveList.push_back(move1);


            //Resets the board to its original state
            board = nextBoard;
            break;
        }
        case 2: {
            //attempts to move zero down
            MoveZero(2, userPS);

            //stores the board after the move
            move2 = board;


            //Add the vector of vectors move2 to the list variable moveList
            moveList.push_back(move2);


            //Resets the board to its original state
            board = nextBoard;
            break;
        }
        case 3: {
            //attempts to move zero left
            MoveZero(3, userPS);

            //stores the board after the move
            move3 = board;


            //Add the vector of vectors move3 to the list variable moveList
            moveList.push_back(move3);


            //Resets the board to its original state
            board = nextBoard;
            break;
        }
        case 4: {
            //attempts to move zero right
            MoveZero(4, userPS);

            //stores the board after the move
            move4 = board;


            //Add the vector of vectors move4 to the list variable moveList
            moveList.push_back(move4);



            //Resets the board to its original state
            board = nextBoard;
            break;
        }
        {
        default:
            break;
        }
        }
    }
}

一旦我在列表中移动了矢量矢量,我就不知道如何访问它们了。 我试过这个:

void PuzzleBoard::PrintMovesFromList()
{
    for (std::list<int>::iterator iter = moveList.begin(); iter < moveList.end(); iter++) {

        cout << *iter << endl;
}

但是,它表示moveList没有转换类型来这样做。 我也试过这个:

void PuzzleBoard::PrintMovesFromList()
{
    for (std::list<vector<vector<int>>>::iterator iter = moveList.begin(); iter < moveList.end(); iter++) {

        cout << *iter << endl;
}

但是,它说&lt;。

没有运营商匹配

基本上,我想在一个列表中存储玩家所做的移动(它总是被表示为向量的向量),并且在游戏结束后我想要访问存储在列表中的向量向量来打印它们到屏幕。我已经搜索了几个关于使用列表的网站,但是我没有来过任何网站,举例说明我想要做什么。任何人都可以指出我正确的方向或解释我如何以我想要的方式访问存储的数据? 任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:0)

std::list迭代器不是随机访问迭代器。只能按照这种方式订购随机访问运营商。

std::list迭代器只能进行相等性比较:

for (std::list<int>::iterator iter = moveList.begin();
         iter != moveList.end(); iter++) {

然而,既然你是C ++的新手,那就是&#34;您应该学习如何使用当前C ++标准中的新语言功能迭代容器:

for (const auto &value:moveList)
      cout << value << endl;