swap an array

hello! im new on c++, i want to know the code of swapping an array,(the values of index) example 12345 >> 54321
sorry for my english.
thanks.
You mean you want to put the elements in the reverse order. You can use std::reverse in <algorithm> for that.
std::reverse(std::begin(array), std::end(array));
or use for loop if do not want to include <algorith>

1
2
3
4
5
6
for(int i=0;i<size/2;i++)
{
    int temp=array[i];
    array[i]=array[size-i-1];
    array[size-i-1]=temp;
}


or

1
2
3
4
5
6
for(int i=0;i<size/2;i++)
{
    array[i]=array[i]+array[size-i-1];
    array[size-i-1]=array[i]-array[size-i-1];
    array[i]=array[i]-array[size-i-1];
}
Topic archived. No new replies allowed.