C ++向量载体 [英] C++ vector of vectors

查看:141
本文介绍了C ++向量载体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个程序,使用一个基类来定义一个算法来解决一个简单的问题。我使用一个ints的向量作为游戏板。我的问题是如何创建一个函数get_moves,返回一个游戏板的向量?

I am trying to write a program that uses a base class to define an algorithm for solving a simple problem. I use a vector of ints as a 'game board'. My question is how can I create a function get_moves that returns a vector of game boards?

这是我对函数的代码:

std::vector< <std::vector<int> > takeaway::generateMoves( std::vector<int> currBoard ) {


    if( currBoard[0] == 1 || currBoard[0] == 2 ) {
       moves.push_back( 1 );
    }
    else if( currBoard[0] == 3 ) {
       moves.push_back( 2 );
    }
    else if( currBoard[0] == 4 ) {
       moves.push_back( 3 );
    }
    else {
      moves.push_back( 1 );
      moves.push_back( 2 );
      moves.push_back( 3 );
    }

    std::vector< <std::vector <int > > toReturn( moves );

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

      std::cout << "MOVES: " << moves[i] << std::endl;
    }

  return toReturn;

我得到的错误是:

takeaway.cpp:55:error:模板参数1无效
takeaway.cpp:55:错误:模板参数2无效

takeaway.cpp:55: error: template argument 1 is invalid takeaway.cpp:55: error: template argument 2 is invalid

如何正确创建和返回向量的向量?

So my question is how can I properly create and return a vector of vectors?

推荐答案

您的模板声明中有太多的<

You have too many <'s on your template declarations.

std::vector< std::vector<int> > takeaway::generateMoves( std::vector<int> currBoard ) 
{

    if( currBoard[0] == 1 || currBoard[0] == 2 ) {
       moves.push_back( 1 );
    }
    else if( currBoard[0] == 3 ) {
       moves.push_back( 2 );
    }
    else if( currBoard[0] == 4 ) {
       moves.push_back( 3 );
    }
    else {
      moves.push_back( 1 );
      moves.push_back( 2 );
      moves.push_back( 3 );
    }

    std::vector< std::vector<int> > toReturn;
    toReturn.push_back( moves );

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

      std::cout << "MOVES: " << moves[i] << std::endl;
    }

    return toReturn;
}

可能有更多,但这是其中之一。

There might be more, but that is one of them.

这篇关于C ++向量载体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆