vector question

K

Kitty

Hello.

Given vector<Object>, I would like to find the position of an element "obj"
in this container. What function should I use? Thanks.
 
H

Howard

Kitty said:
Hello.

Given vector<Object>, I would like to find the position of an element
"obj" in this container. What function should I use? Thanks.

If I undesrstand correctly, vector is not an "associative" container, so you
can't use find() on it. A simple loop using an iterator (or
const_iterator), and doing whatever comparison you need to check if an item
meets your requirement, should do the trick. (You don't say what you mean
by "an element 'obj'", so I'm guessing you've got some way to identify the
object you want...?)

-Howard
 
B

bamity

Kitty said:
Hello.

Given vector<Object>, I would like to find the position of an element "obj"
in this container. What function should I use? Thanks.

Do you want an iterator that points to the object in the vector or the
actual numeric index? If an iterator is what you want, you can call the
std::find() function.

std::vector<Object> vObjects;
// add some Objects into the vector...

std::vector<Object>::iterator it = std::find(vObjects.begin(),
vObjects.end(), obj);
if(it != vObjects.end()){
// then the iterator "it" is ok to use...
}

details at: http://www.sgi.com/tech/stl/find.html

If an iterator isn't what you want, you could always write a for loop,
incrementing the positions until you find the one that matches "obj".
Then that would give you your position as a numeric index.
 
H

Howard

[Re-ordered. Please post responses at the bottom or interspersed as needed,
not at the top.]

Kitty said:
Assume I can identify "obj". Any smart method to do that?

What do you mean? You say you can identify it. So, what's the problem?
Perhaps you need to post the code you have, and let us know exactly what
part of it you're having problems with...?

(Do you have a book that talks about vectors? That's you best source for
proper information.)

-Howard
 
C

Chris Theis

Howard said:
If I undesrstand correctly, vector is not an "associative" container, so you
can't use find() on it. A simple loop using an iterator (or
const_iterator), and doing whatever comparison you need to check if an item
meets your requirement, should do the trick. (You don't say what you mean
by "an element 'obj'", so I'm guessing you've got some way to identify the
object you want...?)

-Howard

You´re right that vector is not an assiciative container but this does not
mean that you cannot call the find() function. However, associative
containers like for example maps have an optimized find function that is
supplied as a member function.

Cheers
Chris
 
C

Chris Theis

Kitty said:
Assume I can identify "obj". Any smart method to do that?
[SNIP]

Please don´t top post. A smart method would be to use the find function and
a uniary predicate.

Chris
 
H

Howard

Howard said:
If I undesrstand correctly, vector is not an "associative" container, so
you can't use find() on it. A simple loop using an iterator (or
const_iterator), and doing whatever comparison you need to check if an
item meets your requirement, should do the trick. (You don't say what you
mean by "an element 'obj'", so I'm guessing you've got some way to
identify the object you want...?)

-Howard

Reading further, it looks like std::find() can be used on a vector. You
pass it begin and end iterators for your vector, followed by an instance of
the object you wish to match against. (It uses the operator == to test for
equality, which you can define yourself for the class in question.)

-Howard
 
C

Chris Theis

[SNIP]>
If an iterator isn't what you want, you could always write a for loop,
incrementing the positions until you find the one that matches "obj".
Then that would give you your position as a numeric index.

You can still use the find algorithm that returns an iterator and call the
distance() function subsequently. But you end up running through the loop
two times under the hood if you first have to search for the object and then
call the distance function.

Chris
 
J

Jeff Flinn

Chris Theis said:
[SNIP]>
If an iterator isn't what you want, you could always write a for loop,
incrementing the positions until you find the one that matches "obj".
Then that would give you your position as a numeric index.

You can still use the find algorithm that returns an iterator and call the
distance() function subsequently. But you end up running through the loop
two times under the hood if you first have to search for the object and
then
call the distance function.

Not typically, if were talking about std::vector. IIRC, std::distance is
specialized for random access iterators, simply returning
iterator2-iterator1.

Jeff Flinn
 
C

Chris Theis

Jeff Flinn said:
Chris Theis said:
[SNIP]>
If an iterator isn't what you want, you could always write a for loop,
incrementing the positions until you find the one that matches "obj".
Then that would give you your position as a numeric index.

You can still use the find algorithm that returns an iterator and call the
distance() function subsequently. But you end up running through the loop
two times under the hood if you first have to search for the object and
then
call the distance function.

Not typically, if were talking about std::vector. IIRC, std::distance is
specialized for random access iterators, simply returning
iterator2-iterator1.

Jeff Flinn

That´s true. I should have phrased my answer more precisely that this
depends on the container and the iterator type.

Cheers
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,661
Latest member
FloridaHan

Latest Threads

Top