如何将c++中的make_pair转换为c#

本文关键字:pair 转换 make c++ 中的 | 更新日期: 2023-09-27 18:11:23

map <pair<unsigned int, unsigned int>, unsigned int> kmapValues;

private Dictionary<KeyValuePair<uint, uint>, uint> kmapValues;
代替

kmapValues[make_pair(j, i)] = 1 

kmapValues[Tuple(j, i)] = 1 // got error

系统。元组是类型,但像变量一样使用错误

如何将c++中的make_pair转换为c#

您正在声明使用KeyValuePair<K,V>键的字典,但您试图使用Tuple<T1,T2>来访问它。你需要决定一种类型,并坚持下去。

由于KeyValuePair<K,V>是不对称的,我将使用Tuple<T1,T2>:

private Dictionary<Tuple<uint, uint>, uint> kmapValues;

那么你的分配就可以正常工作了:

kmapValues[Tuple.Create(j, i)] = 1;

//作为c#的扩展方法,在一些静态类上:

public static KeyValuePair<K, V> MakePair<K, V>(this K k, V v) { return new KeyValuePair<K, V>(k, v); }

我的解决方案是使用一个向量来重构,然后在c#接口附近映射,然后包装器函数通过引用返回子元素,然后可以根据需要重新组合;这有点麻烦,但我认为值得。

最近一个项目的例子:如您所见,这里有很多函数调用,但仍然相当简单。

    public struct InputLinkOutputProbeGeneratedInfoSimple
    {
        public bool HasValidVisualRGBSettings;
        public int VisualRGB_resX0;
        public int VisualRGB_resY0;
        public IlVisualGenPlane VisualRGB_iteratorPlaneMode;
        public ulong VisualRGB_startX;
        public ulong VisualRGB_startY;
        public Dictionary<Tuple<int, int>, int> TextureXYToProbeIndex_Red;
        public Dictionary<Tuple<int, int>, int> TextureXYToProbeIndex_Green;
        public Dictionary<Tuple<int, int>, int> TextureXYToProbeIndex_Blue;
    }
     [DllImport("EDeNServerInterfaceDLL.dll", EntryPoint = "GetCurrentTextureXYToProbeIndex_Count", CallingConvention = CallingConvention.Cdecl)]
    private extern static int GetCurrentTextureXYToProbeIndex_Count();
    //-----
    [DllImport("EDeNServerInterfaceDLL.dll", EntryPoint = "GetCurrentTextureXYToProbeIndex_Red", CallingConvention = CallingConvention.Cdecl)]
    private extern static void GetCurrentTextureXYToProbeIndex_Red(int idx, out int x, out int y, out int inLnkIdx);
    //-----
    [DllImport("EDeNServerInterfaceDLL.dll", EntryPoint = "GetCurrentTextureXYToProbeIndex_Green", CallingConvention = CallingConvention.Cdecl)]
    private extern static void GetCurrentTextureXYToProbeIndex_Green(int idx, out int x, out int y, out int inLnkIdx);
    //-----
    [DllImport("EDeNServerInterfaceDLL.dll", EntryPoint = "GetCurrentTextureXYToProbeIndex_Blue", CallingConvention = CallingConvention.Cdecl)]
    private extern static void GetCurrentTextureXYToProbeIndex_Blue(int idx, out int x, out int y, out int inLnkIdx);
    public static InputLinkOutputProbeGeneratedInfoSimple GetCurrentInputLinkOutputProbeGeneratedInfoSimple()
    {
        InputLinkOutputProbeGeneratedInfoSimple ilopgis = new InputLinkOutputProbeGeneratedInfoSimple();
        //
        ilopgis.HasValidVisualRGBSettings = GetCurrentInputLinkOutputProbeGeneratedInfo_HasValidVisualRGBSettings();
        ilopgis.VisualRGB_resX0 = GetCurrentInputLinkOutputProbeGeneratedInfo_VisualRGB_resX();
        ilopgis.VisualRGB_resY0 = GetCurrentInputLinkOutputProbeGeneratedInfo_VisualRGB_resY();
        ilopgis.VisualRGB_iteratorPlaneMode = GetCurrentInputLinkOutputProbeGeneratedInfo_VisualRGB_iteratorPlaneMode();
        ilopgis.VisualRGB_startX = GetCurrentInputLinkOutputProbeGeneratedInfo_VisualRGB_startX();
        ilopgis.VisualRGB_startY = GetCurrentInputLinkOutputProbeGeneratedInfo_VisualRGB_startY();
        ilopgis.TextureXYToProbeIndex_Red = new Dictionary<Tuple<int, int>, int>();
        ilopgis.TextureXYToProbeIndex_Green = new Dictionary<Tuple<int, int>, int>();
        ilopgis.TextureXYToProbeIndex_Blue = new Dictionary<Tuple<int, int>, int>();
        int x = 0, y = 0, inLnkIdx = 0;
        for (int i = 0; i < GetCurrentTextureXYToProbeIndex_Count(); i++)
        {
            //
            GetCurrentTextureXYToProbeIndex_Red(i, out x, out y, out inLnkIdx);
            ilopgis.TextureXYToProbeIndex_Red[new Tuple<int, int>(x, y)] = inLnkIdx;
            //
            GetCurrentTextureXYToProbeIndex_Green(i, out x, out y, out inLnkIdx);
            ilopgis.TextureXYToProbeIndex_Green[new Tuple<int, int>(x, y)] = inLnkIdx;
            //
            GetCurrentTextureXYToProbeIndex_Blue(i, out x, out y, out inLnkIdx);
            ilopgis.TextureXYToProbeIndex_Blue[new Tuple<int, int>(x, y)] = inLnkIdx;
        }
        //
        return ilopgis;
    }