1

我正在做一个 C# 项目,我想next_permutation从 C++ 中的算法库中调用。我找到了在 c# 中调用 c++ 函数的方法,但我不知道如何从 c++ 获取向量并在 c# 中使用它(因为 next_permutation 需要一个 int 向量......)

这就是我目前正在尝试的:

extern void NextPermutation(vector<int>& permutation) 
{
    next_permutation (permutation.begin(),permutation.end()); 
}

[DllImport("PEDLL.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern void NextPermutation(IntPtr test);
4

2 回答 2

1

这样做的唯一方法是通过 C++/CLI 包装类。但是,您必须将 int[] 或 List< int > 转换为 std::vector 作为单独的传递。如果您传入的向量中有很多数据......这将导致显着减速。

于 2012-06-20T21:11:45.770 回答
1

P/Invoke 对于 C++ 类型非常不利。您应该尝试将您的问题简化为 C 接口。在你的情况下,这很容易!

extern void NextPermutation(int *permutation, int count) 
{
    next_permutation (permutation, permutation + count); 
}
于 2012-06-20T21:12:05.323 回答