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

Lower number/bound

Hi,

If i have an array like {1,4, 10, 15 , 20 , 30 } of size n , now if i
want to search for number

25 , i should get 20 , if i search for number 11 i hould get 10 , if i
search for 4 i should get 4, if i search for a number and it doesn't
exist i should get the lower number between which it lies. All numbers
are bound to lie between 1 and n.

Can you tell me a easy way to do it.

Does STL have anything for this.

Regards
Nik

May 19 '06 #1
4 2122

<te********@gmail.com> wrote in message
news:11*********************@j73g2000cwa.googlegro ups.com...
Hi,

If i have an array like {1,4, 10, 15 , 20 , 30 } of size n , now if i
want to search for number

25 , i should get 20 , if i search for number 11 i hould get 10 , if i
search for 4 i should get 4, if i search for a number and it doesn't
exist i should get the lower number between which it lies. All numbers
are bound to lie between 1 and n.

Can you tell me a easy way to do it.

Does STL have anything for this.


It has something close to it: <algorithm> has std::upper_bound
and std::lower_bound for sequence containers, and std::map,
std::multimap, std::set, and std::multiset types have member
functions 'upper_bound' and 'lower_bound'. But these functions
return iterators, not values. The map and set member functions
return iterators to existing elements, while the 'standalone'
functions used with sequence containers return iterators to
where the searched value would be inserted with existing ordering
preserved (default ordering with operator<, or specified with
a predicate. I'm not sure I stated that exactly accuratedly,
so look this stuff up to be sure.

A great book about the C++ standard library is:
www.josuttis.com/libbook

-Mike
May 19 '06 #2
This can be very easily accomplished if you make sure the array is
sorted in ascending order, as it is in your example.

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

void main()
{
typedef std::vector<int> MyArray;

int tmp[] = {1,4, 10, 15 , 20 , 30 };

MyArray arr(tmp, tmp + sizeof(tmp) / sizeof(tmp[0]));

// sort in asecnding order, in case it's not sorted
std::sort(arr.begin(), arr.end());

// find the first value that is less or equal to 25.. the search is
done from the end of container to its begining
MyArray::reverse_iterator it = std::find_if(arr.rbegin(), arr.rend(),
std::bind2nd(std::less_equal<int>(), 25));

if (it!=arr.rend())
{
// here is the value of the found item
std::cout << *it << std::endl;
}
}

lower_bound and upper_bound won't do in this case. But hey, it's so
easy to use predicates in STL algorithms :-)

May 19 '06 #3
Shimon Shvartsbroit wrote:
This can be very easily accomplished if you make sure the array is
sorted in ascending order, as it is in your example.

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

void main()
{
typedef std::vector<int> MyArray;

int tmp[] = {1,4, 10, 15 , 20 , 30 };

MyArray arr(tmp, tmp + sizeof(tmp) / sizeof(tmp[0]));

// sort in asecnding order, in case it's not sorted
std::sort(arr.begin(), arr.end());

// find the first value that is less or equal to 25.. the search is
done from the end of container to its begining
MyArray::reverse_iterator it = std::find_if(arr.rbegin(), arr.rend(),
std::bind2nd(std::less_equal<int>(), 25));

if (it!=arr.rend())
{
// here is the value of the found item
std::cout << *it << std::endl;
}
}

lower_bound and upper_bound won't do in this case. But hey, it's so
easy to use predicates in STL algorithms :-)


While the question "Why aren't you using vectors?" is valid, it is worth
noting, in my opinion, that pointers themselves can act as iterators and
be passed directly to the standard algorithms.

int tmp[] = {1, 4, 10, 15, 20, 30};
unsigned int size = 6; // size of the array

std::sort(tmp, tmp + size);
int *val = std::upper_bound(tmp, tmp + size, 25);

Jack Saalweachter
May 19 '06 #4
<te********@gmail.com> wrote in message
news:11*********************@j73g2000cwa.googlegro ups.com...
Hi,

If i have an array like {1,4, 10, 15 , 20 , 30 } of size n , now if i
want to search for number

25 , i should get 20 , if i search for number 11 i hould get 10 , if i
search for 4 i should get 4, if i search for a number and it doesn't
exist i should get the lower number between which it lies. All numbers
are bound to lie between 1 and n.
Possibly a pedantic observation, but in the above array, doesn't n = 6? Only
1 and 4 lie between 1 and n in the above array. (Actually, 1 may not either,
it depends how we define "between".) With the bounds given, it's difficult
to know what to return for the following example (say):

[2,2,3,3] (then all the array elements lie between 1 and 4, however you
define "between")

Searching for 1 is expected to give result what?

Regards,
Stu
Can you tell me a easy way to do it.

Does STL have anything for this.

Regards
Nik

May 19 '06 #5

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

Similar topics

19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
3
by: tdmailbox | last post by:
What is the proper way to set up a validation rule to only allow numbers? I want to allow both 1024 and 1024.25
8
by: ben | last post by:
hello, i'm trying to answer a "prove an upper bound on the number of machine instructions required to process M connections on N objects" exercise from an algorithms book. i don't understand a...
4
by: Mark Gibson | last post by:
Hi, I've been playing about with array's, and found the concat operator '||' quite useful, apart from the fact that prepending an element places it in a lower subscript. Is there a way of...
4
by: temper3243 | last post by:
i, If i have an array like {1,4, 10, 15 , 20 , 30 } of size n , now if i want to search for number 25 , i should get 20 , if i search for number 11 i hould get 10 , if i search for 4 i...
2
by: Gianluca | last post by:
If you create an array using Array.CreateInstance() and use a lower bound > 0, you apparently get an array of the wrong type. int lenghts = new int {1}; int lowerBounds = new int {1}; string ar...
17
by: rhitz1218 | last post by:
Hi, I'm trying to create a function that will sort a number's digits from highest to lowest. For example 1000 - will become 0001 or 1234 to 4321
7
by: Caffiend | last post by:
Well, I've been picking at learning python, got tired of reading, and figured I'd try to replicate my prime number generator I wrote (with much TSDN forum help) in C++. I've hit a stumbling block......
25
by: Daniel Kraft | last post by:
Hi, I do need to implement something similar to C++'s std::bitset in C; for this, I use an array of int's to get together any desired number of bits, possibly larger than 32/64 or anything like...
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: 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: 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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.