Features of the mql5 language, subtleties and tricks - page 183

 
Denis Kirichenko:

Colleagues, please help me make a macro, if at all possible.

I need to dynamically declare a two-dimensional array. And the second dimension must also be changed. This is something like this loop:

Of course, the compiler will balk at this:

I would like to see a macro like the following:

Instead of a line:

Thank you.

I use a structure array and everything works, including copying

https://www.mql5.com/ru/forum/85652/page46#comment_15990662

The only thing is that you have to write the names of the fields when you access them, but I'm fine with that

 
If there is an urgent need to reset static variables/arrays (including const) in EAs, a re-login is sufficient.
 
fxsaber:
If there is an urgent need to reset static variables/arrays (including const) in EAs, a re-login is sufficient.

If you need to reset ALL static variables (including those that you yourself declared as constants), you need to change the project architecture). IMHO of course.

 

Can you tell me what to specify instead of ???? to make it work? Thank you

template<typename T, ?????>
void sortArray(T &_array[], ?????) {
   T array;
//---Sort Signals by Time
   for(int i = 0; i < ArraySize(_array); i++) {
      array = _array[i];
      for(int a = 1; a <= i; a++) {
         
            if(_array[i].????? < _array[a - 1].?????) {
               for(int b = i; b >= a; b--) {
                  _array[b] = _array[b - 1];
               }
               _array[a - 1] = array;
               break;
            }
 

   return;
}

You need to tell the function the name of the class member to check the array

 
Georgiy Liashchenko:

Can you tell me what to specify instead of ???? to make it work? Thank you

I need a way to tell the function the name of the class member by which to check the array

This option:

struct MyStruct
{
   double a;
   double b;
};

typedef bool (*FuncLess)( const MyStruct&, const MyStruct& );


bool LessA( const MyStruct& struct1, const MyStruct& struct2 )
{
   return struct1.a < struct2.a;
}

bool LessB( const MyStruct& struct1, const MyStruct& struct2 )
{
   return struct1.b < struct2.b;
}

template< typename T, typename FuncType >
void sortArray( T& _array[], FuncType func )
{
   T array;
//---Sort Signals by Time
   for( int i = 0; i < ArraySize( _array ); i++ )
   {
      array = _array[i];
      for( int a = 1; a <= i; a++ )
      {
         if( func( _array[i], _array[a - 1] ) )
         {
            for( int b = i; b >= a; b-- )
            {
               _array[b] = _array[b - 1];
            }
            _array[a - 1] = array;
            break;
         }
      }
   }
   return;
}

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   MyStruct structArray[25];
   
   for( int i = 0; i < 25; i++ )
   {
      structArray[i].a = rand();
      structArray[i].b = rand();
   }
   
   sortArray< MyStruct, FuncLess >( structArray, LessA );
   
   for( int i = 0; i < 25; i++ )
   {
      PrintFormat( "1: structArray[%i] = %f %f", i, structArray[i].a, structArray[i].b );
   }
   
   sortArray< MyStruct, FuncLess >( structArray, LessB );
   
   for( int i = 0; i < 25; i++ )
   {
      PrintFormat( "2: structArray[%i] = %f %f", i, structArray[i].a, structArray[i].b );
   }
}
 
Koldun Zloy:

Such an option:

Thank you, that's an interesting option. Not quite what I'm looking for though. If I understood the code correctly, you pass a sorting function to the right hand side, but you already write the structure members in it. It means, if I need to sort by another member or another object with another member's name, I will have to create a separate function for each case. Unfortunately, it is not very optimistic. But it may be) I continue my search.

There is something similar in js, but this function with members is written inside the sort line and looks more like a simple parameter assignment, which doesn't lead to code cluttering. I am looking for a universal variant, but I'm lacking knowledge. Maybe it is done via some sort of pointers or mapping, please help.

Документация по MQL5: Основы языка / Типы данных / Структуры, классы и интерфейсы
Документация по MQL5: Основы языка / Типы данных / Структуры, классы и интерфейсы
  • www.mql5.com
Структура является набором элементов произвольного типа (кроме типа void). Таким образом, структура объединяет логически связанные данные разных типов. Объявление структуры Имя структуры нельзя использовать в качестве идентификатора (имени переменной или функции). Следует иметь ввиду, что в MQL5 элементы структуры следуют непосредственно друг...
 
Georgiy Liashchenko:

Can you tell me what to specify instead of ???? to make it work? Thank you

You need to tell the function the name of the class member to be used to check the array.

A universal solution has been posted on the forum. You will have to search for it.

 
fxsaber:

A one-size-fits-all solution was posted on the forum. You need to look it up.

Do you happen to remember which section at least? It's like looking for a needle in a haystack.)
 
Georgiy Liashchenko:
Do you happen to remember which section at least? It's like looking for a needle in a haystack.)

All I remember is that it was two months ago.

 
Georgiy Liashchenko:

Thanks, it's an interesting option. Not quite what I'm looking for though. If I understood the code correctly, you are passing a sorting function to the right hand side, but you are already prescribing the structure members in it. It means, if I need to sort by another member or another object with another member's name, I will have to create a separate function for each case. Unfortunately, it is not very optimistic. But it may be) I continue my search.

There is something similar in js, but the function with members is written inside the sort line and looks more like a simple parameter assignment, which doesn't lead to code cluttering. I'm looking for a universal variant, but I'm lacking knowledge. Perhaps this is done through some sort of pointers or mapping, please help.

Actually this is optimal. And it allows you to set more complex sorting conditions.

For example:

struct MyStruct
{
   int A;
   int B;
   int C;
};

bool Less( const MyStruct& struct1, const MyStruct& struct2 )
{
   if( struct1.A != struct2.A ){
      return struct1.A < struct2.A;
   }
   if( struct1.B != struct2.B ){
      return struct1.B < struct2.B;
   }
   return struct1.C < struct2.C;
}

And there are no other solutions anyway.

Reason: