Reputation: 18242
I'm doing the following:
//d = 3701
//h = 3702
//c = 8
map<int, map<int,int> > table;
table[d][h] = c;
table iter = table.begin();
while(iter != table.end())
{
cout << "d: " << iter->first << "h: " << iter->second[0] << "c: " << iter->second[1] << endl;
iter++;
}
But my output is showing: d: 3701 h: 0 c: 0
Upvotes: 0
Views: 117
Reputation: 29724
The only element on this map is with key = 3701
and this is a map which has only 1
element with key 3702
, equal to 8
. If you try to access any other element like
iter->second[0] // inner map has no key = 0, so key = 0 with default value
// assigned to it is created
iter->second[1] // inner map has no key = 1, so key = 1 with default value
// assigned to it is created
std::map
will insert default value to it.
If you want do avoid insertion of default values when checking if they are present you shoud use find()
function to compare with map.end()
iterator.
while( iter != table.end())
{
cout << "d: " << iter->first;
if ( (iter->second).find(0) != (iter->second).end())
cout << "key 0, value: " << iter->second[0] << endl;
++iter;
}
Upvotes: 2