How do I find an element position in std::vector?

前端 未结 10 1965
情深已故
情深已故 2021-01-31 08:40

I need to find an element position in an std::vector to use it for referencing an element in another vector:

int find( const vector& whe         


        
10条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 08:50

    Take a look at the answers provided for this question: Invalid value for size_t?. Also you can use std::find_if with std::distance to get the index.

    std::vector::iterator iter = std::find_if(vec.begin(), vec.end(), comparisonFunc);
    size_t index = std::distance(vec.begin(), iter);
    if(index == vec.size()) 
    {
        //invalid
    }
    

提交回复
热议问题