【问题标题】:combing two sets of integers using removeDifferent() and removeSame()使用 remove Different() 和 remove Same() 组合两组整数
【发布时间】: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&lt;T&gt;。您是因为想要/需要重新创建它,还是可以使用标准库?
  • 是的,不幸的是我必须这样做。
  • 嗯.. 练习看起来你应该重新发明 std::vector 以以最低性能的方式重新实现 std::set。您允许使用哪些 STL
  • 明白。 C++ 将std::set 实现为树。我建议尝试实现一个二叉树,这使得查找唯一元素变得微不足道。

标签: c++


【解决方案1】:

好吧,您已经实现了您的 contains 方法,并且假设您已经正确地实现了,您应该在实现其他功能时使用该方法。

removeDifferent(IntSet setB){
  for(int i = 0; i < numberOfValues; i++){
    if(!setB.contains(ptr[i])){
       remove(ptr[i]);
       i--; //make sure not to skip any elements by moving back one
    }
  } 
} //end of removeDifferent

removeSame(IntSet setB){
  for(int i = 0; i < numberOfValues; i++){
    if(setB.contains(ptr[i])){
       remove(ptr[i]);
       i--; //make sure not to skip any elements by moving back one
    }
  } 
} //end of removeSame

至于您的 add all 函数,我认为没有添加其他列表元素的好方法,因为 int* ptr 是私有的,并且没有返回任何元素的方法。从理论上讲,您可以扫描每个可能的整数值,但这绝对不是理想的,可能不是预期的解决方案。你确定这里所有的方法都正确吗?

#include <climits> //need to include that for this one

void addAll(IntSet setB){
  for(int i = INT_MIN; i < INT_MAX && !setB.isEmpty(); i++){
    int removals = 0; 
    while(setB.contains(i)){ //temporarily remove all i's in setB
       removals++;
       add(i);
       setB.remove(i);
    }
    while(removals > 0){ //add back all i's that we previously removed
       setB.add(i);
       removals--;
    }
  }
} 

我没有检查这些是否编译,但这绝对是这些函数的基本思想。

祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2020-11-01
    • 1970-01-01
    • 2016-01-29
    • 2014-12-23
    • 2021-08-03
    相关资源
    最近更新 更多