numbers
= [1, 0, 2, 0, 0, 3, 0, 0, 0, 4, 5],要求:写一代码片段,在不产生新对象列表的前提下,删除列表numbers
中值为 0 的元素
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a[] = { 1, 0, 2, 0, 0, 3, 0, 0, 0, 4, 5 };
int len = sizeof(a) / sizeof(int);
sort(a,a+len);
int s = 0;
int i = 0;
for (; i < len; i++) {
if (a[i] > 0){
break;
}
s++;
}
for (int j = 0; j < len - s; j++,i++) {
a[j] = a[i];
cout << a[j] << ' ';
}
}