1

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).

Pat Myron
  • 4,437
  • 2
  • 20
  • 39
  • 3
    Most probably `it` is a const_iterator. So you either need a non-const iterator or you must assign your vector to a const pointer. – 101010 Dec 01 '15 at 16:33
  • 3
    `route3` is a const member function => `adjacencyList` is const within it => `find` returns a const_iterator. – Mat Dec 01 '15 at 16:35
  • 2
    Is `const vector* v = &(it->second);` acceptable in your application? – timrau Dec 01 '15 at 16:39

1 Answers1

4

You have four options:

1) Make the pointer to the vector const

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

2) Make your adjacencyList mutable

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;

3) Copy the vector

This might introduce a heavier overhead and changes done to the vector are NOT stored in your map.

vector<edge> v = (it->second);

4) Make the function non-const

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;
    }
Cybran
  • 2,203
  • 2
  • 17
  • 34