gpt4 book ai didi

c++ - 重命名 map 迭代器的第一个和第二个

转载 作者:IT老高 更新时间:2023-10-28 21:59:54 28 4
gpt4 key购买 nike

有没有办法重命名映射迭代器的第一个和第二个访问器函数。我知道它们有这些名称是因为代表键和值的底层对,但我希望迭代器更具可读性。我认为这可能使用迭代器适配器,但我不确定如何实现它。

请注意,我不能使用 boost。

我的意思的例子:

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
}

最佳答案

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

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

然后:

Vertex v = adjacency(it).vertex;

关于c++ - 重命名 map 迭代器的第一个和第二个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1500064/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com