What I'm trying to do is to somehow replicate in c++ the structure and functionality of a Perl Hash. For those not familiar with Perl, in such language a key can be used to point not only to a variable but other more complicated structures like arrays or vector or even other hashes. What I have tried so far is to create a vector of size 10 which is going to be my mapped_type
size_t size = 10;
std::vector<int> v1(size);
... Code that fills the v1...
and then create the map with v1 and fill it with values.
std::map<unsigned int, v1> x;
std::map<unsigned int,std::vector<int>>::iterator p=x.find(key);
if(p==m.end()) m[key]=v1;
Later, I plan to loop through all the keys and retrieve the vectors associated with those keys
for (std::map<unsigned int, std::vector<int>>::iterator p=x.begin(); p!=x.end(); ++p) {
...Do something with the p...
}
but of course these two last piece of code does not work at all.
I have successfully created other iterators like
std::map<unsigned int, unsigned int> x;
std::map<unsigned int, unsigned int>::iterator p=x.find(key);
if(p==m.end()) m[key]=1;
for (std::map<unsigned int, unsigned int>::iterator p=x.begin(); p!=x.end(); ++p) {
...Do something with the p...
}
but the mapped type is just a variable containing a single number or character. I want to be able to call and work with a complete vector using map (or any other C++ functionality). Is there a way to do this?