MFC-CArray


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

  • 数组索引始终从位置 0 开始。

  • 当您添加超出当前界限的元素时,您可以决定是固定上限还是使数组扩展。

  • 即使某些元素为空,内存也会连续分配到上限。

以下是对 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 的名字被添加为第二个索引。

添加项目

更新项目值

要更新数组中间的项目,可以使用 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→AppendMenu(MF_SEPARATOR);
         pSysMenu→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
}

您现在可以看到我们已经从第二个数组中检索了元素,并且输出是相同的,因为我们使用了复制函数。

复制数组

删除项目

要删除任何特定项目,可以使用 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 不再是数组的一部分。

删除项目