#include <Generic/HashMap.mqh>
class CTrade {
public:
double entryPrice;
double exitPrice;
CTrade(double entry, double exit) : entryPrice(entry), exitPrice(exit) {}
~CTrade() {Print("CTrade destructor called");}
};
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
test();
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
}
void test()
{
// 创建哈希表(键为string,值为CTrade指针)
CHashMap<string, CTrade*> objMap;
// 添加类实例
objMap.Add("Trade1", new CTrade(1.1000, 1.1050));
objMap.Add("Trade2", new CTrade(1.2000, 1.2050));
// 访问对象
CTrade* tradePtr = NULL;
if(objMap.TryGetValue("Trade1", tradePtr) && tradePtr != NULL) {
Print("Trade1 entry: ", tradePtr.entryPrice);
}
// 注意:需要手动管理内存!
CleanupHashMap(objMap);
}
void CleanupHashMap(CHashMap<string, CTrade*>& map) {
for(int i = 0; i < map.Count(); i++) {
CTrade* ptr = map.GetValueAt(i);
if(CheckPointer(ptr) == POINTER_DYNAMIC) {
delete ptr;
}
}
map.Clear();
}
mql5 类来实现哈希映射
最新推荐文章于 2025-12-09 19:50:25 发布
492

被折叠的 条评论
为什么被折叠?



