数组名作为实参传给指针形参,用push_back()将数组复制到vector和list出现异常
数组名作为实参传给指针形参,用push_back()将数组复制到vector和list出现错误
int main( ){
int a[ ]={1,2,3,4};
vector<int> ivec,list<int> ilist;
erase_number(a,ivec,ilist);
return 0;
}
void erase_number(int *ap,vector<int> &ivec,list<int> &ilist){
ivec.clear();
ilist.clear();
while(ap){
ivec.push_back(*ap);
ilist.push_back(*ap);
++ap;
}
}
运行出现错误,求大神解答!!!
------解决方案--------------------
把数组元素数目 n 也传入到函数中,循环用 n 控制。
------解决方案--------------------
while(ap), 你的ap一直在执行++操作, 等它溢出归0后,你的vector和list早爆了。
使用std::array<>替代 int* 传递数组最好。
否则把数据的长度传入erase_number函数,或者通过其它方式来想识别数组的长度或结尾(比如像C字符串一样以0结尾)。
------解决方案--------------------
++ap,超过数组范围之后,取值是随机的,凭什么说一定等于零??
------解决方案--------------------
++ap 和 p->next 可是不一样的啊。。。 p->next的值提前设置为0就是了, 但 ++ap 。。。加法啊, 假如 ap = 10000, 你琢磨琢磨要++多久才能加到0啊?
int main( ){
int a[ ]={1,2,3,4};
vector<int> ivec,list<int> ilist;
erase_number(a,ivec,ilist);
return 0;
}
void erase_number(int *ap,vector<int> &ivec,list<int> &ilist){
ivec.clear();
ilist.clear();
while(ap){
ivec.push_back(*ap);
ilist.push_back(*ap);
++ap;
}
}
运行出现错误,求大神解答!!!
------解决方案--------------------
把数组元素数目 n 也传入到函数中,循环用 n 控制。
------解决方案--------------------
while(ap), 你的ap一直在执行++操作, 等它溢出归0后,你的vector和list早爆了。
使用std::array<>替代 int* 传递数组最好。
否则把数据的长度传入erase_number函数,或者通过其它方式来想识别数组的长度或结尾(比如像C字符串一样以0结尾)。
------解决方案--------------------
++ap,超过数组范围之后,取值是随机的,凭什么说一定等于零??
------解决方案--------------------
++ap 和 p->next 可是不一样的啊。。。 p->next的值提前设置为0就是了, 但 ++ap 。。。加法啊, 假如 ap = 10000, 你琢磨琢磨要++多久才能加到0啊?