C ++ Program Design Practice Guide 1.1 Delete the same number of rewriting requirements in the sequence, Program Design Practice 1.1
Rewrite requirement 1: rewrite to a pointer-based Data Structure
#include <iostream> #include <cstdlib>using namespace std; class ARP { int m; int* p; int count[10]; public: ARP(int x[],int size) { m = size; p = new int [m]; for (int i =0;i<m;i++) { p[i]=x[i]; } for (int i =0;i<m;i++) { count[i]=1; } } void delsame(); void show1(); void show2(); ~ARP() { delete [] p; } }; void ARP::show1(){ for(int i=0;i<m;i++) { cout<<p[i]<<"\t"; } cout<<endl; }void ARP::show2() { for(int i=0;i<m;i++) { cout<<p[i]<<"\t"; } for(int i=0;i<m;i++) { cout<<count[i]<<"\t"; } cout<<endl; } void ARP::delsame() { int i,j; for(i=0;i<m-1;i++) { if(p[i]==p[i+1]) { count[i]=count[i]+1; for(j=i+1;j<m-1;j++) { p[j]=p[j+1]; } m --; i --; } } } int main() { int b[16] = {1,2,2,3,4,4,5,6,6,7,8,8,8,9,10,10}; ARP temp(b,sizeof(b)/sizeof(b[0])); temp.show1(); temp.delsame(); temp.show2(); system("pause") ; return 0; }