检查空/空的数组位置

时间:2021-02-19 21:19:23

I have an array which might contain empty/null positions (e.g: array[2]=3, array[4]=empty/unassigned). I want to check in a loop whether the array position is null.

我有一个可能包含空/空位置的数组(e。旅客:数组[2]= 3,[4]=空/未赋值的数组)。我想在一个循环中检查数组的位置是否为空。

array[4]==NULL //this doesn't work

I'm pretty new to C++.
Thanks.

我对c++很陌生。谢谢。


Edit: Here's more code; A header file contains the following declaration

编辑:这是更多的代码;头文件包含以下声明。

int y[50];

The population of the array is done in another class,

数组的总体在另一个类中完成,

geoGraph.y[x] = nums[x];

The array should be checked for null in the following code;

应该在以下代码中检查数组是否为空;

    int x=0;
    for(int i=0; i<sizeof(y);i++){
        //check for null
        p[i].SetPoint(Recto.Height()-x,y[i]);
        if(i>0){
            dc.MoveTo(p[i-1]);
            dc.LineTo(p[i]);

        }
        x+=50;
    }

4 个解决方案

#1


12  

If your array is not initialized then it contains randoms values and cannot be checked !

如果您的数组没有初始化,那么它包含randoms值,并且不能被检查!

To initialize your array with 0 values:

用0值初始化数组:

int array[5] = {0};

Then you can check if the value is 0:

然后你可以检查这个值是否为0:

array[4] == 0;

When you compare to NULL, it compares to 0 as the NULL is defined as integer value 0 or 0L.

当您比较NULL时,它与0比较,因为NULL被定义为整数值0或0L。

If you have an array of pointers, better use the nullptr value to check:

如果您有一个指针数组,最好使用nullptr值来检查:

char* array[5] = {nullptr}; // we defined an array of char*, initialized to nullptr

if (array[4] == nullptr)
    // do something

#2


3  

If the array contains integers, the value cannot be NULL. NULL can be used if the array contains pointers.

如果数组包含整数,则该值不能为空。如果数组包含指针,可以使用NULL。

SomeClass* myArray[2];
myArray[0] = new SomeClass();
myArray[1] = NULL;

if (myArray[0] != NULL) { // this will be executed }
if (myArray[1] != NULL) { // this will NOT be executed }

As http://en.cppreference.com/w/cpp/types/NULL states, NULL is a null pointer constant!

因为http://en.cppreference.com/w/cpp/types/NULL状态,NULL是一个空指针常量!

#3


2  

You can use boost::optional (optional), which was developed in particular for decision of your problem:

您可以使用boost:::optional(可选),它是专门为解决问题而开发的:

boost::optional<int> y[50];
....
geoGraph.y[x] = nums[x];
....
const size_t size_y = sizeof(y)/sizeof(y[0]); //!!!! correct size of y!!!!
for(int i=0; i<size_y;i++){
   if(y[i]) { //check for null
      p[i].SetPoint(Recto.Height()-x,*y[i]);
      ....
   }
}

P.S. Do not use C-type array -> use std::array or std::vector:

注意:不要使用c型数组->使用std:::数组或std::vector:

std::array<int, 50> y;   //not int y[50] !!!

#4


1  

There is no bound checking in array in C programming. If you declare array as

在C编程中,数组中没有绑定检查。如果你声明数组为

int arr[50];

Then you can even write as

你甚至可以写成

arr[51] = 10;

The compiler would not throw an error. Hope this answers your question.

编译器不会抛出错误。希望这能回答你的问题。

#1


12  

If your array is not initialized then it contains randoms values and cannot be checked !

如果您的数组没有初始化,那么它包含randoms值,并且不能被检查!

To initialize your array with 0 values:

用0值初始化数组:

int array[5] = {0};

Then you can check if the value is 0:

然后你可以检查这个值是否为0:

array[4] == 0;

When you compare to NULL, it compares to 0 as the NULL is defined as integer value 0 or 0L.

当您比较NULL时,它与0比较,因为NULL被定义为整数值0或0L。

If you have an array of pointers, better use the nullptr value to check:

如果您有一个指针数组,最好使用nullptr值来检查:

char* array[5] = {nullptr}; // we defined an array of char*, initialized to nullptr

if (array[4] == nullptr)
    // do something

#2


3  

If the array contains integers, the value cannot be NULL. NULL can be used if the array contains pointers.

如果数组包含整数,则该值不能为空。如果数组包含指针,可以使用NULL。

SomeClass* myArray[2];
myArray[0] = new SomeClass();
myArray[1] = NULL;

if (myArray[0] != NULL) { // this will be executed }
if (myArray[1] != NULL) { // this will NOT be executed }

As http://en.cppreference.com/w/cpp/types/NULL states, NULL is a null pointer constant!

因为http://en.cppreference.com/w/cpp/types/NULL状态,NULL是一个空指针常量!

#3


2  

You can use boost::optional (optional), which was developed in particular for decision of your problem:

您可以使用boost:::optional(可选),它是专门为解决问题而开发的:

boost::optional<int> y[50];
....
geoGraph.y[x] = nums[x];
....
const size_t size_y = sizeof(y)/sizeof(y[0]); //!!!! correct size of y!!!!
for(int i=0; i<size_y;i++){
   if(y[i]) { //check for null
      p[i].SetPoint(Recto.Height()-x,*y[i]);
      ....
   }
}

P.S. Do not use C-type array -> use std::array or std::vector:

注意:不要使用c型数组->使用std:::数组或std::vector:

std::array<int, 50> y;   //not int y[50] !!!

#4


1  

There is no bound checking in array in C programming. If you declare array as

在C编程中,数组中没有绑定检查。如果你声明数组为

int arr[50];

Then you can even write as

你甚至可以写成

arr[51] = 10;

The compiler would not throw an error. Hope this answers your question.

编译器不会抛出错误。希望这能回答你的问题。