C++中与C# List等效的列表

3

我有一些简单的问题。我正在尝试创建一个在C++中等效于C# List的列表。我的目的是创建一个列表,可以包含我已经创建的一些偏移量。因此,最终我的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#代码中它不是一个常量。


1
有一个叫做 vector 的东西。https://www.geeksforgeeks.org/vector-in-cpp-stl/ - panoskarajohn
请注意:已经有C#的Array.Resize方法了,没有必要再自己编写。 - ckuri
3个回答

1
对于您列出的所有问题,std::vector都是解决方案。尝试使用它或其他标准容器(也许std::list也可以),来解决这个问题。
我给您留下了2个有用的链接,介绍这些数据结构:

vector

list


1

虽然我不太熟悉C#,但看起来List应该是一个std::vector

我知道的是,像int[]这样的东西也是std::vector。通常情况下,如果在C++中不知道大小,就使用std::vector

请注意,std::vector已经有一个resize()函数,因此您无需自己创建。当您需要一个元素时,还可以push_back()它,向量会为您处理。不需要担心大小。


1
你只需要将最后一个元素设置为0x8,然后追加0x4,不需要自己进行太多显式的调整大小,这将由std::vector处理。我会保留CCheck来实现这一点。它的最后一个元素将被更新为0x8,然后我们将追加0x4到它,使其成为您在迭代i时想要的向量:
vector<int> CCheck;
CCheck.push_back(0xAC);
CCheck.push_back(0x10);
CCheck.push_back(0x8 );
// Notice i am keeping only 3 members in CCCheck.
vector< vector< int > >  COffset;

for (int i = 4; i < NumPointer ; i++)
    {
        //Set last element as 0x08.
        CCheck[ i - 2] = 0x8; // Why i - 2 ; You want to set second last element to 0x8 which can be computed via "i - 2" or  "CCheck.size() - 1".
        //Now Insert last 0x4 and put it into COffset.
        CCheck.push_back(0x4);
        COffset.push_back(CCheck);
    }

此外,如果您的编译器支持C++11,您可以这样做 - 而不是逐个推送元素:
  vector<int> CCheck = {0xAC, 0x10, 0x8}; // Again, only three elements.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接