1

I am working on a project on C++ and I am new to the language.

I am on a function which has to do something ranging from hex numbers 0x00 to 0xFF, and I have been told that I could do that with a map.

Problem is, so far in the examples I've seen I have just seen it used for just one entry while I need something to be done depending on specific ranges.

Is there a way I can do that in map or do I need to use something else to do the desired function?

Julio Garcia
  • 393
  • 2
  • 7
  • 22

1 Answers1

1

If understand correct you store some values in a map, with key a value from 0x00 for 0xFF? Possible solution could be:

typedef std::map<unsigned char, int> values;
void print( values::const_iterator begin, values::const_iterator end );

values v;
// print range [0x01,0x20]
print( v.lower_bound( 0x01 ), v.upper_bound( 0x20 ) );
// print range [0x10,0x40[
print( v.lower_bound( 0x10 ), v.lower_bound( 0x40 ) );
// print range ]0x20,0x50[
print( v.upper_bound( 0x20 ), v.lower_bound( 0x50 ) );

Is that what you need?

Slava
  • 43,454
  • 1
  • 47
  • 90
  • +1 As far as I understood the question, equal operation for certain ranges is needed. One could indeed store that 'action' within each upper bound. I guess, the OP is up to implement something like a state machine, thus my suggestion to try without a map. – Sam Feb 23 '13 at 21:28
  • Well, kinda. The intended action is if v falls within some range, give the expected instruction the SIC/XE machine would do given those two bytes. – Julio Garcia Feb 24 '13 at 01:39