Iterate over a C ++ map <int, list <int>>

I get this error when trying to iterate over a map and its list member:

error: 'template<class _Category, class _Tp, class _Distance, class _Pointer, class _Reference> struct std::iterator' used without template parameters
invalid use of qualified-name '<declaration error>::ii'
error: 'ii' was not declared in this scope
error: 'struct std::_Rb_tree_iterator<std::pair<const int, std::list<int, std::allocator<int> > > >' has no member named 'second'
error: 'struct std::_Rb_tree_iterator<std::pair<const int, std::list<int, std::allocator<int> > > >' has no member named 'second'

This is the code (the card is correctly filled in before):

map<int, list<int> >::iterator it; 
list<int>iterator:: ii;

for(it=this->mapName.begin(); it!=this->mapName.end(); ++it){ 

    cout<<(*it).first<<" { ";
    for(ii = (*it).second().begin(); ii!=(*it).second.end(); ++ii){
        cout<<(*ii)<<" ";
    }
    cout << " }" << endl;                   
}

Any tips?

+3
source share
3 answers

You have a syntax error here:

 list<int>iterator::

it should be

 list<int>::iterator

Besides,

(*it).second().begin()

it should be

(*it).second.begin()
+4
source

You can end the inner loop with -

    std::copy( it->second.begin(), it->second.end(),
               std::ostream_iterator<int>(std::cout, " ") );

It also means that you can get rid of the ad ii.

+1
source
map<int, list<int> >::iterator it; 
list<int>::iterator ii;

for(it=this->mapName.begin(); it!=this->mapName.end(); ++it){ 

    cout<<(*it).first<<" { ";
    for(ii = (*it).second.begin(); ii!=(*it).second.end(); ++ii){
        cout<<(*ii)<<" ";
    }
    cout << " }" << endl;                   
}
+1
source

All Articles