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

    }

我不确定此错误消息的含义: *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)))â,属于非类* *type âstd::map::mapped_type {aka int}â*

4

1 回答 1

1

counter 是一个映射而不是一个映射数组,一个从 begin() 到 end() 的 for 循环就足够了,将您的 for 循环更改为下面的代码。

    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;
    }
于 2012-12-16T07:11:09.387 回答