gpt4 book ai didi

c++ - 访问 map 元素

转载 作者:行者123 更新时间:2023-11-28 06:36:26 24 4
gpt4 key购买 nike

<分区>

我尝试按如下方式返回我的 std::map 的特定对象:

const Vertex& Graph::getVertex(const std::pair<size_t, size_t>& pos) const // -> compile error
{
return this->_vertices[std::get<0>(pos)][std::get<1>(pos)];
}

map :

std::map<size_t, std::vector<Vertex>> _vertices;

但是,如果我让 getVertex 函数为常量,则会出现编译错误。这是否意味着以这种方式访问​​我的 map 元素实际上修改了它的实例?有没有更好的方法来访问我的 map 元素?

错误:

error: passing ‘const std::map<unsigned int, std::vector<Vertex> >’ as ‘this’ argument of ‘std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::o\
perator[](const key_type&) [with _Key = unsigned int; _Tp = std::vector<Vertex>; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, std::vector<Vertex> > >; std::map\
<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::vector<Vertex>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = unsigned int]’ discards qualifiers [-fpermissive]

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