【发布时间】:2023-04-02 23:46:01
【问题描述】:
有点不知道接下来该做什么。我必须创建一个名为 IntSet 的类,它使用以下数据成员表示整数的数学集合:
一个指向 int 的指针,它将指向一个动态分配的数组,该数组包含当前在 IntSet 中的值
保存数组当前大小的 int(每当 add() 方法创建更大的数组时都需要更新它 **
保存 IntSet 中当前值数量的 int(需要在 add() 和 remove() 方法中更新。
大部分程序都是完整的,除了最后一部分,它需要我组合两组整数并让它们相互交互。我必须使用以下内容:
removeDifferent():setA.removeDifferent(setB) 应该从 setA 中删除任何不在 setB 中的值。
removeSame():setA.removeSame(setB) 应该从 setA 中删除任何也在 setB 中的值。
标题:
#include <iostream>
class IntSet{
public:
void print(); //prints IntSet
IntSet(); //Constructor
~IntSet(); //Destructor
int size(); //returns number of values currently in IntSet
bool isEmpty(); //returns true if IntSet contains no integers. False otherwise
bool contains(int i) const; //returns true if a value is in IntSet. False otherwise.
void add(int i); //adds a value to IntSet. If current array is full, allocate a new array that is twice as big.
void remove(int i); //removes value from IntSet by shifting over all of the subsequent values in the array.
void addAll()
void removeDifferent()
void removeSame()
//----helper methods----
int getIndex(int integer); //returns the integer on an index
private:
int* ptr; //points to a dynamically allocated array that holds the values currently in IntSet
int sizeOfArray; //holds current size of Array
int numberOfValues; //holds number of values currently in IntSet
};
主要:
#include "IntSet.hpp"
#include <iostream>
//prints IntSet
void IntSet::print(){
for (int i = 0; i < numberOfValues; i++){
std::cout << ptr[i] << " ";
}
std::cout << std::endl;
}
//delcaring variables
IntSet::IntSet(){
numberOfValues = 0;
sizeOfArray = 10;
ptr = new int[10];
}
IntSet::~IntSet(){
delete[] ptr;
}
//Returning the number of values in the IntSet
int IntSet::size(){
return numberOfValues;
}
//Determining whether the stack is empty
bool IntSet::isEmpty(){
if(numberOfValues == 0){
return true;
}
return false;
}
//defining contains() function
bool IntSet::contains(int i) const
{
for (int k = 0; k < numberOfValues; k++){
if (ptr[k] == i){
return true;
}
}
return false;
}
//defining add() function
void IntSet::add(int i){
if (contains(i)){
return;
}
if (numberOfValues == 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 < numberOfValues; i++)
{
temp[i] = ptr[i]; //copying old stuff into new one
}
delete[] ptr; //deallocate old array
ptr = temp; //set ptr to new array
}
ptr[numberOfValues] = i;
numberOfValues++;
}
void IntSet::remove(int i){
if(!contains(i)){
return;
}
bool bIntRemoved = false;
for(int k=0; k < numberOfValues; k++){
// check if we are currently searching or shifting
if(!bIntRemoved){
if(ptr[k] == i){
// found the int to remove
bIntRemoved = true;
}
}else if(k < numberOfValues-1){
ptr[k] = ptr[k+1];
} // else, we are at the last index and we have nothing to shift
}
numberOfValues--;
}
int IntSet::getIndex(int index){
return ptr[index];
}
void IntSet::addAll(IntSet b){
for(int i = 0; i < b.size(); i++){
add(b.getIndex(i));
}
}
如何合并我的 add() 和 remove() 函数来组合两组整数并让它们遵循这些规则?我什至不知道如何开始写它们:(
谢谢大家!
编辑:addAll() 已完成,但其他两个仍然没有点击
【问题讨论】:
-
错误,C++ 有一个
std::set<T>。您是因为想要/需要重新创建它,还是可以使用标准库? -
是的,不幸的是我必须这样做。
-
嗯.. 练习看起来你应该重新发明 std::vector 以以最低性能的方式重新实现 std::set。您允许使用哪些 STL ?
-
明白。 C++ 将
std::set实现为树。我建议尝试实现一个二叉树,这使得查找唯一元素变得微不足道。
标签: c++