gpt4 book ai didi

c++ - 为什么 BGL 函数的参数用点而不是逗号分隔?

转载 作者:太空宇宙 更新时间:2023-11-04 16:07:42 24 4
gpt4 key购买 nike

我正在阅读某人的代码。这是来自 boost 图形库的函数。这是原始函数定义。

 void dijkstra_shortest_paths
(const Graph& g,
typename graph_traits<Graph>::vertex_descriptor s,
PredecessorMap predecessor, DistanceMap distance, WeightMap weight,
VertexIndexMap index_map,
CompareFunction compare, CombineFunction combine, DistInf inf, DistZero zero,
DijkstraVisitor vis, ColorMap color = default)

这是我从某人那里挑选的一段代码。它有效,但我只是不明白为什么他在 predecessor_map weight_mapdistance_map 之间使用点而不是逗号?他传入函数的参数有多少?

dijkstra_shortest_paths(graph, source_vertex,
predecessor_map(&predecessors[0])
.weight_map(get(&Edge::cost, graph))
.distance_map(&distances[0]));

最佳答案

documentation解释发生了什么:

Many of the Boost.Graph algorithms have a long list of parameters, most of which have default values. This causes several problems.

[...]

A better solution is provided by bgl_named_params. This class allows users to provide parameters is any order, and matches arguments to parameters based on parameter names.

[...]

Each of the arguments is passed to a function whose name indicates which parameter the argument is for. Each of the named parameters is separated by a period, not a comma.

[...]

Typically the user never needs to deal with the bgl_named_params class directly, since there are functions like boost::weight_map that create an instance of bgl_named_params.

另见 What is the “Named Parameter Idiom”? .

关于c++ - 为什么 BGL 函数的参数用点而不是逗号分隔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32725735/

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