gpt4 book ai didi

c++ - 我怎样才能实现删除方法?

转载 作者:行者123 更新时间:2023-11-30 03:07:16 24 4
gpt4 key购买 nike

void reserve( int newCapacity ){
Object *oldArray = objects;
int numToCopy = newCapacity < theSize ? newCapacity : theSize;
newCapacity += SPARE_CAPACITY;
objects = new Object[newCapacity];
for( int k=0; k < numToCopy; k++ )
objects[k] = oldArray[k];
theSize = numToCopy;
theCapacity = newCapacity;
delete [ ] oldArray;
}

我如何实现删除方法,可能使用 reserve()?

iterator erase ( iterator position ){
if (position + 1 != end())
copy(position + 1, finish, position);
--finish;
return position;
}

最佳答案

看起来应该这样做:

iterator erase ( iterator position ) {
copy(position+1, end(), position);
resize(size()-1);
return position;
}
iterator erase ( iterator first, iterator last ) {
copy(last, end(), first);
resize(size()-(last-first);
return first;
}

关于c++ - 我怎样才能实现删除方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5930713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com