#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
const int size = sizeof array / sizeof *array;
void reform(int (&array)[size])
{
int i = 0, j = size - 1;
while(i <= j) {
while(i <= j) {
if(array[i] % 2 == 0)
break;
else
i++;
}
while(i <= j) {
if(array[j] % 2 != 0)
break;
else
j--;
}
//如果不在这里进行i++,j--就会出错
swap(array[i++], array[j--]);
}
}
void main()
{
reform(array);
copy(array, array + size, ostream_iterator<int>(cout, " "));
}
#include <iterator>
#include <algorithm>
using namespace std;
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
const int size = sizeof array / sizeof *array;
void reform(int (&array)[size])
{
int i = 0, j = size - 1;
while(i <= j) {
while(i <= j) {
if(array[i] % 2 == 0)
break;
else
i++;
}
while(i <= j) {
if(array[j] % 2 != 0)
break;
else
j--;
}
//如果不在这里进行i++,j--就会出错
swap(array[i++], array[j--]);
}
}
void main()
{
reform(array);
copy(array, array + size, ostream_iterator<int>(cout, " "));
}