(C++)删除整型数列中相同的数

xiaoxiao2021-02-27  290

#include<iostream> #include<string> using namespace std; class ARR { public: ARR(int x[], int size):m(size) { a=new int[m]; for(int i=0; i<m; i++) a[i]=x[i]; } void delsame()//使用双数组的方式删除重复数字 { int top=0; int *newArr=new int[m];//get一个新数组 for(int i=0; i<m-1; i++) { if(a[i]!=a[i+1]) newArr[top++]=a[i]; } if(newArr[top-1]!=a[m-1]) newArr[top++]=a[m-1]; m=top; a=newArr; delete[] newArr;//释放占用的内存空间 } void show() { for(int i=0; i<m; i++) cout<<a[i]<<" "; cout<<endl; } private: int m; int *a; }; int main() { int n,*s; while(cin>>n) { s=new int[n]; for(int i=0; i<n; i++) cin>>s[i]; ARR test(s,n); test.delsame(); test.show(); delete[] s; } return 0; }
转载请注明原文地址: https://www.6miu.com/read-8671.html

最新回复(0)