How-to use CSortetedMap<int,int>.CopyTo() to an array of CKeyValuePair<int,int>

 

I use Standard Library Generic Data Collection CSortedMap. To get the values out of the object I like to use CopyTo with an array of CKeyValuePair.


//+------------------------------------------------------------------+
//| Script                                             tryCopyTo.mq5 |
//+------------------------------------------------------------------+
#property version   "1.00"

#include <Generic\SortedMap.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   CSortedMap<int,int>resultmap;

// fill it with some example values
   resultmap.Add(1,  1);
   resultmap.Add(2,  4);
   resultmap.Add(3,  9);
   printf("resultmap.Count() = %d",resultmap.Count());

// CKeyValuePair<int, int>  array to receive the content of resultmap
   CKeyValuePair<int,int>values[];
   ArrayResize(values,resultmap.Count(),0);

// I do not know how to call
// int CopyTo( 
//   CKeyValuePair<TKeyTValue>*&  dst_array[],
//   const int                    dst_start=0  
//  );

// Some calls I tried:
// -------------------

//  resultmap.CopyTo(values); 
// Err: 'CopyTo' - no one of the overloads can be applied to the function call  tryCopyTo.mq5

//   resultmap.CopyTo(&values);
//  Err: int CSortedMap<int,int>::CopyTo(CKeyValuePair<int,int>*&[],const int) SortedMap.mqh

//   resultmap.CopyTo(GetPointer(values));
// Err: 'values' - invalid array access tryCopyTo.mq5

// I tried to cast, but had no success ...
//
//  resultmap.CopyTo((CKeyValuePair<int,int>*&[]) values);
//  resultmap.CopyTo((CKeyValuePair<int,int>*&) values);
//  resultmap.CopyTo((CKeyValuePair<int,int>*) GetPointer(values));
//  resultmap.CopyTo(GetPointer( (CKeyValuePair<int,int>) values));
//  resultmap.CopyTo(GetPointer( (CKeyValuePair<int,int>&) values));
//  resultmap.CopyTo(GetPointer( (CKeyValuePair<int,int>&[]) values));
//  resultmap.CopyTo(GetPointer( (CKeyValuePair<int,int>&[]) *values));
  }
//+------------------------------------------------------------------+
 

From here you start. Then loop over the arrays to build your vector.

https://www.mql5.com/en/docs/standardlibrary/generic/csortedmap/csortedmapcopyto

Documentation on MQL5: Standard Library / Generic Data Collections / CSortedMap / CopyTo
Documentation on MQL5: Standard Library / Generic Data Collections / CSortedMap / CopyTo
  • www.mql5.com
Standard Library / Generic Data Collections / CSortedMap / CopyTo - Reference on algorithmic/automated trading language for MetaTrader 5
 
lippmaje:

From here you start. Then loop over the arrays to build your vector.

https://www.mql5.com/en/docs/standardlibrary/generic/csortedmap/csortedmapcopyto

Not a valid link:

https://www.mql5.com/en/docs/standardlibrary/generic/csortedmap/csortedmapcopyto


I have read the documentation many times.

The question how do I call the first form of CopyTo

"

CopyTo

Copies all key/value pairs from the sorted hash table to the specified arrays, starting at the specified index.

The version that copies a hash table to the array of key/value pairs.

int CopyTo(
   CKeyValuePair<TKeyTValue>*&  dst_array[],     // an array for writing key/value pairs
   const int                    dst_start=0      // the starting index for writing
   );

"

Documentation on MQL5: Standard Library / Generic Data Collections / CSortedMap / CopyTo
Documentation on MQL5: Standard Library / Generic Data Collections / CSortedMap / CopyTo
  • www.mql5.com
Standard Library / Generic Data Collections / CSortedMap / CopyTo - Reference on algorithmic/automated trading language for MetaTrader 5
 
CKeyValuePair<int,int> *values[];
int count=map.CopyTo(values);
Did you try this?
 
lippmaje:
Did you try this?

Thank you, very much. That works.

But with your answer I understand now, that I get an array of pointers. This pointers point to objects "on the heap". I have to delete this objects explicit with delete.

//+------------------------------------------------------------------+
//| Script                                             tryCopyTo.mq5 |
//+------------------------------------------------------------------+
#property version   "1.00"

#include <Generic\SortedMap.mqh>
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   CSortedMap<int,int>resultmap;

// fill it with some example values
   resultmap.Add(1,  1);
   resultmap.Add(2,  4);
   resultmap.Add(3,  9);
   printf("resultmap.Count() = %d",resultmap.Count());

   CKeyValuePair<int,int>*hValues[];
   int count=resultmap.CopyTo(hValues);
   printf("ArraySize: %d",ArraySize(hValues));

   for(int i=0; i<ArraySize(hValues);++i)
      printf("hValues[i].Key()=%d, hValue[i].Value()=%d",hValues[i].Key(),hValues[i].Value());

// ... program goes on, I can work with hValues[i] ...

//---
// ... but it is an array of pointers to dynamically created objects "on the heap".
// All that objects must be explicitlly deleted ...
// ... that is ugly and I will not use form 1 of CopyTo().
   for(int i=0;i<ArraySize(hValues);++i)
      delete hValues[i];
   return;
  }
//+------------------------------------------------------------------+

If the lifetime of the CKeyValuePair does not automatically end at the end of the block of definition, I assume it makes no sence for me to use form 1 of the CopyTo().

So I have to "fall back" to form 2 of the CopyTo(() like the solution in this tread:  https://www.mql5.com/en/forum/304390#comment_10728090

Thanks again, MQL5 is very good, but it is not perfect.

How iterate over CHashMap?
How iterate over CHashMap?
  • 2019.02.21
  • www.mql5.com
I have populated HashMap with keys and values. Now I need to iterate over all pair (key, values). How to do this...