c++ - 通过引用返回 vector

标签 c++ c++11

我有以下问题。

typedef std::pair<VertexT,CostT>  LocalEdgeT;
typedef std::vector<LocalEdgeT>   NeighborT;
typedef std::size_t                 VertexT;
typedef double                      CostT;

virtual const NeighborT& getNeighbors( VertexT v) const override   
    {
        std::vector<LocalEdgeT> neighbors;
        //here I'm adding elements, not important for the question
        return neighbors;
    }

我不能让函数在没有引用的情况下返回 NeighborT,因为我必须使用我大学给我的函数,出于某种原因需要引用。

但是当我在 main 中使用以下调用返回它时:

std::vector<NeighborT> test = Object.getNeighbors(arg);

它给出了一个段错误,可能是因为我正在返回对局部变量的引用。知道我该如何修复它,它仍然通过引用返回 vector 并且它与我在 main 方法中的函数调用一起工作吗? 此外,我必须使用 c++11 标准进行编译。

一些附加信息:

我只是输入了“对象”,因为我认为它对问题来说并不那么重要。在我的例子中,函数 getNeighbors 是一个 Graph 类的成员,它有一定数量的顶点和一个 vector ,用于从顶点 a 到顶点 b 的所有边。函数 getNeighbors 现在应该找到给定 Vertex v 的所有邻居。不建议为类中的每个顶点拥有自己的 vector (从我的角度来看)。 我确实有一张 map ,我在其中保存了所有边缘,并用它的双“CostT”去那个边缘。 这是完整的类(class)。

typedef std::size_t                 VertexT;
typedef std::pair<VertexT,VertexT>  EdgeT;
typedef double                      CostT;
    class DistanceGraph
    {
  public:
    typedef std::pair<VertexT,CostT>  LocalEdgeT;
    typedef std::vector<LocalEdgeT>   NeighborT;

  protected:

    std::size_t vertexCount;

  public:
    DistanceGraph( int num_verts= 0)
      : vertexCount(num_verts) {}

    virtual ~DistanceGraph() {}

    std::size_t numVertices() const { return vertexCount; }


    virtual const NeighborT& getNeighbors( VertexT v) const = 0;


    virtual CostT estimatedCost( VertexT from, VertexT to) const = 0;

    virtual CostT cost( VertexT from, VertexT to) const = 0;
};

class CoordinateGraph : public DistanceGraph {
public:

    std::map<  EdgeT, CostT  > allEdges;
    std::vector < std::pair < double, double > > geometricPosition; 

    void setNumVertices( size_t);

    friend std::istream& operator >> (std::istream& in,CoordinateGraph& g);

    virtual const NeighborT& getNeighbors( VertexT v) const override   
    {
        std::vector<LocalEdgeT> neighbors;
        for(size_t i = 0; i < (*this).numVertices(); i++)
        {
            EdgeT edge = std::make_pair(v,i);
            if((*this).allEdges.find(edge) != (*this).allEdges.end())
            {
                neighbors.push_back( std::make_pair(i,(*this).allEdges.find(edge) -> second));
            }
        }
        return neighbors;
    }

    virtual CostT cost( VertexT from, VertexT to) const override
    {
        EdgeT edge = std::make_pair(from,to);
        if((*this).allEdges.find(edge) != (*this).allEdges.end()) return (*this).allEdges.find(edge) -> second;
        else return 10000000;
    }
};

再次澄清一下,我不能让函数 getNeighbors 返回 NeighborT。 我看到的一种解决方案是使每个顶点的邻居成为存储在 vector 中的类成员。 当我调用上述函数时,上面的代码显然在局部变量的返回方面存在问题。

最佳答案

因为你似乎有通过引用返回邻居 vector 的硬性要求,你基本上别无选择,必须将它存储在你的类中。

只需制作一个 std::map<VertexT, NeighborT>类成员来存储它们。

调用getNeighbors检查现有条目并返回现有条目或创建新条目并将其添加到 map 。

关于c++ - 通过引用返回 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50886837/

相关文章:

c++ - OpenGL GLFW,三角形未渲染

c++ - 嵌套 `static_for` 导致编译器错误

c++ - 具有 OR 搜索功能的多键容器

c++ - C位运算题

c++ - 将此指针分配给指针的右值引用

c++ - std::vector<type> 的类型要求

c++ - c++14 中的静态 thread_local 内存异步信号安全吗?

c++ - ISampleGrabber::BufferCB 到 IplImage;在 OpenCV 中显示显示乱码图像 - C++

c++ - 使用 GCC 4.7 从初始化程序列表初始化 unique_ptrs 的容器失败

c++ - 如何将 std::tuple 类型与 boost::mpl 算法一起使用?