检查是否值的数组中存在 [英] Check if a value exist in a array

查看:172
本文介绍了检查是否值的数组中存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何检查一个值或一个物体像蟒蛇一个数组中存在:

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

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

但我想知道,如果东西用Cython已经存在。我有指针的结构对象数组,想知道如果对象数组中存在。像

but I want to know if something already exist in cython. I have a struct object array of pointer and want to know if the object exist in this array. 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

下code不是正确的,但它是想说明我的意思。

The code below is not correct but it's for trying to illustrate what I mean.

推荐答案

您pretty有很多通过数组进行迭代,并检查每一个元素。

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;
}

这篇关于检查是否值的数组中存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆