In C++, with priority queue I can write:
priority_queue<int, vector<int>, greater<int>> min_pq;
priority_queue<int, vector<int>, less<int>> max_pq;
I wonder is there an equivalent way to do it in C# container? I’m implementing a priority queue, where I need a way to specify this behavior when the user use its constructor. I could use a boolean flag, but it just doesn’t look right to me. Any idea?
public class PriorityQueue<T> where T : IComparable<T> {
private List<T> data;
/// <summary>
///
/// </summary>
/// <param name="item"></param>
public void Push(T item) {
}
/// <summary>
///
/// </summary>
public void Pop() {
}
}
An idiomatic solution in C# is to pass an instance of
IComparer<T>to the constructor of your generic class.