473,386 Members | 1,668 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,386 software developers and data experts.

please help me on lower_bound && upper_bound

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 called boards__[n], and I have
got the array sorted by board.h ascendantly.

I have a pair<int,intt=pair<int,int>(x0,h0).

What I want to do is: to find the last board int boards__[n] which
satisfies this:
its h< h0,
and its x1<=t.first
and its x2>=t.second.

So, can I do it by upper_bound or lower_bound?
Which one do I have to use?
I tried for a moment and I couldn't find a way. Will you please help me
out?
Thanks!

Dec 26 '06 #1
3 2468

co*******@gmail.com wrote:
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 called boards__[n], and I have
got the array sorted by board.h ascendantly.

I have a pair<int,intt=pair<int,int>(x0,h0).

What I want to do is: to find the last board int boards__[n] which
satisfies this:
its h< h0,
and its x1<=t.first
and its x2>=t.second.

So, can I do it by upper_bound or lower_bound?
Which one do I have to use?
I tried for a moment and I couldn't find a way. Will you please help me
out?
Thanks!
Take a look at std::map. It inserts std::pair key/elements using a
programmeable order and already provides lower_bound and upper_bound
member functions.
http://www.sgi.com/tech/stl/Map.html
You also have std::multimap if you need duplicate pairs.

Dec 26 '06 #2
thank you,
but I'm not dealing with pair stuff.
I wrap the two numbers in a pair is just to pass is as a single param
into the algorithm.
It's also OK to put the two numbers in an arbitrary structure.

Salt_Peter wrote:
co*******@gmail.com wrote:
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 called boards__[n], and I have
got the array sorted by board.h ascendantly.

I have a pair<int,intt=pair<int,int>(x0,h0).

What I want to do is: to find the last board int boards__[n] which
satisfies this:
its h< h0,
and its x1<=t.first
and its x2>=t.second.

So, can I do it by upper_bound or lower_bound?
Which one do I have to use?
I tried for a moment and I couldn't find a way. Will you please help me
out?
Thanks!

Take a look at std::map. It inserts std::pair key/elements using a
programmeable order and already provides lower_bound and upper_bound
member functions.
http://www.sgi.com/tech/stl/Map.html
You also have std::multimap if you need duplicate pairs.
Dec 26 '06 #3
So, can I do it by upper_bound or lower_bound?
You can find range of elements satisfying first condition (h < h0)
using upper_bound(), but then you will need to iterate through this
range to check all conditions.

Though, you can check all elements of sorted array and stop scanning
when condition h < h0 becomes false.

Dec 26 '06 #4

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

Similar topics

0
by: Erik Arner | last post by:
Hi, let's say I have a std::map<std::string,int> and I want to search the map for all keys that start with "foo". The regexp equivalent is to search for "foo*", or perhaps "^foo*". At present...
7
by: lmanchur. | last post by:
Hi, I am implementing a version of the C++ STL map class for a school assignment and have a question... what is the difference between const_iterator and just iterator??... If I already have an...
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...
2
by: jesper | last post by:
In frustration I made this code using equal_range. It does not behave in the way I expected however. It finds the correct breakpoint in the set but finds it as the first iterator. why? I would...
3
by: asdf | last post by:
Can you create std::set::upper_bound function just using std::set::lower_bound ?
3
by: Diego Martins | last post by:
Andrew Koenig wrote:
12
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b)...
3
by: Henrik Goldman | last post by:
Hello, Assume we have a std::map<time_t, int>. Now I would like to find all values (second) which fits in (within the time range of first) between first of April 1st. and end of July. Notice...
4
by: qlin88 | last post by:
Hi, In STL multi-map, the lower_bound, upper_bound,equal_range all return an iterator. Now ifone wants to iterate from an upper bound towards a lower bound, what would be the best way to do it?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.