文章 "MQL5 编程基础:数组" - 页 2

 
德米特里-费多谢耶夫 的这篇文章解释得非常清楚,非常有帮助。
 
如何从函数中返回一个数组? 我找到了一个变通方法,但显然一定有 "正面 "的方法))。
 
thejobber:
如何从函数中返回数组? 目前我找到了一种变通方法,但显然一定有 "正面 "返回的方法))。

没有 "正面 "的方法...除了指向数组的指针。举个例子

CArrayDouble *Function(void);

不是 "正面",而是相当标准的方法,就像本节中的这样:

void Function(double &_arr_out[]);
一般来说,返回的数组是一个参数引用...
 

是的,谢谢。我试着立即

return *x; 出于习惯,就像在 C++ 中一样 ))

而不是

returnGetPointer(x)

想通了 ))

 
MetaQuotes Software Corp.:

新文章MQL5 编程基础:数组 已发布:

作者: Dmitry FedoseevDmitry Fedoseev

1 个问题:

#define  SIZE_X 3;

(...)

int ArrayName[SIZE_X];

编译器禁止这种构造。为什么?

 
非常感谢您的评论
 
#import "dll.dll"
  double qwerty(double &q[]);
#import

我正在制作一个 dll,我想向它传递一个数组指针。

但我不知道该怎么做。

有问题。


我需要一些类似于 C++ 的函数


double qwerty(double *q);

double *q;

q= new 双人[100];

qwerty(q);


如何在 MQL5 中实现...

 
在 mql 中,无论有什么数组,它就是什么,你不能对它做任何事情。应该不会有任何问题。错误出在你的 dll 附近。也许你应该在 dll 中传递数组的大小
 

感谢您的代码,它为我节省了一些编写时间。我曾尝试使用 MQL5 数组对象(double、int 等),结果很沮丧。我来到你的帖子,发现你的代码可以调整数组的大小,这真是太棒了,谢谢。我修改了你的代码,以适应任何数据类型。只是不要尝试对对象使用 Contains (Search method) 方法,因为它可能不起作用,因为对象可能是引用,我还没测试过。我试图在 MQL5 中重新创建 C#,因此缩写相似 :)


上帝保佑



// + ----------------------------------------------- ------------------- + 


// |
CDynamicArray.mqh | CDynamicArray.mqh 


// |
Integer | 


// |
https://login.mql5.com/ru/users/Integer | 


// + ----------------------------------------------- ------------------- + 


#property 

copyright  

"Integer" 


#property 

link  

"https://login.mql5.com/en/users/Integer" 


// + - -------------------------------------------------- --------------- + 


// |
| 


// + ----------------------------------------------- ------------------- + 


template 

< 

typename 

 T> 


class 

 CDynamicArray 


  { 


private 

 : 
   

int 

                m_ChunkSize;     

// 块大小 
   

int 

                m_ReservedSize; 

// 数组的实际大小 
   

int 

                m_Size;         

// 数组中活动元素的数量 


public 

 : 


   T Element [];       

// 数组本体。
它位于公共部分、 
                                     

// 以便我们在必要时直接使用它 
   

// + --------------------------- --------------------------------------- + 
   

|
构造函数 
   

// + ----------------------------------------------- ------------------- + 
   

void 

 CDynamicArray ( 

int 

 ChunkSize = 

1024 

 ) 


     { 


      m_Size = 

0 

 ;                             

// 活动元素的数量 


       m_ChunkSize = ChunkSize;               

// 块大小 


       m_ReservedSize = ChunkSize;             

// 数组的实际大小 
      

ArrayResize 

 (Element, m_ReservedSize); 

// 准备数组 


      } 
   

// + ----------------------------------------- ------------------------- + 
   

|
在数组末尾添加元素的函数 
   

// + ----------------------------------------------- ------------------- + 
   

void 

 Add (T Value) 


     { 


      m_Size ++; 

// 增加活动元素的数量 
      

if 

 (m_Size> m_ReservedSize) 


        { 

// 所需的数字小于实际数组大小 


          m_ReservedSize + = m_ChunkSize; 

// 计算新数组的大小 
         

ArrayResize 

 (Element, m_ReservedSize); 

// 增加数组的实际大小 


         } 


      Element [m_Size- 

1 

 ] = Value; 

// 添加数值 


      } 
     
     

void 

 Set ( 

int 

 index, T Value) 


     { 


      m_Size ++; 

// 增加活动元素的数量 
      

if 

 (m_Size <index) 


        { 
         

return 

 ; 


        } 


      Element [index] = Value; 

// 添加数值 


      } 
   

// + ----------------------------------------- ------------------------- + 
   

// |
获取数组中活动元素个数的函数 
   

// + ----------------------------------------------- ------------------- + 
   

int 

 Count () 


     { 
      

return 

 (m_Size); 


     } 
   


   T 

operator 

 [] ( 

const  

int 

 index) 

const 

 { 

return 

 Element [index]; 
} 
   
   

bool 

Contains (T itemToFind) 


   { 
      

for 

 ( 

int 

 i = 

0 

 ; i <Count (); i ++) 


        { 
            

if 

 (Element [i] == itemToFind) 


            { 
               

return  

true 

 ; 


            } 


        } 
        
      

return  

false 

 ; 


   } 
   
   

int 

 IndexOf (T itemToFind) 


   { 
      

for 

 ( 

int 

 i = 

0 

 ; i <Count (); i ++) 


        { 
            

if 

 (Element [i] == itemToFind) 


            { 
               

return 

 i; 


            } 


        } 
        
      

return 

 - 

1 

 ; 


   } 


  }; 


// + ----------------------------------------------- ------------------- + 

然后您可以像这样声明它们:

CDynamicArray 

< 

int 

> 

  *Tickets; 


CDynamicArray 

< 

bool 

> 

 *FixedSLUsed; 


CDynamicArray 

< 

bool 

> 

 *PrevBarSLUsed; 

并像这样创建它们:

                        Tickets = 

new 

 CDynamicArray< 

int 

>(); 


                        FixedSLUsed = 

new 

 CDynamicArray< 

bool 

>(); 


                        PrevBarSLUsed = 

new 

 CDynamicArray< 

bool 

>(); 

并在代码中像使用普通类一样使用它们:



int 

 ticket = 

PositionGetInteger 

 ( 

POSITION_TICKET 

 ); 


int 

 index; 


if 

 (!Tickets.Contains(ticket)) // If Ticket Object does NOT contains ticket then we go into the if 


{ 


        Tickets.Add(ticket); 


        FixedSLUsed.Add( 

false 

); 


        PrevBarSLUsed.Add( 

false 

); 


} 
      


index = Tickets.IndexOf(ticket);   
 

好的,让我们试试英文版的 MQL。


感谢您发布的代码。它节省了我的时间。我试过使用 MQL 数组,但它们让人困惑。但后来我发现了你的代码,它节省了我研究数组以及如何使数组动态增长/增加的时间。非常感谢。

我希望我能回报你!下面的代码适用于所有数据类型。它也适用于对象,但 Contains(搜索)方法可能不起作用。我只在 double、int 和 bool 类型上进行了测试。字符串可能也会有问题,代码可能需要扩展。


//+------------------------------------------------------------------+
//|CDynamicArray.mqh
//|整数
//| https://login.mql5.com/ru/users/Integer ||.
//+------------------------------------------------------------------+
#property copyright "Integer"
#property link "https://login.mql5.com/ru/users/Integer"
//+------------------------------------------------------------------+
//||
//+------------------------------------------------------------------+
template <typename T>
class CDynamicArray
  {
private:
   int               m_ChunkSize;    // 块大小
   int               m_ReservedSize; // 数组的实际大小
   int               m_Size;         // 数组中活动元素的数量
public:
   T                 Element[];      // 数组本体。它位于公共部分、 
                                     // 以便我们在必要时直接使用它
   //+------------------------------------------------------------------+
   //| 构造函数|
   //+------------------------------------------------------------------+
   void CDynamicArray(int ChunkSize=1024)
     {
      m_Size=0;                            // 活动元素的数量
      m_ChunkSize=ChunkSize;               // 块大小
      m_ReservedSize=ChunkSize;            // 数组的实际大小
      ArrayResize(Element,m_ReservedSize); // 准备数组
     }
   //+------------------------------------------------------------------+
   // 在数组末尾添加元素的函数
   //+------------------------------------------------------------------+
   void Add(T Value)
     {
      m_Size++; // 增加活动元素的数量
      if(m_Size>m_ReservedSize)
        { // 所需数字大于实际数组大小
         m_ReservedSize+=m_ChunkSize; // 计算新数组的大小
         ArrayResize(Element,m_ReservedSize); // 增加数组的实际大小
        }
      Element[m_Size-1]=Value; // 添加数值
     }
     
     void Set(int index, T Value)
     {
      m_Size++; // 增加活动元素的数量
      if(m_Size<index)
        { 
         return;
        }
      Element[index]=Value; // 添加数值
     }
   //+------------------------------------------------------------------+
   //} 获取数组中活动元素数量的函数
   //+------------------------------------------------------------------+
   int Count()
     {
      return(m_Size);
     }
   
   T operator[](const int index) const { return Element[index]; }
   
   bool Contains(T itemToFind)
   {
      for(int i=0;i<Count();i++)
        {
            if(Element[i] == itemToFind)
            {
               return true;
            }
        }
        
      return false;
   }
   
   int IndexOf(T itemToFind)
   {
      for(int i=0;i<Count();i++)
        {
            if(Element[i] == itemToFind)
            {
               return i;
            }
        }
        
      return -1;
   }
  };
//+------------------------------------------------------------------+


然后,您可以像这样为所有需要的类型声明它:


//我在我的类中是这样声明的
   CDynamicArray<int> *Tickets;
   CDynamicArray<bool> *FixedSLUsed;
   CDynamicArray<bool> *PrevBarSLUsed;


// 然后,我在我的类方法中这样声明它们

   Tickets = new CDynamicArray<int>();
   FixedSLUsed = new CDynamicArray<bool>();
   PrevBarSLUsed = new CDynamicArray<bool>();

// 我在我的类方法中这样使用它们:
      int ticket = PositionGetInteger(POSITION_TICKET);
      int index;
      if(!Tickets.Contains(ticket))
      {
         Tickets.Add(ticket);
         FixedSLUsed.Add(false);
         PrevBarSLUsed.Add(false);
      }
      
      index = Tickets.IndexOf(ticket);  
// 删除了更多代码



希望对大家有所帮助