set/map question

C

cbdeja

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?
 
V

Victor Bazarov

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
 
K

Kai-Uwe Bux

(e-mail address removed) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top