c++ - 将 Scala 转换为 C++ - 将列表转换为 vector

标签 c++ scala c++11

我在scala中有以下代码

type Pos = (Int, Int)
def legal_moves(dim: Int, path: Path, x: Pos): List[Pos] = 
{
 List((x._1 + 1, x._2 + 2), 
    (x._1 + 2, x._2 + 1), 
    (x._1 + 2, x._2 - 1), 
    (x._1 + 1, x._2 - 2), 
    (x._1 - 1, x._2 - 2), 
    (x._1 - 2, x._2 - 1), 
    (x._1 - 2, x._2 + 1), 
    (x._1 - 1, x._2 + 2))
}

我想将其转换为 C++,我已尝试执行以下操作:

typedef vector<pair<int,int> > Path;

Path move(const pair<int,int> & passed_Pair)
{

    Path allMoves = 
    (
        ( (get<0>(passed_Pair))+1, (get<1>(passed_Pair))+2),
        ( (get<0>(passed_Pair))+2, (get<1>(passed_Pair))+1),
        ( (get<0>(passed_Pair))+2, (get<1>(passed_Pair))-1),
        ( (get<0>(passed_Pair))+1, (get<1>(passed_Pair))-2),

        ( (get<0>(passed_Pair))-1, (get<1>(passed_Pair))-2),
        ( (get<0>(passed_Pair))-2, (get<1>(passed_Pair))-1),
        ( (get<0>(passed_Pair))-2, (get<1>(passed_Pair))+1),
        ( (get<0>(passed_Pair))-1, (get<1>(passed_Pair))+2)
    );

    return allMoves;
}

我收到此错误消息:

conversion from ‘int’ to non-scalar type ‘Path {aka std::vector >}’ requested

我得到所有红色的 allMoves。

如果我这样做

Path allMoves = { // as above }

我收到此错误消息:

> error: could not convert ‘{(((void)(((int)std::get<0ul, int, int>((* &
> passed_Pair))) + 1)), (((int)std::get<1ul, int, int>((* &
> passed_Pair))) + 2)), (((void)(((int)std::get<0ul, int, int>((* &
> passed_Pair))) + 2)), (((int)std::get<1ul, int, int>((* &
> passed_Pair))) + 1)), (((void)(((int)std::get<0ul, int, int>((* &
> passed_Pair))) + 2)), (((int)std::get<1ul, int, int>((* &
> passed_Pair))) - 1)), (((void)(((int)std::get<0ul, int, int>((* &
> passed_Pair))) + 1)), (((int)std::get<1ul, int, int>((* &
> passed_Pair))) - 2)), (((void)(((int)std::get<0ul, int, int>((* &
> passed_Pair))) - 1)), (((int)std::get<1ul, int, int>((* &
> passed_Pair))) - 2)), (((void)(((int)std::get<0ul, int, int>((* &
> passed_Pair))) - 2)), (((int)std::get<1ul, int, int>((* &
> passed_Pair))) - 1)), (((void)(((int)std::get<0ul, int, int>((* &
> passed_Pair))) - 2)), (((int)std::get<1ul, int, int>((* &
> passed_Pair))) + 1)), (((void)(((int)std::get<0ul, int, int>((* &
> passed_Pair))) - 1)), (((int)std::get<1ul, int, int>((* &
> passed_Pair))) + 2))}’ from ‘<brace-enclosed initializer list>’ to
> ‘Path {aka std::vector<std::pair<int, int> >}’   };

谢谢你的时间

最佳答案

你的 C++ 语法是错误的。您需要学习在堆栈上构造对象的语法以及如何使用 vector 的 initializer_list 构造函数 (http://en.cppreference.com/w/cpp/utility/initializer_list)

#include <vector>
#include <utility>

typedef std::vector<std::pair<int,int> > Path;

Path move(const std::pair<int,int>& passed_Pair)
{
    using std::get;
    Path allMoves
    {
        { get<0>(passed_Pair)+1, get<1>(passed_Pair)+2},
        { get<0>(passed_Pair)+2, get<1>(passed_Pair)+1},
        { get<0>(passed_Pair)+2, get<1>(passed_Pair)-1},
        { get<0>(passed_Pair)+1, get<1>(passed_Pair)-2},

        { get<0>(passed_Pair)-1, get<1>(passed_Pair)-2},
        { get<0>(passed_Pair)-2, get<1>(passed_Pair)-1},
        { get<0>(passed_Pair)-2, get<1>(passed_Pair)+1},
        { get<0>(passed_Pair)-1, get<1>(passed_Pair)+2}
    };

    return allMoves;
}

关于c++ - 将 Scala 转换为 C++ - 将列表转换为 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47652765/

相关文章:

c++ - 在 C/C++ 中将 64 位 bmp 文件转换为数组

c++ - 继承层次结构中的析构函数顺序

Scala 隐式参数被编译器标记为未使用

scala - scala.io.Source 的 readInt

scala - Wartremover 仍会在排除的 Play 路线文件中报告疣

c++ - 转发引用和模板模板

c++ - 对流使用 unique_ptr 有意义吗?

c++ - 在 Ranges-v3 中,如何从一对迭代器创建范围?

c++ - 如何为不允许修改数据的 shared_ptrs 容器编写 getter

c++ - 知道 fscanf(fp, "%s", strr1) 中参数的大小