我想知道如何检查数组中是否存在值或对象,例如在python中:
a = [1,2,3,4,5]
b = 4
if b in a:
print("True!")
else:
print("False")
Run Code Online (Sandbox Code Playgroud)
我想知道cython中是否存在类似的东西.我有一个struct对象数组指针; 我想知道该数组中是否存在该对象.
喜欢
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
Run Code Online (Sandbox Code Playgroud)
上面的代码不正确,但它说明了我的意思.
lus*_*oog 35
你几乎必须遍历数组并检查每个元素.
#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;
}
Run Code Online (Sandbox Code Playgroud)