如何将 C++ std::p ush_heap 转换为 C#?
how can I convert C++ std::push_heap to C#?
我有一些这样的C++代码:
std::vector<PathNode> nodes;
PathNode node;
....
nodes.push_back(node);
std::push_heap(nodes.begin(), nodes.end());
现在我将把这些C++代码重写为 C#,如何实现 std::p ush_heap 方法?
你需要为 PathNode 编写自己的比较函数
struct greaters{
bool operator()(const PathNode& a,const PathNode& b) const{
return a.x>b.x;
}
};
std::vector<PathNode> nodes;
PathNode node;
nodes.push_back(node);
std::make_heap(nodes.begin(), nodes.end(), greaters()));
std::push_heap(nodes.begin(), nodes.end(), greaters()));
对于 C#
您可以将SortedList
或SortedDictionary
与自定义密钥一起使用。如果您使用了具有引用相等性的类型,但可以根据您关心的值进行比较,那么这可能会起作用。
.NET 中的堆类
我使用了一个通用列表<>,排序时插入:
static List<int> nodes = new List<int>();
static void Main()
{
PushNode(3);
PushNode(12);
PushNode(4);
PushNode(6);
PushNode(5);
PushNode(8);
PushNode(2);
PushNode(9);
PushNode(1);
foreach (int value in nodes)
{
Console.WriteLine(value);
}
}
static void PushNode(int value)
{
for (int i = 0; i < nodes.Count; i++)
{
if (nodes[i] > value)
{
nodes.Insert(i, value);
return;
}
}
nodes.Add(value);
return;
}
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 是否可以从int转换为enum类类型
- 了解 GLM- openGL 中的相机转换
- 将无符号char*转换为std::istream*C++