MFC - 数组(Carray)

MFC - 数组(Carray) 首页 / MFC入门教程 / MFC - 数组(Carray)

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

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

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

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

创建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 dd作为第二个索引。

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
}

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

链接:https://www.learnfk.comhttps://www.learnfk.com/mfc/mfc-carray.html

来源:LearnFk无涯教程网

Update Items

复制数组

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

创建另一个数组,并复制第一个数组中的所有元素,如以下代码所示。

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()) {
         pSysMenuAppendMenu(MF_SEPARATOR);
         pSysMenuAppendMenu(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不再是数组的一部分。

删除项目

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

技术与商业案例解读 -〔徐飞〕

零基础学Python -〔尹会生〕

深入浅出云计算 -〔何恺铎〕

如何看懂一幅画 -〔罗桂霞〕

Django快速开发实战 -〔吕召刚〕

说透数字化转型 -〔付晓岩〕

说透低代码 -〔陈旭〕

说透元宇宙 -〔方军〕

JavaScript进阶实战课 -〔石川〕

好记忆不如烂笔头。留下您的足迹吧 :)