最近DjikstraのShortest Pathアルゴリズムの実装をオンラインで見て、次の呼び出しを見つけました。 は、次の呼び出しは、次のようにC#では、大括弧内の2つのパラメータを使用して並べ替えを呼び出すのはどういう意味ですか?
nodes.Sort((x, y) => distances[x] - distances[y]);
完全なコードがどういう意味int型のノードと種類の距離の辞書のリストを、与えられた:私は答えを探し
public List<int> shortest_path(int start, int finish)
{
var previous = new Dictionary<int, int>();
var distances = new Dictionary<int, int>();
var nodes = new List<int>();
List<int> path = null;
foreach (var vertex in vertices)
{
if (vertex.Item1 == start)
{
distances[vertex.Item1] = 0;
}
else
{
distances[vertex.Item1] = int.MaxValue/2;
}
nodes.Add(vertex.Item1);
}
while (nodes.Count != 0)
{
nodes.Sort((x, y) => distances[x] - distances[y]);
var smallest = nodes[0];
nodes.Remove(smallest);
if (smallest == finish)
{
path = new List<int>();
while (previous.ContainsKey(smallest))
{
path.Add(smallest);
smallest = previous[smallest];
}
break;
}
if (distances[smallest] == int.MaxValue)
{
break;
}
foreach (var neighbor in vertices[smallest].Item2)
{
var alt = distances[smallest] + neighbor.Item2;
if (alt < distances[neighbor.Item1])
{
distances[neighbor.Item1] = alt;
previous[neighbor.Item1] = smallest;
}
}
}
return path;
}
たくさんありますが、それが意味することの明確な説明がないようです。 一般的に、LINQでは、Array.Select((x、i)=> ...)の呼び出しは、xが配列内の実際の要素であり、iが配列内の要素xのインデックスであることを意味します。しかし、これは上記のようには見えません。
説明ありがとうございます。
[ラムダ式](https://msdn.microsoft.com/en-us/library/bb397687.aspx) – Steve
https://msdn.microsoft.com/en-us/library/w56d4y5z(v=vs) .110).aspxとlinqではありません – Nkosi
http://stackoverflow.com/questions/4829054/what-does-this-c-sharp-code-with-an-arrow-mean-and-how-is-it -called?rq = 1 –