Reversing an array data structure with the reverse STL library Algorithm

A substantially linear array is a data structure, in fact, is a block of memory, we can assign a new array of through c ++

int* a= new int[5]; 

Then fill in each element of the array

a[0]=1;

a[1]=2;

a[2]=6;

a[3]=4;

a[4]=5;

The continuous array stood 12,645

Inverted write the following function, the first switching array and the i n-1-i element

void reverseArray(int* ar, int n)

{

     int m=n/2;

     for (int i=0;i<m;i++)

     {

          int temp=ar[i];

          ar[i]=ar[n-1-i];

          ar[n-1-i]=temp;

      }

}

If you are using stl Standard Template Library provides a reverse function in the algorithm in reverse,

Generics, int array may be reversed, char type, string type and the like, e.g.

#include<algorithm>

#include<vector>

int main ()

{

    std::vector<int> a;

    a.push_back(1);

    a.push_back(2);

    a.push_back(6);

    std :: reverse (a.begin (), a.end ()); // the 1 2 6 int sequence is inverted in a 621

    return 0;

}

 

Guess you like

Origin www.cnblogs.com/abcstar/p/11651243.html