I have a map<string, list<int> > which I want to iterate over the list and print out each number. I keep getting a compile error talking about conversion between a const_iterator and iterator. What am I doing wrong with this?
for (map<string, list<int> >::iterator it = words.begin(); it != words.end(); it++)
{
cout << it->first << ":";
for (list<int>::iterator lit = it->second.begin(); lit != it->second.end(); lit++)
cout << " " << intToStr(*lit);
cout << "\n";
}
error: conversion from
‘std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::list<int, std::allocator<int> > > >’
to non-scalar type
‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::list<int, std::allocator<int> > > >’
requested|
should be
Either your
mapisconst, or your map is a member of aclassand you’re calling this code in aconstfunction, which also makes yourmapconst. Either way, you can’t have a non-constoperator on aconstcontainer.EDIT: Am I the only one who prefers to use explicit types over
auto?