Sign in to follow this  

std vectors

This topic is 4333 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I have a vector of ints or floats, is there any algorithm that lets me find the place of a certain number? For example if I have a vector with numbers 1-10 and I'm looking for the number 6, I want to get output 5. Thanks.

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
int index_of(const std::vector<T> & v, const T & val)
{
std::vector<T>::iterator i = std::find(v, val);
assert( i != v.end() );
return distance(v.begin(), i);
}


Fixed?


should this be like that?
int index_of(const std::vector<T> & v, const T & val)
{
std::vector<T>::iterator i = std::find(v.begin(),v.end(), val);
assert( i != v.end() );
return distance(v.begin(), i);
}


or are you using some special sort of STL?
In my book The C++ Standard Library from Nicolai M.Josuttis theres no find function that expects a reference to a container, instead all find functions he mentions use iterators.
I tried to compile your code anyways, but this didn t work either so I guess it s been a typo :)



[Edited by - Basiror on January 29, 2006 3:58:52 AM]

Share this post


Link to post
Share on other sites
#include <stdexcept>

template<class T>
size_t index_of(const std::vector<T> & v, const T & val)
{
std::vector<T>::iterator i = std::find(v.begin(),v.end(), val);

if (i==v.end()) throw std::domain_error("The value was not found.");

return distance(v.begin(), i);
}


Quote:
A domain error occurs when the function is not defined for its input argument value or values.


Pure iterator version:

#include <stdexcept>
#include <iterator>

template<class ForwardIterator>
size_t index_of(ForwardIterator begin, ForwardIterator end,
const std::iterator_traits<ForwardIterator>::reference val)
{
ForwardIterator i = std::find(begin, end, val);

if (i==end) throw std::domain_error("The value was not found.");

return distance(begin, i);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Basiror
should this be like that?

Nope, you caught something that I didn't :-). The lack of checking if the return of a find() against end() is all I noticed D:.
Quote:
Original post by Fruny
...

Probably better for the general case, yes. Showoff. ;-).

Share this post


Link to post
Share on other sites
And since you usually want your return value to indicate a failure to find the value instead of bombing out.....

template<class ForwardIterator>
typename iterator_traits<ForwardIterator>::difference_type
index_of(ForwardIterator begin, ForwardIterator end,
const std::iterator_traits<ForwardIterator>::reference val)
{
ForwardIterator i = std::find(begin, end, val);
return (i!=end) ? std::distance(begin, i) : -1;
}

Share this post


Link to post
Share on other sites
Why check for the end? It doesn't seem that any of the functions requires it, and simply returning the distance to the end {which, when using .begin() and .end(), should be the equal to or greater than .size() } would be consistant with the stl's use of .end() itself.

Share this post


Link to post
Share on other sites

This topic is 4333 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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