Copy array reverse

To add comments, please log in or register
Amirhossein Karimzadeh
629
Amirhossein Karimzadeh  

How copy array to another reversed.

For example:

Double aa[]={1,20,3,25,9} >> copy to array bb {9,25,3,20,1}

Anthony Garot
1822
Anthony Garot  
Amirhossein Karimzadeh:

How copy array to another reversed.

For example:

Double aa[]={1,20,3,25,9} >> copy to array bb {9,25,3,20,1}

    double aa[] = {1,20,3,25,9};
    double bb[];

    int size = ArraySize(aa);
    ArrayResize(bb, size);

    for ( int i=0; i<size; i++ )
    {
        bb[size-i-1] = aa[i];
    }
whroeder1
17941
whroeder1  
If you don't need the original, reverse in place, otherwise reverse_copy.
template<typename T>
void       swap(T& a, T& b){             T tmp=a; a=b; b=tmp;        }
// reverse and reverse_copy, and their auxiliary functions
template<typename T>
void       reverse(T& arr[], INDEX iBeg, INDEX iEnd){
   while(iBeg < iEnd)   swap(arr[iBeg++], arr[--iEnd] );
}
template<typename Ti, typename To>
INDEX      reverse_copy(const Ti& inp[], INDEX iBeg, INDEX iEnd,
                                     To& out[], INDEX oBeg){
   while(iBeg != iEnd)  out[oBeg++] = inp[--iEnd];
   return oBeg;
}
To add comments, please log in or register