Skip to content

VecFind

VecFind

Description

Find the first element equal to the searched value.

item_ptr must point to a value comparable with vector elements. Use &LVAL(expr) when searching with a temporary expression.

Parameters

Name Direction Description
v in Vector to search.
item_ptr in Pointer to searched value.
compare in Comparator returning 0 for equality.

Success

Index of first matching element.

Failure

SIZE_MAX if no element matches.

Usage example (Cross-references)

Usage examples (Cross-references)
    // Test VecEmpty, VecFind, and VecContains functions
    bool test_vec_empty_find_contains(void) {
        WriteFmt("Testing VecEmpty, VecFind, and VecContains\n");
    
        typedef Vec(int) IntVec;
        int  missing = 99;
        bool result  = VecEmpty(&vec);
        result       = result && (VecFind(&vec, &needle, compare_ints) == SIZE_MAX);
        result       = result && !VecContains(&vec, &needle, compare_ints);
    
        result = result && !VecEmpty(&vec);
        result = result && (VecFind(&vec, &needle, compare_ints) == 1);
        result = result && VecContains(&vec, &needle, compare_ints);
        result = result && !VecContains(&vec, &missing, compare_ints);
        result = result && VecContains(&vec, &needle, compare_ints);
        result = result && !VecContains(&vec, &missing, compare_ints);
        result = result && (VecFind(&vec, &missing, compare_ints) == SIZE_MAX);
    
        VecDeinit(&vec);
    /// TAGS: Vec, Contains, Search, Compare
    ///
    #define VecContains(v, item_ptr, compare) (VecFind((v), (item_ptr), (compare)) != SIZE_MAX)
    
    #endif // MISRA_STD_CONTAINER_VEC_ACCESS_H
Last updated on