C语言 检查 Cython 中的数组中是否存在值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15094834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:27:10  来源:igfitidea点击:

Check if a value exists in an array in Cython

carrayscython

提问by Jean-Francois Gallant

I want to know how to check if a value or a object exists in an array, like in python:

我想知道如何检查数组中是否存在值或对象,例如在 python 中:

a = [1,2,3,4,5]
b = 4
if b in a:
    print("True!")
else:
    print("False")

I want to know if something similar already exists in cython. I have a struct object array of pointers; I want to know if the object exists in this array.

我想知道cython中是否已经存在类似的东西。我有一个指针结构对象数组;我想知道该对象是否存在于该数组中。

Like

喜欢

cdef Node *array

array = <Node *>malloc( 5 * cython.sizeof(Node))

for i in range(5):
     array[i].index = i

cdef Node test = array[3]

if test in array:
    print("True!")

cdef struct Node:
    int index

The code above is not correct, but it illustrates what I mean.

上面的代码不正确,但它说明了我的意思。

回答by luser droog

You pretty much have to iterate through the array and check each element.

您几乎必须遍历数组并检查每个元素。

#include <stdbool.h>

bool isvalueinarray(int val, int *arr, int size){
    int i;
    for (i=0; i < size; i++) {
        if (arr[i] == val)
            return true;
    }
    return false;
}

回答by PADYMKO

Try next:

接下来试试:

(read comments and tests for details how it working)

(阅读评论和测试以了解其工作原理)

#include <stdio.h>
#include <assert.h>


/*
Return index an integer number begin from start an array,
otherwise return -1.
 */
int indexOf(int *array, int array_size, int number) {
    for (int i = 0; i < array_size; ++i) {
        if (array[i] == number) {
            return i;
        }
    }
    return -1;
}


// Tests for indexOf()
void test_indexOf() {
    int array[10] = {12, 78, -43, 0, 21, 12, 20, -9, 1, -1};
    assert(indexOf(array, 10, 12) == 0);
    assert(indexOf(array, 10, 0) == 3);
    assert(indexOf(array, 10, 2) == -1);
    assert(indexOf(array, 10, 43) == -1);
    assert(indexOf(array, 10, 11) == -1);
    assert(indexOf(array, 10, -1) == 9);
    assert(indexOf(array, 10, 1) == 8);
    assert(indexOf(array, 10, -2) == -1);
    assert(indexOf(array, 10, 3) == -1);
}


int main () {
    test_indexOf();
    return 0;
}

Notes:

笔记:

  1. Since the C has not support for reload functions, indexOf() will be work only with array type of integer.

  2. The C has not support for determinate length of a pointer of an array. So, you need manually pass size of array.

  3. Testing environment:

  1. 由于 C 不支持重新加载函数,因此 indexOf() 将仅适用于整数数组类型。

  2. C 不支持确定数组指针的长度。因此,您需要手动传递数组的大小。

  3. 测试环境:

-

——

$ uname -a
Linux wlysenko-Aspire 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
$ gcc --version
gcc (Ubuntu 4.8.5-2ubuntu1~14.04.1) 4.8.5