Question on find on a vector - is it just a linear search?

A

Angus

I assume that find on a vector is a linear search a bit like iterating
through the items in vector until first instance found?

If so, then I suppose it is slightly more convenient than writing code
to iterate vector.

If more than the odd search required obviously would be better to sort
vector and use lower_bound I guess.
 
N

Noah Roberts

I assume that find on a vector is a linear search a bit like iterating
through the items in vector until first instance found?

If so, then I suppose it is slightly more convenient than writing code
to iterate vector.

If more than the odd search required obviously would be better to sort
vector and use lower_bound I guess.

There's also std::binary_search.
 
J

Jeff Flinn

Noah said:
There's also std::binary_search.

That only tells you whether the item is contained in a sorted sequence,
it doesn't return an iterator to the found item as does find or lower_bound.

Jeff
 
J

Juha Nieminen

Jeff Flinn said:
That only tells you whether the item is contained in a sorted sequence,
it doesn't return an iterator to the found item as does find or lower_bound.

Technically speaking std::lower_bound does not return an iterator to
an element in the container that is equal to the searched element. It
returns an iterator to the first location where, if you inserted the
searched element, the container would still be sorted.

If the searched element was already in the container, then that
location happens to contain the first such element. However, more
importantly, if the searched element is not already in the container,
the iterator will still just point to somewhere in the container
(according to the abovementioned rule). In order to see if the searched
element is there you will have to compare the element pointed by the
iterator to the searched element explicitly (taking into account that
the iterator could point to the end of the container).
 
A

Angus

  Technically speaking std::lower_bound does not return an iterator to
an element in the container that is equal to the searched element. It
returns an iterator to the first location where, if you inserted the
searched element, the container would still be sorted.

  If the searched element was already in the container, then that
location happens to contain the first such element. However, more
importantly, if the searched element is not already in the container,
the iterator will still just point to somewhere in the container
(according to the abovementioned rule). In order to see if the searched
element is there you will have to compare the element pointed by the
iterator to the searched element explicitly (taking into account that
the iterator could point to the end of the container).

I don't think this is correct. Because if the item is NOT in the list
then the iterator returned is the end of the container. This
conflicts with your analysis.
 
J

Jeff Flinn

Angus said:
I don't think this is correct. Because if the item is NOT in the list
then the iterator returned is the end of the container. This
conflicts with your analysis.

Juha is correct. Can you provide an example disproving him?

In any case, maybe your design would be better implemented with a
std::set or std::map which provide find methods to give the behavior you
require.

Jeff
 
J

James Kanze

That's not just a technicality. It's the basic definition of
lower_bound.
I don't think this is correct. Because if the item is NOT in the list
then the iterator returned is the end of the container. This
conflicts with your analysis.

I'd suggest you look up the specification of lower_bound. It
returns an iterator to the first element less than or equal to
the search item.
 
J

James Kanze

this can be expensive since a vector guarantees contiguous memory storage.

Why does contiguous memory make sorting and binary search more
expensive? If anything, it makes them less expensive (because
of improved locality).
 
M

Michael Doubez

Not true; lower_bound will return an iterator to the first element
*greater* than or equal to the search item or an iterator to the end of
the range.

Nitpicking: Formally it returns the iterator to the first element that
doesn't compare strictly less than the search item (or end of range
failing that).

This is of course mathematic
 
A

Angus

Juha is correct. Can you provide an example disproving him?

In any case, maybe your design would be better implemented with a
std::set or std::map which provide find methods to give the behavior you
require.

Jeff

Sorry, yes you are correct. You have to also check iterator returned
is the same value as value searched.
 
J

James Kanze

On 26/03/2011 13:17, James Kanze wrote:
[...]
I'd suggest you look up the specification of lower_bound. It
returns an iterator to the first element less than or equal to
the search item.
Not true; lower_bound will return an iterator to the first element
*greater* than or equal to the search item or an iterator to the end of
the range.

Oops. Typo on my part. (Formally, the standard words it a
little different, but what it comes out to is >=.)
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top