【发布时间】:2020-04-23 06:33:02
【问题描述】:
我有一个简单的问题。我正在尝试在 C++ 中创建一个与 C# 列表等效的列表。我的目的是创建一个可以包含我创建的一些偏移量的列表。所以最后我的COffset 将包含 200 个我的偏移量列表,就像这样
1.{ 0xAC, 0x10, 0x8, 0x4 }
2.{ 0xAC, 0x10, 0x8, 0x8, 0x4 }
3.{ 0xAC, 0x10, 0x8, 0x8, 0x8, 0x4 }
.
.
.
200.{ 0xAC, 0x10, 0x8, ........., 0x8, 0x4}
我的 C# 代码:
private int[] CRead;
private int[] CCheck = { 0xAC, 0x10, 0x8, 0x4 };
List<int[]> COffset = new List<int[]>();
private int NumPointer = 204;
private void CalculateOffset()
{
for (int i = 4; i < NumPointer ; i++)
{
CRead = (int[])ResizeArray(CCheck, i);
CRead[i - 1] = 0x4;
CRead[i - 2] = 0x8;
CCheck = CRead;
COffset.Add(CRead);
}
}
private static System.Array ResizeArray(System.Array oldArray, int newSize)
{
int oldSize = oldArray.Length;
System.Type elementType = oldArray.GetType().GetElementType();
System.Array newArray = System.Array.CreateInstance(elementType, newSize);
int preserveLength = System.Math.Min(oldSize, newSize);
if (preserveLength > 0)
System.Array.Copy(oldArray, newArray, preserveLength);
return newArray;
}
这在 C# 中运行良好,没有错误。但现在我正在尝试使它与 C++ 一起使用。首先我需要告诉你我刚刚开始学习 C++。
我的问题是它是否有与此 C# 代码等效的代码?
我的 C++ 进度代码:
static int CRead[]; //*Can't create this because it have to tell the size.
static int CCheck[] = { 0xAC, 0x10, 0x8, 0x4 };
list<int[]> COffset; //*don't know that it is correct or not. I don't know how to test it without fully done.
//#include <list> for using list
static int NumPointer = 204;
static void CalculateOffset(){
for(int i =4 ; i<NumPointer ; i++){
//This steps I have no idea at all because its need ResizeArray in C#.
}
}
我指出的所有评论都是我不知道如何使它工作的部分。例如在CRead 中,我无法指定它的大小,因为它不是您在 C# 代码中看到的常量。
【问题讨论】:
-
有
vector。 geeksforgeeks.org/vector-in-cpp-stl -
请注意:已经有一个 C# Array.Resize 方法。无需自己编写。