我有一道C++题要问,特急。希望高手帮忙。

来源:百度知道 编辑:UC知道 时间:2024/06/22 13:31:14
问题是:假定数组a[n]中有若干个值为0的元素,用一个循环语句,把a中非0元素放到数组的前半部,要求非0元素的相对次序保持不变。输出非0元个数和重排后的数组元素(不输出0元素)。
要求用指向一维数组的指针做出。
最好把详细的程序写出来。

#include<iostream>
#include<algorithm>
#include<iterator>
using namespace std;
bool f(int a, int b){return b == 0;}
int main()
{
int a[] = {9, 1, 0, 2, 5, 0 ,3, 0};
const size_t size = sizeof(a)/sizeof(a[0]);
copy(a, a + size, ostream_iterator<int>(cout, " "));cout << endl;
sort(a, a + size, f); // 排序!!!
copy(a, a + size, ostream_iterator<int>(cout, " "));cout << endl;
}