So I am trying to set an unordered_map as value for another unordered_map. Now I have the problem that I am not able to put values into the second unordered_map. My code looks like this.
std::string postCode;
std::string[] postCodeSource = new std::string{1111,2222,3333,4444};
std::unordered_map<std::string,unordered_map<int,Foo*>*> data;
int k1=1234;//can be any number
for(int i = 0; i < 4; i++){
postCode = postCodeSource[i]
if(data[postCode]==NULL) {
data[postCode]=new std::unordered_map<int,Foo*>();
}
data[postCode]->at(int(k1)) = new Foo(postCodeSource[i]);
}
classes:
class Foo{
public:
Foo();
Foo(std::string postCode);
std::string firstName,Customername,postCode;
}
Foo(std::string postCode); is a simple copy constructor.
Now when I reach data[lastPlz.getString()]->at(int(k1.customer)) = new Foo(&k1); I get an out of range exception from unordered_map which makes sense since there is no object at k1.customer yet!
Before I changed the code I created and filled a pointer looking like that.
std::unordered_map<int,Foo*> kdnrMap
kdnrMap[k1.customer] = new Foo(&k1);
and later on, I added kdnrMap to data. That won't work anymore the way I intended since this method would need a completely filled kdnrMap before I can add it to data which I can't do anymore.
So I am looking for help to get kdnrMap filled I've tried pretty much anything I could think of for now.
Foo,Foo2; a minimal example that actually compiles. We could probably tease your problem out of your text description, but that is needlessly complicated and ambiguous. Just write some simple code that demonstrates your problem.operator[]is quite expensive, you better use it only once unless it is really necessary to use it multiple times. You as your map seems to have ownership of the data you better use smart pointers.std::unordered_mapstores its data?