MFC - CArray

CArray 是一个最适合以随机或非顺序方式访问的数据的集合. CArray类支持类似C数组的数组,但可以根据需要动态缩小和增长.

  • 数组索引始终从位置0.

  • 您可以决定在添加超出当前范围的元素时是修复上限还是启用数组扩展.

  • 内存是连续分配给上限的,即使某些元素为空.

Sr .No.姓名&说明
1

Add

将一个元素添加到数组的末尾;必要时增长数组.

2

Append

将另一个数组追加到数组中;必要时增长数组

3

Copy

将另一个数组复制到数组中;必要时增长数组.

4

ElementAt

返回对数组中元素指针的临时引用.

5

FreeExtra

将所有未使用的内存释放到当前上限之上.

6

GetAt

将所有未使用的内存释放到当前上限之上.

7

GetCount

获取此数组中元素的数量.

8

GetData

允许访问数组中的元素.可以 NULL .

9

GetSize

获取此数组中元素的数量.

10

GetUpperBound

返回最大的有效索引.

11

InsertAt

在指定的索引处插入元素(或另一个数组中的所有元素).

12

IsEmpty

确定数组是否为空.

13

RemoveAll

从中删除所有元素这个数组.

14

RemoveAt

删除特定索引处的元素.

15

SetAt

集值得的给定指数;数组不允许
增长.

16

SetAtGrow

设置给定索引的值;必要时增长数组.

17

SetSize

设置此数组中包含的元素数.

以下是对CArray对象的不同操作 :

创建CArray对象

要创建CArray值或对象的集合,必须首先确定集合的值的类型.您可以使用现有的原始数据类型之一,如int,CString,double等,如下所示;

CArray<CString, CString>strArray;

添加项目

要添加项目,您可以使用CArray :: Add()函数.它在数组的末尾添加一个项目.在OnInitDialog()中,创建CArray对象并添加三个名称,如下面的代码所示.

CArray<CString, CString>strArray;

//Add names to CArray
strArray.Add(L"Ali");
strArray.Add(L"Ahmed");
strArray.Add(L"Mark");

检索项目

要检索任何项目,可以使用CArray :: GetAt()函数.此函数将一个整数参数作为数组的索引.

步骤1 : 让我们看一个简单的例子,它将检索所有的名字.

//Retrive names from CArray
   for (int i = 0; i < strArray.GetSize(); i++) {
      m_strText.Append(strArray.GetAt(i) + L"\n"); 
}

第2步 : 以下是CMFCCArrayDlg :: OnInitDialog()的完整实现

BOOL CMFCCArrayDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();

   // Set the icon for this dialog. The framework does this automatically
   // when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);               // Set big icon
   SetIcon(m_hIcon, FALSE);             // Set small icon

   // TODO: Add extra initialization here
   CArray<CString, CString>strArray;
   
   //Add names to CArray
   strArray.Add(L"Ali");
   strArray.Add(L"Ahmed");
   strArray.Add(L"Mark");
   
   //Retrive names from CArray
   for (int i = 0; i < strArray.GetSize(); i++) {
      m_strText.Append(strArray.GetAt(i) + L"\n");
   }
   
   UpdateData(FALSE);
   return TRUE; // return TRUE unless you set the focus to a control
}

步骤3 : 编译并执行上面的代码时,您将看到以下输出.

检索项目

在中间添加项目

要在数组中间添加项目,可以使用CArray ::.InsertAt()函数.它需要两个参数 - 第一,索引和第二,值.

让我们在索引1处插入一个新项目,如下面的代码所示.

BOOL CMFCCArrayDlg::OnInitDialog() {
   
   CDialogEx::OnInitDialog();
   
   // Set the icon for this dialog. The framework does this automatically
   // when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);             // Set big icon
   SetIcon(m_hIcon, FALSE);            // Set small icon

   // TODO: Add extra initialization here
   CArray<CString, CString>strArray;
   //Add names to CArray
   strArray.Add(L"Ali");
   strArray.Add(L"Ahmed");
   strArray.Add(L"Mark");

   strArray.InsertAt(1, L"Allan");

   //Retrive names from CArray
   for (int i = 0; i < strArray.GetSize(); i++) {
      m_strText.Append(strArray.GetAt(i) + L"\n");
   }

   UpdateData(FALSE);
   return TRUE; // return TRUE unless you set the focus to a control
}

编译并执行上述代码时,您将看到以下输出.你现在可以看到Allan这个名字作为第二个索引.

Add Items

更新项目值

要更新数组中间的项目,可以使用CArray :: .SetAt()函数.它需要两个参数 - 第一,索引和第二,值.

让我们更新数组中的第三个元素,如下面的代码所示.

BOOL CMFCCArrayDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();

   // Set the icon for this dialog. The framework does this automatically
   // when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);                 // Set big icon
   SetIcon(m_hIcon, FALSE);               // Set small icon

   // TODO: Add extra initialization here
   CArray<CString, CString>strArray;

   //Add names to CArray
   strArray.Add(L"Ali");
   strArray.Add(L"Ahmed");
   strArray.Add(L"Mark");
  
   strArray.InsertAt(1, L"Allan");
   
   strArray.SetAt(2, L"Salman");
   
   //Retrive names from CArray
   for (int i = 0; i < strArray.GetSize(); i++) {
      m_strText.Append(strArray.GetAt(i) + L"\n");
   }

   UpdateData(FALSE);
   return TRUE; // return TRUE unless you set the focus to a control
}

编译并执行上述代码时,您将看到以下输出.您现在可以看到第三个元素的值已更新.

更新项目

复制数组

要将整个数组复制到另一个CArray对象,可以使用CArray :: Copy()函数.

Step1 : 让我们创建另一个数组并复制第一个数组中的所有元素,如下面的代码所示.

BOOL CMFCCArrayDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();

   // Add "About..." menu item to system menu.

   // IDM_ABOUTBOX must be in the system command range.
   ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
   ASSERT(IDM_ABOUTBOX < 0xF000);
   CMenu* pSysMenu = GetSystemMenu(FALSE);
   if (pSysMenu != NULL) {
      BOOL bNameValid;
      CString strAboutMenu;
      bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
      ASSERT(bNameValid);
      if (!strAboutMenu.IsEmpty()) {
         pSysMenu&rarr;AppendMenu(MF_SEPARATOR);
         pSysMenu&rarr;AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
      }
   }
   // Set the icon for this dialog. The framework does this automatically
   // when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);               // Set big icon
   SetIcon(m_hIcon, FALSE);              // Set small icon

   // TODO: Add extra initialization here
   CArray<CString, CString>strArray;
   //Add names to CArray
   strArray.Add(L"Ali");
   strArray.Add(L"Ahmed");
   strArray.Add(L"Mark");

   strArray.InsertAt(1, L"Allan");

   strArray.SetAt(2, L"Salman");

   CArray<CString, CString>strArray2;
   strArray2.Copy(strArray);
   //Retrive names from CArray
   for (int i = 0; i < strArray2.GetSize(); i++) {
      m_strText.Append(strArray2.GetAt(i) + L"\n");
   }

   UpdateData(FALSE);
   return TRUE; // return TRUE unless you set the focus to a control
}

现在你可以看到我们从2 中检索了元素nd 数组,输出相同,因为我们使用了复制功能.

复制数组

删除项目

要删除任何特定项目,可以使用CArray :: RemoveAt()函数.要从列表中删除所有元素,可以使用CArray :: RemoveAll()函数.

让我们从数组中删除第二个元素.

BOOL CMFCCArrayDlg::OnInitDialog() {
   CDialogEx::OnInitDialog();

   SetIcon(m_hIcon, TRUE);             // Set big icon
   SetIcon(m_hIcon, FALSE);            // Set small icon

   // TODO: Add extra initialization here
   CArray<CString, CString>strArray;

   //Add names to CArray
   strArray.Add(L"Ali");
   strArray.Add(L"Ahmed");
   strArray.Add(L"Mark");

   strArray.InsertAt(1, L"Allan");

   strArray.SetAt(2, L"Salman");

   CArray<CString, CString>strArray2;
   strArray2.Copy(strArray);

   strArray2.RemoveAt(1);

   //Retrive names from CArray
   for (int i = 0; i < strArray2.GetSize(); i++) {
      m_strText.Append(strArray2.GetAt(i) + L"\n");
   }

   UpdateData(FALSE);
   return TRUE; // return TRUE unless you set the focus to a control
}

编译并执行上述代码时,您将看到以下输出.您现在可以看到Allan这个名称不再是数组的一部分.

删除项目