在c++中实现一个set模板的添加和删除

Implementing a set template in c++ adding and removing

本文关键字:set 添加 删除 一个 c++ 实现      更新时间:2023-10-16

我需要为SET数据结构创建一个添加和删除函数,而不使用c++的STL。这里是。h代码,我需要写出代码来实现。h代码中的函数。这是一个模板化类,用于将其属性继承到更大范围的项目中。虽然我知道没有太多的事情要做,但我真的很感激你的帮助,因为这是我简单给出的所有指示。

template <class T>
class Set {
public:
Set();
Set(const Set<T> & other); //copy constructor that makes a deep copy
~Set(); //destructor
void add (const T & item); //Adds the item to the set
void remove (const T & item); //Removes the item from the set
}

如果有人能告诉我正确的方向,我将非常感激。

这里有一个简单的方向:看看set和map在STL中的实现。也要读一些关于二叉搜索树(如果你需要实现和std::set一样复杂的东西),尤其是红黑树的知识。另一种选择是使用散列映射。