Rasho
Rasho

Reputation: 31

iterating over c++ map<int, list<int>>

I am getting this error while 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 map is correctly populated 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 advices?

Upvotes: 3

Views: 5335

Answers (3)

chabeee
chabeee

Reputation: 305

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;                   
}

Upvotes: 1

Happy Green Kid Naps
Happy Green Kid Naps

Reputation: 1673

You could do away with the inner for loop with -

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

Which also means that you could get rid off the declaration of ii.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258568

You have a syntax error here:

 list<int>iterator::

should be

 list<int>::iterator

Also,

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

should be

(*it).second.begin()

Upvotes: 4

Related Questions