473,405 Members | 2,272 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,405 software developers and data experts.

Question about lower_bound

On page 181 of Effective STL, it said 'It 's knowning when equal_range
is a better way to search than lower_bound, knowing when lower bound is
preferable to find..."

My question is understand what situation is lower_bound is better than
find?
I think both algorithm stops when it find the first item which matches
the condition.
So they should be the same, right?

Thank you for any help.

Mar 17 '06 #1
3 2188

Al************@gmail.com wrote:
On page 181 of Effective STL, it said 'It 's knowning when equal_range
is a better way to search than lower_bound, knowing when lower bound is
preferable to find..."

My question is understand what situation is lower_bound is better than
find?
I think both algorithm stops when it find the first item which matches
the condition.
So they should be the same, right?


No. std::lower_bound requires that the range be sorted. std::find
does not. std::lower_bound searches in logarithmic time. std::find
searches in linear time. std::lower_bound returns an iterator to where
the object would be inserted in the sorted range, even if the object
doesn't exist in the range. std::find simply returns the end of the
range if it can't find the object.

I haven't read it in a while, but I imagine Effective STL explains all
this.

Best regards,

Tom

Mar 17 '06 #2
Thomas Tutone wrote:
Al************@gmail.com wrote:
On page 181 of Effective STL, it said 'It 's knowning when equal_range
is a better way to search than lower_bound, knowing when lower bound is
preferable to find..."

My question is understand what situation is lower_bound is better than
find?
I think both algorithm stops when it find the first item which matches
the condition.
So they should be the same, right?


No. std::lower_bound requires that the range be sorted. std::find
does not. std::lower_bound searches in logarithmic time. std::find
searches in linear time. std::lower_bound returns an iterator to where
the object would be inserted in the sorted range, even if the object
doesn't exist in the range. std::find simply returns the end of the
range if it can't find the object.

I haven't read it in a while, but I imagine Effective STL explains all
this.


If nothing else, it's summarised in a table on the back of the front cover.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 17 '06 #3
JE

Thomas Tutone wrote:
<snip>
std::lower_bound searches in logarithmic time.


to clarify, logarithmic with random access iterators, linear otherwise.
Best regards, JE

Mar 17 '06 #4

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

Similar topics

4
by: Alex Vinokur | last post by:
============================= Windows 2000 MinGW 2.0.0.-2 GNU gcc/g++ version 3.2 ============================= I have some question concerning the lower_bound algorithm. ========= C++ code...
1
by: Jim West | last post by:
The following code does what I want it to do on two different compilers (g++ and Intel icc under Linux), but I'm not sure if it is undefined and just happens to work: class A { public: int...
3
by: Jaap | last post by:
Hi all, I'm looking for functionality very similar to std::map::lower_bound: I'd like to find the first element whose key is equal to or smaller than the key I pass. In a map filled like...
8
by: olanglois | last post by:
Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const key_type& k) or calling (option #2)
3
by: Diego Martins | last post by:
Andrew Koenig wrote:
3
by: could.net | last post by:
I have a class board, like this: struct board { int x1, x2, h; int time1, time2; void calculate_time(); void assign(int x1__,int x2__,int h__); }; And I have n boards stored in an array...
3
by: cbdeja | last post by:
I'm fairly new to using STL and I'm trying to decide what I should be using, and whether set or map can provide what I need. I need to store a random set of numbers which can have values in the...
1
by: phdscholar80 | last post by:
I am using the following code: #include <vector> #include <algorithm> class A { }; bool comparator( A * b, const char * a )
0
by: mathieu | last post by:
hi there, I do not understand why the following piece of code is not working for me. I have a set of cstrings, where I am trying to find one 'UT'. If I search from "AE" to "OB or OW" everything...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.