我正在处理这个个人项目,我对remove()函数的工作方式感到有些困惑。
头:
class IntSet {
public:
IntSet(); //Constructor
~IntSet(); //Destructor
int size() ; //
bool isEmpty();
bool contains(int number1);
void add(int number2);
void remove(int number2);
private:
int* ptr; //pointer to the array
int sizeOfArray; //current size of the array
int currentValue; //number of values currently in IntSet
};
main(仅包括add()部分)
#include "IntSet.hpp"
#include <iostream>
IntSet::IntSet(){
sizeOfArray = 10;
currentValue = 0;
ptr = new int[10];
}
IntSet::~IntSet(){
delete[] ptr;
}
//returning the number of values in the IntSet
int IntSet::size()
{
return currentValue;
}
//Determining whether the stack is empty
bool IntSet::isEmpty()
{
if (currentValue == 0)
return true;
else
return false;
}
//defining contains() function
bool IntSet::contains(int number1)
{
for (int i = 0; i < currentValue; i++)
{
if (ptr[i] == number1)
return true;
}
return false;
}
//defining add() function
void IntSet::add(int number2)
{
if (currentValue == sizeOfArray)
{
sizeOfArray = sizeOfArray * 2; //doubling size of arrayCapacity
int* temp = new int[sizeOfArray]; //allocating new one that's twice as large
for (int i = 0; i < currentValue; i++)
{
temp[i] = ptr[i]; //copying old stuff into new one
}
delete[] ptr; //deallocate old array
ptr = temp; //set ptr to new array
}
}
//defining remove() function goes here
因此对于add()函数,我必须将一个int参数添加到数组中。当它变满时我必须将数组的大小加倍,将旧数组的内容复制到新数组中,将数据成员指针重定向到新数组,然后释放数组。
对于remove()函数,我必须通过移动数组的所有后续元素来获取一个int参数并将其从IntSet中删除。我应该只使用我的add函数的部分并且几乎告诉它为我的remove()函数做相反的事情吗?如果没有,我怎么开始编写remove()函数?如果需要,我会显示其余的代码。非常感谢你们!
答案 0 :(得分:0)
尝试删除:
void IntSet::remove(int number2)
{
bool bIntRemoved = false;
for(int i=0; i < currentValue; i++){
// check if we are currently searching or shifting
if(!bIntRemoved){
// still searching
// check if we should remove int at current index
if(ptr[i] == number2){
// found the int to remove
// We'll decrement i and set bIntRemoved = to true
// So the else-if code handles shifting over the array
i--;
bIntRemoved = true;
}
}else if(i < currentValue-1){
// We have spots to shift
// Check if this is the last index
ptr[i] = ptr[i+1];
} // else, we are at the last index and we have nothing to shift
}
if(bIntRemoved){
// The int was successfully located and any necessary shifting has been
// executed. Just decrement currentValue so the current last index will be
// disregarded.
currentValue--;
} // else, the int to remove could not be located
}
我还没有测试过,但从理论上讲,这应该找到你需要删除的int的第一个实例,将所有值保留一个点(除非要删除的int在数组的最后一个索引中) ),然后递减currentValue
变量,以便忽略数组的上一个最后一个索引,并且可以覆盖该变量。无论如何,对不起,如果这是一个糟糕的解释,但它不是最容易解释的概念。我试图很好地记录代码,所以希望这是有道理的:P如果您有任何问题请告诉我,如果这有效或者不适合您,请告诉我(我发现反馈是非常重要。)
编辑:另外,我打算提一下,但我忘了,谢谢你,@ Viet,在你的回答中提到这一点,你的add()
功能似乎没有处理currentValue
小于数组大小的情况。我假设你已经处理过了,你只是省略了else
语句来处理它?</ p>
编辑#2: 以下是正确处理向数组添加新元素的代码:
void IntSet::add(int number2){
if (currentValue == sizeOfArray)
{
sizeOfArray = sizeOfArray * 2; //doubling size of arrayCapacity
// nothrow is used below to allow for graceful error handling if there is not enough
// ram to create the new array
int* temp = new (nothrow) int[sizeOfArray]; //allocating new one that's twice as large
// check if new int array could be create
if(temp == nullptr){
// new int array could not be created
/** Possibly set an error flag here or in some way warn the calling function that
the function failed to allocate the necessary memory space.
I'll leave that up to you, OP. **/
// Right now we'll just return without modifying the existing array at all
return;
}
for (int i = 0; i < currentValue; i++)
{
temp[i] = ptr[i]; //copying old stuff into new one
}
delete[] ptr; //deallocate old array
ptr = temp; //set ptr to new array
// Now we'll just let the code below add the number to the array
} // else we have enough space to add the number to the array
ptr[currentValue] = number2;
currentValue++;
}
同样,我还没有测试过这段代码,但请告诉我它是否有效或无效。此外,我修改了生成新数组(int *temp = new int[sizeOfArray];
)的行,以便在无法成功分配内存时处理错误。为此,我使用(nothrow)
对象(this CPlusPlus page上的更多内容)。如果分配失败,则将temp
设置为空指针。如果没有这个,该方法将抛出bad_alloc
异常,否则程序将终止。这不是很优雅,所以我更喜欢正确处理错误(并以一种在调用函数上不那么费力的方式处理它)。要使用此功能,您需要包含<new>
标头(定义{{1}}的位置)。
答案 1 :(得分:0)
您的班级是集合还是列表?如果你的班级是一套,那就意味着班上没有相同的数字 示例:集合{1,2,3},列表:{1,2,3,1,3,2}
关于你的添加功能,我有一些意见:
关于删除功能,我有一些想法:
示例:您有一组{1,2,3,4},当前大小为4.并且您想删除一个数字&#34; 2&#34;