Array member remove

 

Hello,

I have an array with struct members. The struct is also including strings.

struct test{string a; int b;};

test arr[5];
//now I filled the complete array with data....
arr[0].a = "active";
arr[0].b = 555;
arr[1].a = ...
//...and so on..

In the end, is it possible now to remove arr[3]?

Thank you

 
Frika:

Hello,

I have an array with struct members. The struct is also including strings.

In the end, is it possible now to remove arr[3]?

Thank you


If you don't care about the order:

Copy the last index to [3]

Resize the array to be 1 less.

int cnt = ArraySize(arr)-1;
int remove = 3;
arr[remove].a = arr[cnt].a;
arr[remove].b = arr[cnt].b;
ArrayResize(arr,cnt);

(uncompiled)

 
Create a copy constructor and just move higher elements down, like any other data type.
   #define INDEX uint
   template<typename Ti, typename To>
   INDEX      copy(const Ti& inp[], INDEX iBeg, INDEX iEnd,
                         To& out[], INDEX oBeg=0){
      while(iBeg != iEnd)  out[oBeg++] = inp[iBeg++];
      return oBeg;
   }
   template<typename T>
   void       remove_at(T&      vector[],   ///<[in,out] The vector.
                        INDEX   index)      /**<[in]Location. */{
      INDEX    iEnd = ArraySize(vector);
      if(index != iEnd) iEnd = copy(vector, index+1, iEnd, vector, index);
   }

struct test{string a; int b;
   void operator=(const test& that){ this.a=that.a; this.b=that.b; }
};

// remove arr[3]
remove_at(arr, 3);
 

Thank you both, very smart and very professionel.

Reason: