简体   繁体   English

如何检查对象数组是否为空/空?

[英]How do I check if an array of objects is empty/null?

Lets say I have an object array declared like this: 可以说我有一个这样声明的对象数组:

Object Array[100];
int count = 0;
bool exit;

do
{
    if (Array[count] == "")
    {
        //code that stores data
        exit = true;
    }
    else
    {
        count++;
    }
}
while (exit != true);

I keep having errors from the compiler which says: 我一直在编译器中显示错误消息:

error: no match for ‘operator==’ in ‘Array[count] == ""'

I know I can use a for loop function to store them correctly or even use a vector, but for now, I have to use this method to check if the array is empty/null. 我知道我可以使用for循环函数正确地存储它们,甚至可以使用向量,但是现在,我必须使用此方法来检查数组是否为空/空。 Any idea how to do it? 知道怎么做吗? I have seen many examples here, but almost all of it are string/int/float etc etc arrays. 我在这里看到了很多示例,但是几乎所有示例都是string / int / float等数组。

When you created the array using: Object Array[100]; 使用以下方法创建数组时: Object Array[100]; it already created 100 memory slots for your Object s, and called the default constructor for each one of them. 已经为您的Object 创建了 100个内存插槽,并为每个插槽调用了默认的构造函数。

  • Physically, there are 100 elements in your array right after the quoted line. 物理上,在引用行之后,数组中有100个元素。

  • Logically, there are as much as you decide, and its best you save that number to count . 从逻辑上讲,您可以决定多少,最好还是保存该数字以进行count


If the Object 's default c'tor is not enough in terms of initialization, then you have several options for checking weather it's content is initialized or not: 如果Object的默认值在初始化方面还不够,那么您可以通过几种方法来检查其内容是否已初始化:

  • you can add a method (ie bool isInitialized() ) 您可以添加一个方法(即bool isInitialized()
  • you can add an operator== that takes const char * as an argument and compare it to "" . 您可以添加一个将const char *作为参数的operator==并将其与""进行比较。
  • you can add an operator== that takes another Object as an argument and compare it to "" assuming Object has a c'tor from const char * . 您可以添加一个operator== ,该operator==将另一个Object用作参数,并将其与""进行比较,假定Object具有来自const char *的c'tor。

The array itself cannot be "empty". 数组本身不能为“空”。

Object Array[100];

is a declaration that creates an array of 100 Object s. 是创建100个Object的数组的声明。 They are already constructed and present (if default constructible). 它们已经构造并存在(如果默认可构造)。

If the type Object has some notion of an "empty" state you could check for that (A std::vector for example provides the member .empty() to check for emptyness.) or you can use a container to hold your data (which also makes it part of the heap instead of the stack memory and enables dynamic resizing). 如果类型Object具有某种“空”状态的概念,则可以检查该状态(例如std::vector提供了成员.empty()来检查是否为空。),也可以使用容器来保存数据(这也使其成为堆的一部分,而不是堆栈存储器的一部分,并实现动态调整大小。

std::vector<Object> vec;
int count = 0;
// do stuff...
do
{
    if (count >= vec.size())
    {
        //code that stores data
        exit = true;
    }
    else
    {
        count++;
    }
}
while (exit != true);

Whereas you could also say std::size_t count = vec.size(); 而您也可以说std::size_t count = vec.size(); .

Object Array[100]; cannot be empty. 不能为空。 On the other hand, pointer to dynamically allocated array can be initialized to nullptr and checked against that: 另一方面,可以将指向动态分配数组的指针初始化为nullptr并对此进行检查:

Object *Array = nullptr;

if(!Array) // or Array == nullptr
    cout << "Array is empty (unallocated)" << endl;

Array = new Array[100];

if(Array) // or Array != nullptr
    cout << "Array is NOT empty" << endl;

Array[count] doesn't make sense. Array[count]没有任何意义。 you are accessing element at index count (which is a bad name for index) and comparing that with const char* . 您正在以索引count (这对索引来说是个坏名字)访问元素,并将其与const char*进行比较。 std::vector is the way to go. std::vector是必经之路。 Code above (and everything other than using std::vector ) - ugly and bad practice. 上面的代码(以及除使用std::vector之外的所有代码)-丑陋和不良做法。

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

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