gpt4 book ai didi

c++ - 错误打印 map 元素值

转载 作者:太空宇宙 更新时间:2023-11-04 12:01:34 25 4
gpt4 key购买 nike

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

}

我不确定这条错误信息是什么意思:*错误:在“counter.std::map<_Key, _Tp, _Compare, _Alloc>::operator[] [with _Key = char, _Tp = int, _Compare = std::less, _Alloc = std::分配器 >, std::map<_Key, __Tp, _Compare, _Alloc>::mapped_type = int, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = char]((* &((std::map::key_type)j)))â,属于非类 * *类型 âstd::map::mapped_type {aka int}â*

最佳答案

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

关于c++ - 错误打印 map 元素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13899326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com