c++数组删除指定元素

如题所述

第1个回答  2022-12-11

//浮点数型数组(以double型数组为例)

intremoveGivenValue(double*pArray,constintnLen,constdoublelfGivenValue)

{

if(pArray==NULL||nLen<1)

return0;

intnValidLen=0;

for(inti=0;i<nLen;++i)

{

if(fabs(pArray[i]-lfGivenValue)<0.000001)

continue;

pArray[nValidLen++]=pArray[i];

}

returnnValidLen;

}

//整型数组

intremoveGivenValue(int*pArray,constintnLen,constintnGivenValue)

{

if(pArray==NULL||nLen<1)

return0;

intnValidLen=0;

for(inti=0;i<nLen;++i)

{

if(pArray[i]==nGivenValue)

continue;

pArray[nValidLen++]=pArray[i];

}

returnnValidLen;

}

扩展资料

C++数组指针用法

将a理解为指向数组头的一个指针,这样就好理解了。理解了之后确实好像豁然开朗的样子。这样a[5]就等于*(a+5),也就相当于将数组头指针向后推5个位置,然后取到该位置的数据了。

#include<iostream>

#include<typeinfo>

usingnamespacestd;

#definetype(a)typeid(a).name()

intmain(){

inta[10]={1,2,3,4,5,6,7,8,9,10};

cout<<"a="<<a<<"&a="<<&a<<endl;

int*p1=(int*)(&a+1);

int*p2=(int*)(a+1);

cout<<"*(p1-1)="<<*(p1-1)<<"*(p2-1)="<<*(p2-1)<<endl;

cout<<"sizeof(a)="<<sizeof(a)<<"sizeof(&a)="<<sizeof(&a)<<endl;

cout<<"TypeOf(a)="<<type(a)<<"TypeOf(&a)="<<type(&a)<<endl;

}

相似回答