类边缘列表C

class Edge List c++

本文关键字:列表 边缘      更新时间:2023-10-16

我正在尝试在C 上进行边缘列表类。当我编译代码时,我会遇到很多错误,但是我听不懂,我错了?

struct vertex
{
   double x,y;
};
class EdgeList
{
private:
    std::map<vertex, vector<vertex>> graph_container;
public:
    void add_vertex(const vertex& v) { //add a vertex to the map
        graph_container.insert(pair<vertex,vector<vertex> >(v, vector<vertex>()));
    }
    //*
    void add_edge(const vertex& v, const vertex& u) { //look up vertex in map 
            auto it = graph_container.find(v);
            if (it != graph_container.end())
                *it.push_back(u);
    }    
    //*/
 };

第一个错误是

64-w64-mingw32/include/c++/iostream:39,
                 from EarTr.cpp:1:
C:/mingw-w64/x86_64-5.2.0-win32-seh-rt_v4-rev0/mingw64/x86_64-w64-mingw32/includ
e/c++/bits/stl_function.h:387:20: note:   'const vertex' is not derived from 'co
nst std::__cxx11::basic_string<_CharT, _Traits, _Alloc>'
       { return __x < __y; }

我已经在struct

中添加了2个功能
bool operator==(const vertex &o)const {
    return x == o.x && y == o.y;
}
bool operator<(const vertex &o) const{
    return x < o.x || (x == o.x && y < o.y);
}

所以,现在地图可以与我的数据类型一起使用。我也使用

it->second.push_back(u);

而不是

*it.push_back(u);