# std vectors

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

## 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 on other sites
int index_of(const std::vector<T> & v, const T & val)
{
std::vector<T>::iterator i = std::find(v, val);
return distance(v.begin(), i);
}

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by MaulingMonkeyint index_of(const std::vector & v, const T & val){ std::vector::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 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);

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);

return distance(begin, i);
}

##### Share on other sites
Quote:
 Original post by Basirorshould 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 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 on other sites
I'd just like to say that the community coding process exhibited here is really impressive/interesting to watch in action. :)

Thanks everyone!

##### 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.

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 76
• 11
• 10
• 14
• ### Forum Statistics

• Total Topics
632968
• Total Posts
3009584
• ### Who's Online (See full list)

There are no registered users currently online

×