449,018 Members | 870 Online Need help? Post your question and get tips & solutions from a community of 449,018 IT Pros & Developers. It's quick & easy.

# Find the position of an elemant in a vector

 P: n/a Hi, I've to write a function similar to this. int process(const vector& vct,int key) { // Here I've to find the position of key in the vector and do some processing. } Is there any fucntion in algorithm which directly gives the position of an elemant. find() returns an iterator but not the postion of the elemant. Is there any function equivalent to indexof()? Regards, Sunil Nov 16 '06 #1
5 Replies

 P: n/a Sunil Varma wrote: Hi, I've to write a function similar to this. int process(const vector& vct,int key) { // Here I've to find the position of key in the vector and do some processing. } Is there any fucntion in algorithm which directly gives the position of an elemant. find() returns an iterator but not the postion of the elemant. Is there any function equivalent to indexof()? Use the result of find() - vct.begin(). Cheers! --M Nov 16 '06 #2

 P: n/a mlimber wrote: Sunil Varma wrote: Hi, I've to write a function similar to this. int process(const vector& vct,int key) { // Here I've to find the position of key in the vector and do some processing. } Is there any fucntion in algorithm which directly gives the position of an elemant. find() returns an iterator but not the postion of the elemant. Is there any function equivalent to indexof()? Use the result of find() - vct.begin(). Cheers! --M Hi limber, It worked fine. Thank you. Nov 16 '06 #3

 P: n/a There is a more 'official' method: the 'distance' function is guaranteed to work, whereas the subtraction is not. Especially when you swap your vector with a list at some point, the subtraction will give you a compiler error, whereas 'distance' will give you the right answer. The subtraction can only work on random access iterators. For vectors, however, the subtraction is fine and works. Stefan mlimber wrote: Sunil Varma wrote: Hi, I've to write a function similar to this. int process(const vector& vct,int key) { // Here I've to find the position of key in the vector and do some processing. } Is there any fucntion in algorithm which directly gives the position of an elemant. find() returns an iterator but not the postion of the elemant. Is there any function equivalent to indexof()? Use the result of find() - vct.begin(). Cheers! --M Nov 16 '06 #4

 P: n/a "Sunil Varma"

 P: n/a On 16 Nov, 21:08, "Andrew Koenig"

### This discussion thread is closed

Replies have been disabled for this discussion. 