2012-06-20 90 views
1

我正在做一個C#項目,我想從C++的算法庫中調用next_permutation。我找到了在c#中調用C++函數的方式,但我不知道如何從C++獲取向量並在c#中使用它(因爲next_permutation需要int向量...)C++ int vector to c#

這就是我現在正在嘗試的:

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); 
+1

你需要一些膠水來轉換它。爲什麼不用C#來做呢?谷歌給我這個:http://code.google.com/p/monoalgorithm/source/browse/trunk/Algorithm.cs?spec=svn2&r=2#163 –

回答

1

這樣做的唯一方法是通過C++/CLI包裝類。但是,您必須將int []或List < int>轉換爲std :: vector作爲單獨傳遞。如果傳入的矢量中有大量數據,則會導致顯着速度減慢。

1

P/Invoke對C++類型非常不利。您應該嘗試將您的問題簡化爲C界面。在你的情況下,這很容易!

extern void NextPermutation(int *permutation, int count) 
{ 
    next_permutation (permutation, permutation + count); 
}