Talk About Network

Google





Programming > C - C++ Learning > Re: Storing Arr...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 9 Topic 4066 of 4400
Post > Topic >>

Re: Storing Arrays in Maps (Or, alternatively, convert hex to dec)

by Jerry Coffin <jcoffin@[EMAIL PROTECTED] > Mar 17, 2008 at 12:08 AM

In article <0h4Cj.11565$wM2.6305@[EMAIL PROTECTED]
>, hal@[EMAIL PROTECTED]
 says...
> I need to store 2 - 4 values that I have in hex in a map.  I've tried
> storing an int[] in a map and can't make it work.  As of now, the only
way
> I can think of is to store it as a string, like "0x01 0xDF".  But then
when
> I get it out of the map, I don't see how to convert it to one int for
each
> specified hex number.
> 
> If I could either store an array of 2-4 integers in a map or convert the
hex
> strings to integers, it would work.  I've also experimented, on the
output
> end, with sscanf and sprintf, but I don't seem to have them working
right. 
> (Plus I have to convert the string to a char[].)

You can't store an array in a container because anything you store in a 
container has to be copyable and assignable, but an array is neither. An 
obvious alternative is to create a class of your own that does sup****t 
those, something like this:

class small_array { 
	int data[4];
	unsigned char max:2;

public:
	int &operator[](size_t index) { 
		assert(index<4);
		return data[index&0x3];
	}

	small_array &operator=(small_array const &other) {
		max = other.max;
		std::copy(other.data, other.data+other.max+1, data);
		return *this;
	}

	small_array(small_array const &other) : max(other.max) {
		std::copy(other.data, other.data+other.max+1, data);
	}
	
	small_array(int *values, size_t num_values = 0) 
		: max(num_values-1) 
	{
		assert(num_values <= 4);
		std::copy(values, values+num_values, data);
	}

	bool push_back(int const &value) { 
		if (max>=3)
			return false;
		data[++max] = value;
	}
};

Though this doesn't sup****t the full interface for std::vector, it 
sounds like it's probably close enough for your purposes -- and if not, 
you have the full source code to work with in adding things like 
pop_back, begin(), end(), etc.

-- 
    Later,
    Jerry.

The universe is a figment of its own imagination.
 




 9 Posts in Topic:
Storing Arrays in Maps (Or, alternatively, convert hex to dec)
Hal Vaughan <hal@[EMAI  2008-03-13 06:59:08 
Re: Storing Arrays in Maps (Or, alternatively, convert hex to de
"Mike Wahler" &  2008-03-13 06:10:31 
Re: Storing Arrays in Maps (Or, alternatively, convert hex to de
Hal Vaughan <hal@[EMAI  2008-03-13 13:31:50 
Re: alternatively, convert hex to dec
Hal Vaughan <hal@[EMAI  2008-03-13 13:58:41 
Re: alternatively, convert hex to dec
Hal Vaughan <hal@[EMAI  2008-03-13 14:13:34 
Re: Storing Arrays in Maps (Or, alternatively, convert hex to de
Jerry Coffin <jcoffin@  2008-03-17 00:08:33 
Re: Storing Arrays in Maps (Or, alternatively, convert hex to de
Jerry Coffin <jcoffin@  2008-03-17 00:13:49 
Re: Storing Arrays in Maps (Or, alternatively, convert hex to de
Jeff Schwab <jeff@[EMA  2008-03-17 10:17:47 
Re: Storing Arrays in Maps (Or, alternatively, convert hex to de
Jerry Coffin <jcoffin@  2008-03-18 07:17:53 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Wed Jan 7 13:48:35 PST 2009.