I am implementing a biconnected-components algorithm. There are several read-only methods written by other in the graph representation that returns a copy. How do I change these to return the actual reference instead of a copy? I am confused. Is there a good source to learn these stuff? Here are some examples:
getting neighbors vertex.
list<Vertex> Vertex::getNeighbors() const { return this->_neighbors; }
getting edges
vector<Edge> Graph::getEdges() const { unordered_set<string> distinctEdges; vector<Edge> edges; for (auto vertex = _verticies.begin(); vertex != _verticies.end(); vertex++) { auto neighbors = vertex->getNeighbors(); for (auto neighbor = neighbors.begin(); neighbor != neighbors.end(); neighbor++) { Edge e(*vertex, *neighbor); if (distinctEdges.count(e.str()) > 0) { continue; } distinctEdges.insert(e.str()); edges.push_back(e); } } return edges; }
Update: I need the reference so that I can update it later with something like this:
void Graph::updateVertex(size_t id, size_t level) {
auto u = this->_findVertex(Vertex(id));
if (u == this->_verticies.end()) {
return;
}
u->level = level;
}
If it's a copy, it won't update the original. Thanks.