简体   繁体   English

如何检查类数组的元素是否为空? [c ++]

[英]How do I check if an element of an array of classes is empty? [c++]

For my latest assignment, I need to create a hash table that houses stocks, which are encapsulated in a class. 对于我的最新任务,我需要创建一个散列表,其中包含封装在类中的股票。 To avoid collisions, I need to use linear probing. 为了避免冲突,我需要使用线性探测。 The problem I've run into, however; 但是,我遇到了这个问题。 is that I can't test whether or not an element of the array (which is the hash table) is empty. 是我无法测试数组的元素(即哈希表)是否为空。

Here's some code aggregated from several files, but this is just to give you an idea of what's going on. 这是从几个文件中汇总的一些代码,但这只是为了让您了解发生了什么。

class Stock{

friend class HashMap

}

class HashMap{

bool get() //this function is used for putting new stocks into the table
private:
  struct Slot {
    Stock  slotStock;
  }
Slot *slots;
}

Within the get() function 在get()函数中

while(slots[index] != NULL)

This gives an error: no operator "!=" matches these operands HashMap::Slot != int 这给出了一个错误:没有运算符“!=”匹配这些操作数HashMap :: Slot!= int

What alternative way would there be for me to check whether or not a slot is empty? 我有什么其他方法可以检查插槽是否为空?

The array is allocated dynamically. 该数组是动态分配的。

EDIT: When I initialize the array, does it use the default constructor to create an object for each element of the array, or does it leave the elements empty? 编辑:当我初始化数组时,它是否使用默认构造函数为数组的每个元素创建一个对象,还是将元素留空?

If you have an array of objects of type X , none of the slots are "empty". 如果您有X类型的对象数组,则所有插槽都不是“空”的。 They all contain an object of type X . 它们都包含X类型的对象。 To represent an empty object, it needs to be a possible state of the type which is stored in the array. 为了表示一个空对象,它必须是存储在数组中的可能类型的状态。 You could, for example, have boost::optional<Slot> , or std::unique_ptr<Slot> . 例如,您可以使用boost::optional<Slot>std::unique_ptr<Slot> Otherwise, you can encode the state directly into your Slot class (with a bool member, for example). 否则,您可以将状态直接编码到Slot类中(例如,使用bool成员)。

What you want to do is store an array of Stock pointers, whereas you are currently storing an array of Slot objects. 您想要做的是存储一个Stock指针数组,而当前正在存储一个Slot对象数组。 To make things even easier on yourself, you can use a vector to store the pointers. 为了使事情变得更轻松,您可以使用向量存储指针。

Your backing data structure would look like this: 您的支持数据结构如下所示:

std::vector< Stock* > vecStocks;

Each item in the vector is a "slot", and you do not need your Slot class unless you intend to store some metadata about the stock. 向量中的每个项目都是一个“插槽”,除非您打算存储有关库存的某些元数据,否则不需要插槽类。

To check whether or not you have a stock in any slot of the vector, you compare the vector item to NULL like this: 要检查向量的任何位置中是否有股票,可以将向量项目与NULL进行比较,如下所示:

if (vecStocks[index] == NULL)

This approach has the positive side effect of not needing to allocate X number of Stock objects up front, where X is the size of your hashmap (potentially a very large number, depending on how often you like to collide). 这种方法具有积极的副作用,不需要预先分配X个Stock对象,其中X是您的哈希图的大小(可能很大,具体取决于您要碰撞的频率)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM