Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Question on find on a vector - is it just a linear search? (http://www.velocityreviews.com/forums/t745527-question-on-find-on-a-vector-is-it-just-a-linear-search.html)

 Angus 03-22-2011 02:52 PM

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

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.

 Noah Roberts 03-22-2011 04:21 PM

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

On 3/22/2011 7:52 AM, Angus wrote:
> 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.

--
http://crazycpp.wordpress.com

 Jeff Flinn 03-23-2011 12:49 PM

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

Noah Roberts wrote:
> On 3/22/2011 7:52 AM, Angus wrote:
>> 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.

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

 Juha Nieminen 03-24-2011 09:08 AM

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

Jeff Flinn <TriumphSprint2000@hotmail.com> wrote:
> 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).

 Angus 03-24-2011 11:11 AM

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

On Mar 24, 9:08*am, Juha Nieminen <nos...@thanks.invalid> wrote:
> Jeff Flinn <TriumphSprint2...@hotmail.com> wrote:
> > 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).

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

 Jeff Flinn 03-24-2011 01:11 PM

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

Angus wrote:
> On Mar 24, 9:08 am, Juha Nieminen <nos...@thanks.invalid> wrote:
>> Jeff Flinn <TriumphSprint2...@hotmail.com> wrote:
>>> 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 above mentioned 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

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

 James Kanze 03-26-2011 01:17 PM

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

On Mar 24, 11:11 am, Angus <anguscom...@gmail.com> wrote:
> On Mar 24, 9:08 am, Juha Nieminen <nos...@thanks.invalid> wrote:
> > Jeff Flinn <TriumphSprint2...@hotmail.com> wrote:
> > > 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.

That's not just a technicality. It's the basic definition of
lower_bound.

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

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.

--
James Kanze

 James Kanze 03-26-2011 01:19 PM

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

On Mar 22, 4:53 pm, cg_chas <cg_c...@hotmail.com> wrote:
> On Tue, 22 Mar 2011 07:52:05 -0700 (PDT), Angus <anguscom...@gmail.com> wrote:

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

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

--
James Kanze

 Michael Doubez 03-26-2011 08:25 PM

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

On 26 mar, 14:27, Leigh Johnston <le...@i42.co.uk> wrote:
> On 26/03/2011 13:17, James Kanze wrote:
>
>
>
>
>
>
>
>
>
> > On Mar 24, 11:11 am, Angus<anguscom...@gmail.com> *wrote:
> >> On Mar 24, 9:08 am, Juha Nieminen<nos...@thanks.invalid> *wrote:
> >>> Jeff Flinn<TriumphSprint2...@hotmail.com> *wrote:
> >>>> 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.

>
> > That's not just a technicality. *It's the basic definition of
> > lower_bound.

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

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

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

 Angus 03-28-2011 04:57 PM

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

On Mar 24, 2:11*pm, Jeff Flinn <TriumphSprint2...@hotmail.com> wrote:
> Angus wrote:
> > On Mar 24, 9:08 am, Juha Nieminen <nos...@thanks.invalid> wrote:
> >> Jeff Flinn <TriumphSprint2...@hotmail.com> wrote:
> >>> 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 above mentioned 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.

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

All times are GMT. The time now is 12:19 PM.