【问题标题】:C++ List equivalent with C# ListC++ List 等价于 C# List
【发布时间】: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# 代码中看到的常量。

【问题讨论】:

标签: c# c++ list arraylist


【解决方案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.

【讨论】:

    【解决方案2】:

    对于您列出的所有问题,std::vector 是解决方案。试试它或其他标准容器(也许std::list 可以)来解决这个问题。

    我给你留下 2 个有用的链接到这些结构:

    vector

    list

    【讨论】:

      【解决方案3】:

      不熟悉C#,但it seems that List would be an std::vector

      我所知道的是int[] 也是std::vector。通常,只要您不知道 C++ 中的大小,请使用 std::vector

      注意std::vector 已经有一个resize() 函数,所以你不必自己做一个。您也可以在需要时push_back() 一个元素,vector 会为您处理它。无需担心大小。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-30
        • 2011-12-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多