Sign in to follow this  
Yourself

[C++] iterator to index

Recommended Posts

hi all, I'm using std::find on a std::vector, this returns the iterator but I need the index of the returned iterator. if I'm not mistaking C# has a build in function for this (indexof()) but I need this in c++. is there a quick easy way to know this ? or do I have just have to loop though the vector , compaire it to the iterator and if equal return the loop index ? cheers

Share this post


Link to post
Share on other sites
It's worth pointing out there are various kind of iterators, std::vector's iterator is random access iterator so binary - operator is defined but if you want to keep your code generic then use visitor's solution, std::distance, this function is specialized for different kinds of iterator and will pick the most optimal method (like with random access iteartors it can use binary - operator).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this