find vector
Definition
int find_vector(vector_t *vector, void *object);
Description
Finds the specified object using the previous supplied compare_func_t function.
Parameters
- vector
- Vector in which to search
- object
- Object to search for.
Return Value
When the object is found, the index is returned. Else -1 is returned.
Example
#include <stdio.h> #include <libc/string.h> signed char compare_double_value(void *ptr1, void *ptr2) { if(*((double *)ptr1) < *((double *)ptr2)) { return VECTOR_COMPARE_LESS; } else if(*((double *)ptr1) == *((double *)ptr2)) { return VECTOR_COMPARE_EQUAL; } else { return VECTOR_COMPARE_MORE; } } int main(int argc, char *argv[]) { int index = 0; double *d1 = malloc(sizeof(double)); double *d2 = malloc(sizeof(double)); double *d3 = malloc(sizeof(double)); double *d4 = malloc(sizeof(double)); double *search = malloc(sizeof(double)); *d1 = 3.3; *d2 = 5.8; *d3 = 7.6; *d4 = 8.2; vector_t *vector = create_vector_s(compare_double_value); push_vector(vector, d1); push_vector(vector, d2); push_vector(vector, d3); push_vector(vector, d4); *search = 7.6; index = find_vector(vector, search); printf("index: %i\n", index); destroy_vector_all(vector, free); free(search); return 0; }
Output:
index: 2