By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

set/map question

P: n/a
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
range from 0 to 0xffffffff.The quantity of these numbers will vary
from none to (in the unlikely worst case) 0xffffffff.

The random numbers are fed to my program in no particular order but I
must store them so that I can later iterate through them from the
smallest value to the largest value and vice versa. This seems to
suggest a STL "set" would be the most appropriate to use. (I may at
some point wish to add an associated value for each of these numbers,
and presumably I'd then use "map" instead of "set", but let's ignore
this for now).

Having stored all of these values in a set(?), I will now receive an
occasional random number generated from user interaction and I need
to be able to get the closest existing value in my set. Actually I
need to be able to choose between getting the next LOWEST or next
HIGHEST value.

Can any of the STL templates offer this functionality? If set (or map)
does not allow me to get the CLOSEST values, is there a simple and
efficient way to extend set or map to do this?

Feb 21 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
cb****@my-deja.com wrote:
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
range from 0 to 0xffffffff.The quantity of these numbers will vary
from none to (in the unlikely worst case) 0xffffffff.

The random numbers are fed to my program in no particular order but I
must store them so that I can later iterate through them from the
smallest value to the largest value and vice versa. This seems to
suggest a STL "set" would be the most appropriate to use. (I may at
some point wish to add an associated value for each of these numbers,
and presumably I'd then use "map" instead of "set", but let's ignore
this for now).

Having stored all of these values in a set(?), I will now receive an
occasional random number generated from user interaction and I need
to be able to get the closest existing value in my set. Actually I
need to be able to choose between getting the next LOWEST or next
HIGHEST value.

Can any of the STL templates offer this functionality? If set (or map)
does not allow me to get the CLOSEST values, is there a simple and
efficient way to extend set or map to do this?
If you need to also record the quantity, you're better off with a map.
Make your random number the key, and make the quantity the value.

What book on the Standard Library are you reading that doesn't explain
and gives no examples of using the standard containers? RTFM to see
what member functions are available in 'std::map' and see if you can
use any of them (hint: lower_bound)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 21 '07 #2

P: n/a
cb****@my-deja.com wrote:

[snip:story about how values for a set arise]
Having stored all of these values in a set(?), I will now receive an
occasional random number generated from user interaction and I need
to be able to get the closest existing value in my set. Actually I
need to be able to choose between getting the next LOWEST or next
HIGHEST value.

Can any of the STL templates offer this functionality? If set (or map)
does not allow me to get the CLOSEST values, is there a simple and
efficient way to extend set or map to do this?
Have a look at the std::set<member

lower_bound( value )

returns the smallest element not less than value

The closest value is either the one returned by lower_bound() or the
preceding one.

Best

Kai-Uwe Bux
Feb 21 '07 #3

P: n/a
That's great. Thanks both.

Feb 22 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.