Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查Cython中的数组中是否存在值_C_Arrays_Cython - Fatal编程技术网

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

检查Cython中的数组中是否存在值,c,arrays,cython,C,Arrays,Cython,我想知道如何检查数组中是否存在值或对象,如python中: a = [1,2,3,4,5] b = 4 if b in a: print("True!") else: print("False") 我想知道cython是否已经存在类似的东西。我有一个指针的结构对象数组;我想知道这个数组中是否存在该对象 像 cdef节点*数组 数组=malloc(5*cython.sizeof(节点)) 对于范围(5)中的i: 数组[i]。索引=i cdef节点测试=阵列[3] 如果在阵列中进行

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

a = [1,2,3,4,5]
b = 4
if b in a:
    print("True!")
else:
    print("False")
我想知道cython是否已经存在类似的东西。我有一个指针的结构对象数组;我想知道这个数组中是否存在该对象

cdef节点*数组
数组=malloc(5*cython.sizeof(节点))
对于范围(5)中的i:
数组[i]。索引=i
cdef节点测试=阵列[3]
如果在阵列中进行测试:
打印(“真!”)
cdef结构节点:
整数索引

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

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

#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;
}
#包括
bool isvalueinarray(int-val,int*arr,int-size){
int i;
对于(i=0;i
请尝试下一步:

(阅读评论和测试了解其工作原理的详细信息)


谢谢,我认为这是最好的解决办法。我的数组很短,我必须在其中搜索很多次。每一次的分类和搜索可能都不会带来很大的时间改进。谢谢。
#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;
}
#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;
}
$ 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