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

前端 未结 10 1980
情深已故
情深已故 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 09:06

    First of all, do you really need to store indices like this? Have you looked into std::map, enabling you to store key => value pairs?

    Secondly, if you used iterators instead, you would be able to return std::vector.end() to indicate an invalid result. To convert an iterator to an index you simply use

    size_t i = it - myvector.begin();
    

提交回复
热议问题