gpt4 book ai didi

c++ - < 遍历 C++ 映射时缺少运算符

转载 作者:搜寻专家 更新时间:2023-10-31 00:05:58 25 4
gpt4 key购买 nike

下面的代码不想编译。请参阅随附的错误消息。

代码:

#include <map>
#include <vector>
#include <iostream>

class MapHolder {
public:
std::map<std::vector<std::string>,MapHolder> m_map;

void walk_through_map() {
std::map<std::vector<std::string>,MapHolder>::iterator it;
for(it = m_map.begin(); it < m_map.end(); ++it) {
it->second.print();
}
}

void print() { std::cout << "hey" << std::endl; }
};

int
main(int argc, char *argv[])
{
MapHolder m;
m.walk_through_map();
}

错误:

$ g++ test.cc -O test
test.cc: In member function 'void MapHolder::walk_through_map()':
test.cc:12: error: no match for 'operator<' in 'it < ((MapHolder*)this)->MapHolder::m_map.std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Tp = MapHolder, _Compare = std::less<std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, _Alloc = std::allocator<std::pair<const std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, MapHolder> >]()'

我以前多次使用这种类型的映射和迭代过程。这里有什么问题?怎么解决。

(代码看起来毫无意义,但这是一个缩减的示例,应该仍然可以工作)

最佳答案

在迭代器比较中使用 != 而不是 <。

关于c++ - < 遍历 C++ 映射时缺少运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1042392/

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