C++ 使用无序映射值初始化指向向量的指针时出错
我有一个名为street_map的类,它包含一个带有int键和C++ 使用无序映射值初始化指向向量的指针时出错,c++,pointers,dictionary,constants,object-composition,C++,Pointers,Dictionary,Constants,Object Composition,我有一个名为street_map的类,它包含一个带有int键和vector类型值的映射。在其中一个方法中,我试图初始化指向向量的指针以获取其内容 class street_map { public: explicit street_map (const std::string &filename); bool geocode(const std::string &address, int &u, int &v, float &pos) co
vector
类型值的映射。在其中一个方法中,我试图初始化指向向量的指针以获取其内容
class street_map {
public:
explicit street_map (const std::string &filename);
bool geocode(const std::string &address, int &u, int &v, float &pos) const;
bool route3(int source, int target, float &distance) const {
auto it = adjacencyList.find(source);
vector<edge>* v = &(it->second);
return true;
}
private:
unordered_map<side , vector<segment>> map;
unordered_map<int, vector<edge>> adjacencyList;
};
我想知道这是否是因为const关键字,如果是因为const关键字,如何修复它(我应该保留const关键字,但如果没有其他解决方案,我想我可以去掉它)。您有四个选项:
1) 使指针指向向量常量
您将无法修改向量
bool route3(int source, int target, float &distance) const {
auto it = adjacencyList.find(source);
const vector<edge>* v = &(it->second);
return true;
}
您有四种选择:
1) 使指针指向向量常量
您将无法修改向量
bool route3(int source, int target, float &distance) const {
auto it = adjacencyList.find(source);
const vector<edge>* v = &(it->second);
return true;
}
它很可能是一个常量迭代器。因此,您要么需要一个非常量迭代器,要么必须将向量分配给常量指针。route3
是常量成员函数=>adjacencyList
is const in it=>find
返回常量迭代器。isconst vector*v=&(it->second)代码>在您的应用程序中可接受?很可能它是一个常量迭代器。因此,您要么需要一个非常量迭代器,要么必须将向量分配给常量指针。route3
是常量成员函数=>adjacencyList
is const in it=>find
返回常量迭代器。isconst vector*v=&(it->second)代码>在您的应用程序中是否可接受?
private:
unordered_map<side , vector<segment>> map;
mutable unordered_map<int, vector<edge>> adjacencyList;
vector<edge> v = (it->second);
bool route3(int source, int target, float &distance) {
auto it = adjacencyList.find(source);
vector<edge>* v = &(it->second);
return true;
}