ConfusedCoder
ConfusedCoder

Reputation: 387

Declaring an unordered_map of an unsigned int and an array (will have different sizes)?

Please ignore the unsigned int I put it there for completeness. Anyways, my question is how do I declare an unordered_map if I want to store arrays of different sizes in it.

I found a related answer on here: Using unordered_map on array of doubles

However, the person said that the size of the array has to be declared as well (from what I understood is that, the array can only have the same size in the unordered_map). Is there a way to do what I'm trying to accomplish?

Update............................................

I'm using "easygl" to draw somethings, one their functions fillpoly requires an array of points in order to draw the polygon. I tried passing a vector, it didn't work. So I decided to use an array instead and it ended badly.

Upvotes: 0

Views: 714

Answers (1)

BitTickler
BitTickler

Reputation: 11947

If you need arrays with varying sizes, you typically use std::vector<>.

So your data type you are looking for (a hash table from unsigned int to std::vector<>, containing, say... t_point... would look like this:

typedef std::unordered_map<unsigned int, std::vector<t_point> > MyHashTable;

If you have a legacy C++/C api which needs a plain old array of your t_point, you can call it with a vector like this:

void Foo( t_point* array, size_t length );
std::vector<t_point> vec;
vec.push_back(t_point()); // just so our array is not empty...
Foo( &vec[0], vec.length() );

Upvotes: 1

Related Questions