I am trying to create a graph. I am modelling the edges as a std::pair
of the connected vertices and the weights of the edges connecting these vertices. I am inserting this edge through another graph class which holds all these vertices as a std::set
.
The way I am going about it is as follows:
Here's the vertex class :
class Vertex {
public:
friend std::ostream &operator<<(std::ostream &os, const Vertex &v);
Vertex(char c) : ID_(c) {}
bool operator==(const Vertex &rhs) const {
return (this->ID_ == rhs.ID_);
}
bool operator<(const Vertex &rhs) const {
return (this->ID_ < rhs.ID_);
}
char ID_;
std::vector<std::pair<Vertex, int>> neighbors;
};
Here's the graph class which holds the vertices:
class Graph {
public:
friend std::ostream &operator<<(std::ostream &os, const Graph &g);
Graph();
Graph(char c[]);
void insertVertex(char c);
void insertEdge(char ID1, char ID2, int weight);
protected:
std::set<Vertex> vertices_;
};
And here's my edge insertion method:
void Graph::insertEdge(char c1, char c2, int w) {
auto it1 = vertices_.find(c1);
auto it2 = vertices_.find(c2);
auto p1 = std::make_pair(*it1, w);
if(it1 != vertices_.end())
it1->neighbors.push_back(p1); // Fails here
}
Does the ->
operator return the object that the iterator points to, or does it return something like a const reference to it?