CArray 是最适合用于以随机或非顺序方式访问的数据的集合, CArray类支持类似于C数组的数组,但是可以根据需要动态收缩和增长。
数组索引始终从位置0开始。
当您添加超出当前边界的元素时,您可以决定是固定上限还是启用数组扩展。
即使某些元素为空,内存也会连续分配给上限。
要创建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作为第二个索引。
要更新数组中间的元素,可以使用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无涯教程网
要将整个数组复制到另一个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()) { 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 }
现在您可以看到无涯教程已经从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不再是数组的一部分。
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)