1
votes
map<char, int> counter;

//some code...

map<char, int>::iterator iter;

for (i = 0; i<26; i++) 
    {
    for (iter = counter[i].begin(); iter != counter[i].end(); iter++)  //error occurs
         {
         cout << (*iter).first << " - " << (*iter).second << endl;
         }

    }

I'm not sure what this error message means: *error: request for member âbeginâ in âcounter.std::map<_Key, _Tp , _Compare, _Alloc>::operator[] [with _Key = char, _Tp = int, _Compare = std::less, _Alloc = std::allocator >, std::map<_Key, __Tp, _Compare, _Alloc>::mapped_type = int, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]((* &((std::map::key_type)j)))â, which is of non-class* *type âstd::map::mapped_type {aka int}â*

1

1 Answers

1
votes

counter is a map not an array of map, one for loop from begin() to end() is enough, change your for loop to below code.

    map<char, int> counter;

   //some code...

    for (map<char, int>::iterator iter = counter.begin(); iter != counter[i].end(); ++iter)
    {
       cout << (*iter).first << " - " << (*iter).second << endl;
    }