I have a class called street_map that contains a map with int keys and values of type vector<edge>
. In one of the methods, I am trying to initialize a pointer to the vector<edge>
value to get its contents.
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;
};
The line vector<edge>* v = &(it->second);
gives the error:
Cannot initialize a variable of type 'vector<edge> *' with an rvalue of type 'const std::__1::vector<edge, std::__1::allocator<edge> > *'
Here is the edge class:
class edge {
int node;
string street;
float length;
int startingNode;
public:
edge(int startingNode, int node, string street, float length) {
startingNode = startingNode;
node = node;
street = street;
length = length;
}
};
I am wondering if this is because of the const keyword and how to fix this if it because of the const keyword (I am supposed to keep the const keyword, but I suppose I can get rid of it if there is no other solution).
You have four options:
You will not be able to modify the vector.
bool route3(int source, int target, float &distance) const {
auto it = adjacencyList.find(source);
const vector<edge>* v = &(it->second);
return true;
}
mutable means accessible as non-const from a const function - this can be risky if you don't pay attention to your access design!
private:
unordered_map<side , vector<segment>> map;
mutable unordered_map<int, vector<edge>> adjacencyList;
This might introduce a heavier overhead and changes done to the vector are NOT stored in your map.
vector<edge> v = (it->second);
Be aware that this way the function cannot be called in a context where street_map
is const!
bool route3(int source, int target, float &distance) {
auto it = adjacencyList.find(source);
vector<edge>* v = &(it->second);
return true;
}