This topic is now archived and is closed to further replies.

STL, arrays, general crappiness

This topic is 5013 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to have a hash map, whose key is a character, and whose value is an array of characters (which I will actually use for small integers). 

By the way, I will hard-code the values for each array associated with each character, and all arrays will not be the same size.

I cant come up with the syntax to get it to work.

I tried declaring the hash like this:
hash_map<char, char*> theHash;

and just storing pointers to the array of characters.

but then I have to do something like this:

char *temp;
temp = new char[I need to know the size here!];
temp={4, 2, 6, 9, 23, 1, 5}; // syntax like this doesnt work!

I also tried declaring the hash like this:
hash_map<char, char[]> theHash;

,and actually storig the array in the hash, but I couldnt get that to work either.

Ideally, this is what I would like to be able to do: (but of course this syntax doesnt work) theHash['A'] = {3,4,2,6,22,9,21}; theHash['B'] = {1,4,6}; theHash['C'] = {7,5,3,2,1,5,6,7,4,2,1,54,3}; .. theHash['Z'] = {3,4,6,7,5,}; But I cant get anything I try to work. Also, I would like the final solution to be compact like that; I dont want to have to mess with 20 or so lines of code for each letter of the alphabet. I'll bake a caek for whoever can figure this mess out! [edited by - AndreTheGiant on March 25, 2004 11:38:33 PM]

Share this post

Link to post
Share on other sites
Would two lines of code per letter work for you?

stdext::hash_map<char, std::vector<char> > the_map;

char a_arr[] = {3,4,2,6,22,9,21};
the_map[''A''] = std::vector<char>(a_arr, a_arr + sizeof(a_arr));
char b_arr[] = {1,4,6};
the_map[''B''] = std::vector<char>(b_arr, b_arr + sizeof(b_arr));
char c_arr[] = {7,5,3,2,1,5,6,7,4,2,1,54,3};
the_map[''C''] = std::vector<char>(c_arr, c_arr + sizeof(c_arr));
// ...

char z_arr[] = {3,4,6,7,5,};
the_map[''Z''] = std::vector<char>(z_arr, z_arr + sizeof(z_arr));

Share this post

Link to post
Share on other sites