I have some simple question. I am trying to create a List in C++ that equivalent with C# List. My purpose is to creating a List that can contain some offset that I have create. So at finally my COffset will contain with 200 list of my offset like this
1.{ 0xAC, 0x10, 0x8, 0x4 }
2.{ 0xAC, 0x10, 0x8, 0x8, 0x4 }
3.{ 0xAC, 0x10, 0x8, 0x8, 0x8, 0x4 }
.
.
.
200.{ 0xAC, 0x10, 0x8, ........., 0x8, 0x4}
My C# Code:
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;
}
This works very well in C# with no error. But now I am trying to make it work with C++.First I need to tell that I am just beginning learning C++.
My question is does it have any equivalent code to this C# code?
My C++ progress code:
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#.
}
}
All the comment that I indicate is the part that I don't have any idea how to make it work. For example in CRead I can't assign the size of it because it is not a constant as you can see in C# code.
You only need to set last element as 0x8 and then append 0x4, there's not need to do so much explicit resizing by yourself, that'd be taken care of by - std::vector. I am keeping CCheck for this. It will have it's last element updated to 0x8 and then we'll will append 0x4 to it, making it the vector you'd want at iteration 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);
}
Moreover, if your compiler supports C++11, you can do this - instead of pushing elements one by one:
vector<int> CCheck = {0xAC, 0x10, 0x8}; // Again, only three elements.
For all the problem you listed the std::vector is the solution. Try it or other standard container ( maybe std::list could be fine ) to solve this problem.
I leave you 2 useful link to these construct:
vector
list
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With