Reputation: 1998
Looked around SO and couldn't find quite what I needed. I am looking for a way to pull the key from the outside map and the value from the inside map for usin in an output statement. If I had one map I know I can pull the data I need from the inside map by using insideMap[key]
. However, the [] operator
does not seem to work in this implementation ((*itr).second)[keyword]
.
map< string, map<string, int> >::const_iterator itr;
for( itr=books.begin(); itr!=books.end(); ++itr)
//code I need here
Upvotes: 1
Views: 1299
Reputation: 1713
You can use std::map::at() instead of operator[]. at() returns the value of a key like operator[], but there is a const version of at().
(itr->second).at(keyword)
The difference between at() and operator[] is that at() does an additional range check and throws an out_of_range exception if it fails.
Upvotes: 1
Reputation: 726579
The problem with []
is const
-ness of your iterator: the []
operator is not const
, so you need to use find(keyword)
instead, and dereference the iterator that it returns:
*(((*itr).second).find(keyword))
You could also switch to non-const iterator and use []
.
Upvotes: 1