Help me on array handle

 

Hello, I'm trying to push object to array which function have to work with template.

For now, I test with object. But when to push object to first element of array, the old one loss data after move.

Please help me to keep data of object after move. Thanks very much!

My test code is

//+------------------------------------------------------------------+
//|                                                testarraypush.mq4 |
//|                                                         truongxx |
//|                                            https://t.me/truongpm |
//+------------------------------------------------------------------+
#property copyright "truongxx"
#property link      "https://t.me/truongpm"
#property version   "1.00"
#property strict

struct Hand
  {
   double x;
   double y;
   string note;
  };
struct Person
  {
   Hand h1;
   Hand h2;
   string head;
  };  
  
Person persons [];  
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   Person p1;
   p1.h1.x = 1;
   p1.h1.y = 1;
   p1.h2.x = 2;
   p1.h2.y = 2;
   p1.head = "heade of 1";
   ArrayResize(persons, 1);
   persons[0] = p1;
   
   Person p2;
   p2.h1.x = 10;
   p2.h1.y = 10;
   p2.h2.x = 20;
   p2.h2.y = 20;
   p2.head = "heade of 2";
   
   unshift(persons, p2);
   
   for(int i=0;i<ArraySize(persons);i++)
     {
      Print("i:",i,"-head:",persons[i].head, "-h1_x:", persons[i].h1.x);
     }
   
  }
//+------------------------------------------------------------------+
template <typename T> void unshift(T& arr[], T& elm){
   int size = ArraySize(arr);
   ArrayResize(arr,size+1);
   if(size>0)
   {
      for(int i=size-2;i>=0;i--)
      {
         arr[i+1]=arr[i];   
      }
   }   
   arr[0]=elm;
}

The log is on image:

Script test here

Files:
 
Minh Truong Pham:

Hello, I'm trying to push object to array which function have to work with template.

For now, I test with object. But when to push object to first element of array, the old one loss data after move.

Please help me to keep data of object after move. Thanks very much!

My test code is

The log is on image:

Script test here

strange thing is when i change solution to pushlast and remove first elm of array. I work!

Files:
 
  1.    ArrayResize(arr,size+1);
       if(size>0)
       {
          for(int i=size-2;i>=0;i--)
          {
             arr[i+1]=arr[i];

    On the first run size is one, and you have a copy of p1 in arr[0]. Your loop doesn't move any objects (size-2 < 0). So you end up with p2 in arr[0] and an uninitialized Person in arr[1].

  2. The if statement is unnecessary, remove it.

  3. Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.

    The word unshift is meaningless, and what your function does is actually shift and insert elements. Since it creates a stack, I would suggest renaming it to push and the removal function pop.

  4. The code (once corrected) is fine for a few items. For a large number, the following code will be more efficient.
    template <typename T> int push(T& arr[], const T& elm){
       int size = ArraySize(arr);
       bool asSeries = ArrayGetAsSeries(arr); 
       ArraySetAsSeries(arr, !asSeries);
          ArrayResize(arr,++size);
       ArraySetAsSeries(arr, asSeries);
       arr[0]=elm;
       return size;
    }

 
William Roeder #:
  1. On the first run size is one, and you have a copy of p1 in arr[0]. Your loop doesn't move any objects (size-2 < 0). So you end up with p2 in arr[0] and an uninitialized Person in arr[1].

  2. The if statement is unnecessary, remove it.

  3. Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.

    The word unshift is meaningless, and what your function does is actually shift and insert elements. Since it creates a stack, I would suggest renaming it to push and the removal function pop.

  4. The code (once corrected) is fine for a few items. For a large number, the following code will be more efficient.

Thank William Roeder!

I will try as your recommend

Reason: