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!
-
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].
-
The if statement is unnecessary, remove it.
-
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.
- 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; }
-
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].
-
The if statement is unnecessary, remove it.
-
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.
- 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
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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