Renaming first and second of a map iterator

后端 未结 8 1722
北恋
北恋 2020-12-14 02:41

Is there any way to rename the first and second accessor functions of a map iterator. I understand they have these names because of the underlying pair which represents the

相关标签:
8条回答
  • 2020-12-14 03:12

    You can't rename the members, but you can have some functions to help.

    inline Vertex& vertex(map<Vertex, Edge>::iterator& it) {return it->first;}
    inline Edge& edge(map<Vertex, Edge>::iterator& it) {return it->second;}
    

    Then, instead of it->vertex like you want, you can do vertex(it)

    0 讨论(0)
  • 2020-12-14 03:13

    If you're just concerned about readability you could do something like this:

    typedef map<Vertex, Edge> AdjacencyList;
    struct adjacency
    {
        adjacency(AdjacencyList::iterator& it) 
          : vertex(it->first), edge(it->second) {}
        Vertex& vertex;
        Edge& edge;
    };
    

    And then:

    Vertex v = adjacency(it).vertex;
    
    0 讨论(0)
提交回复
热议问题