如何将c++中的make_pair转换为c#
How to translate make_pair in c++ to c#?
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
系统。元组是类型,但像变量一样使用错误
您正在声明使用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;
}
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 将std::pair转换为具有任意数量元素的std::tuple
- C++ 是否可以定义从pair<int,int>到pair<float,float>的隐式转换?
- 为什么允许从 pair<int64_t,int64_t> 隐式转换为 pair<int,int>?
- 将指向成员函数的指针绑定到std::pair并强制转换为void*
- 是否可以强制转换成对的Key, Value>到pair
- std::pair中的Const转换
- 映射和节点,错误智能感知:不存在从"const std::pair<const int, double>"到"Node"的合适用户定义转换
- 正在强制转换std::pair
const&T1 const, T2>const&安全的