By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,018 Members | 870 Online
Bytes IT Community
+ Ask a Question
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<int>& 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
Share this Question
Share on Google+
5 Replies


P: n/a

Sunil Varma wrote:
Hi,

I've to write a function similar to this.

int process(const vector<int>& 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<int>& 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<int>& 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" <su*******@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...
find() returns an iterator but not the postion of the elemant.
How's that again? An iterator *is* the position of the element.
Nov 16 '06 #5

P: n/a


On 16 Nov, 21:08, "Andrew Koenig" <a...@acm.orgwrote:
"Sunil Varma" <sunil....@gmail.comwrote in messagenews:11**********************@k70g2000cwa.g ooglegroups.com...
find() returns an iterator but not the postion of the elemant.How's that again? An iterator *is* the position of the element.
Not quite, more of a pointer to the element, not it's position. It does
not tell you anything about where in a container (of any type) the
element is. It's not much of a differance but think of it like this:
take the first element of a list and take an iterator to it, the insert
a new element at the beginning of the list, the iterator still points
to the original element even though it has changed position in the
list.

--
Erik Wikström

Nov 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.