lundi 5 septembre 2016

How do I return actual reference in C++

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:

  1. getting neighbors vertex.

    list<Vertex> Vertex::getNeighbors() const {
    return this->_neighbors;
    }
    
    
  2. 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;
    }
    
    

Aucun commentaire:

Enregistrer un commentaire