重命名地图迭代器的第一个和第二个 [英] Renaming first and second of a map iterator

查看:151
本文介绍了重命名地图迭代器的第一个和第二个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法重命名地图迭代器的第一个和第二个访问器函数。我知道他们有这些名字,因为代表关键和价值的底层对,但是我希望迭代器更易读。我认为这可能是使用迭代器适配器,但我不知道如何实现它。



请注意,我不能使用boost。



我的意思是:

  map<顶点,边缘>的adjacency_list; 
for(map< Vertex,Edge> :: iterator it = adjacency_list.begin();
it!= adjacency_list.end();
++ it)
{
顶点v = it-> first;
//而不是我想要它 - >顶点
}


解决方案

如果你只是关心可读性,你可以这样做:

  typedef map< Vertex,Edge>邻接表; 
struct adjacency
{
adjacency(AdjacencyList :: iterator& it)
:vertex(it-> first),edge(it-> second){}
顶点&顶点;
Edge&边缘;
};

然后:

 顶点v =邻接(it).vertex; 


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 key and value, but I'd like the iterators to be a little more readable. I think this might be possible using an iterator adaptor, but I'm not sure how to implement it.

Please note that I can't use boost.

Example of what I mean:

map<Vertex, Edge> adjacency_list;
for(map<Vertex, Edge>::iterator it = adjacency_list.begin();
    it != adjacency_list.end();
    ++it)
{
    Vertex v = it->first;
    //instead I would like to have it->vertex
}

解决方案

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;

这篇关于重命名地图迭代器的第一个和第二个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆