473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Finding an element in a container

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?

May 15 '07 #1
4 1625
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...
May 15 '07 #2
On 15 Maj, 16:07, techie <jignati...@googlemail.comwrote:
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

May 15 '07 #3
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
May 15 '07 #4
On May 15, 4:07 pm, techie <jignati...@googlemail.comwrote:
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:ja*********@gmail.com
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

May 16 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Wolfgang Lipp | last post by:
my question is: do we need container elements for repeating elements in data-centric xml documents? or is it for some reason very advisable to introduce containers in xml documents even where not...
0
by: Wolfgang Lipp | last post by:
From: Lipp, Wolfgang Sent: Tuesday, 27?January?2004 13:26 <annotation> the first eleven contributions in this thread started as an off-list email discussion; i have posted them here with...
2
by: FLEB | last post by:
Okay, so I've got this XML: <qa> <questionset> <question name="yourname">What is your name?</question> <question name="yourquest">What is your quest?</question> <question name="favcolor"> What...
3
by: Markus Ernst | last post by:
Hello Reading the follwing document: http://www.w3.org/TR/WD-positioning-970131#In-flow it seems very clear that position:relative should be relative to the parent element. So in the following...
1
by: Doug | last post by:
The html below shows DataList "DiscountList" nested within DataList "EventItemList". DiscountList contains a Label control. I'm trying to find the label, using FindControl, during...
4
by: bienwell | last post by:
Hi all, Data displayed on the datalist control is bound by the column name of the dataset like this : <%# DataBinder.Eval(Container.DataItem, "title")%> Could I use an element of the array...
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
12
by: Howard | last post by:
Is there an easy way to get an iterator (*not* a reverse-iterator) to the last element in a list? The last() function returns the element itself, not an iterator. Thanks, -Howard
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.