Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Finding an element in a container

Reply
Thread Tools

Finding an element in a container

 
 
techie
Guest
Posts: n/a
 
      05-15-2007
I want to find an element in a sequence or map by comparing its value
(not the key). I can search for an element with a particular value in
a sequence or map by iterating through the elements one by one using
an iterator for the container. I could alternatively use the the
std::find or std::find_if algorithms to find an element in a sequence
or map respectively. For example, to find an element in a map I could
write a function object to check the value of an element:
http://www.josuttis.com/libbook/cont/mapfind.cpp.html

I guess internally the std::find or std::find_if internally use
iterators to search for an element. Is there any advantage of using
the algorithms over for loops that use iterators. What is the best
most efficient method? Using algorithms or using iterators?

 
Reply With Quote
 
 
 
 
Fei Liu
Guest
Posts: n/a
 
      05-15-2007
techie wrote:
> I want to find an element in a sequence or map by comparing its value
> (not the key). I can search for an element with a particular value in
> a sequence or map by iterating through the elements one by one using
> an iterator for the container. I could alternatively use the the
> std::find or std::find_if algorithms to find an element in a sequence
> or map respectively. For example, to find an element in a map I could
> write a function object to check the value of an element:
> http://www.josuttis.com/libbook/cont/mapfind.cpp.html
>
> I guess internally the std::find or std::find_if internally use
> iterators to search for an element. Is there any advantage of using
> the algorithms over for loops that use iterators. What is the best
> most efficient method? Using algorithms or using iterators?
>

algorithm, no need to reinvent the the wheel...
 
Reply With Quote
 
 
 
 
peter koch
Guest
Posts: n/a
 
      05-15-2007
On 15 Maj, 16:07, techie <(E-Mail Removed)> wrote:
> I want to find an element in a sequence or map by comparing its value
> (not the key).


[snip]

> I guess internally the std::find or std::find_if internally use
> iterators to search for an element. Is there any advantage of using
> the algorithms over for loops that use iterators. What is the best
> most efficient method? Using algorithms or using iterators?


The most efficient method would often be std::find_if, the exception
possibly being if lots of local state needs to be copied. The reason
is that knowledge of the container can often lead to more efficient
algorithms. But do not expect anything better than a (small) constant
factor.

/Peter

 
Reply With Quote
 
Zeppe
Guest
Posts: n/a
 
      05-15-2007
techie wrote:

> I guess internally the std::find or std::find_if internally use
> iterators to search for an element. Is there any advantage of using
> the algorithms over for loops that use iterators. What is the best
> most efficient method? Using algorithms or using iterators?
>


The advantage is the clearness. Usually when you have to search for an
element a good std::find_if is very crear and nice, even if you have to
write two lines of code for the comparison function. Otherwise, if it
happens that a for with the iterators is just a faster and cleanest way
to perform the search in some particular case for whatever reason, just
go that way, your choice.

Regards,

Zeppe
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-16-2007
On May 15, 4:07 pm, techie <(E-Mail Removed)> wrote:
> I want to find an element in a sequence or map by comparing its value
> (not the key). I can search for an element with a particular value in
> a sequence or map by iterating through the elements one by one using
> an iterator for the container. I could alternatively use the the
> std::find or std::find_if algorithms to find an element in a sequence
> or map respectively. For example, to find an element in a map I could
> write a function object to check the value of an element:http://www.josuttis.com/libbook/cont/mapfind.cpp.html


> I guess internally the std::find or std::find_if internally use
> iterators to search for an element. Is there any advantage of using
> the algorithms over for loops that use iterators. What is the best
> most efficient method? Using algorithms or using iterators?


There is no real difference in efficiency. In both cases, you
are doing a linear search. The advantage of find or find_if is
that the name of the function says this immediately, up front.
The disadvantage is that if you don't already have a
predicate handy for the job, you have to write one---if the
logic of the predicate is connected to the logic of the calling
function, this means separating the logic into two separate
places, since the predicate cannot be a local class. (Normally,
this will very rarely be the case, unless you write functions
which are too complex to begin with. But it's still not always
the most readable solution to replace a three token in the loop
predicate with a full class definition outside of the function.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Copy elements from one STL container to another STL container Marko.Cain.23@gmail.com C++ 4 02-16-2006 05:03 PM
std::transform container => std::abs(container) Steven T. Hatton C++ 4 12-05-2004 07:10 AM
STL: container's values setup by another container Maitre Bart C++ 2 02-11-2004 12:11 AM
std::container::iterator vs std::container::pointer Vivi Orunitia C++ 11 02-04-2004 08:09 AM



Advertisments