After declaring a CArray variable, you should set its initial size. This is done by calling the CArray::SetSize() member function. Its syntax is: void SetSize(INT_PTR nNewSize, INT_PTR nGrowBy = -1); The first argument must be a positive number. If you are creating a brand new list, you should set this argument to 0. The second argument specifies how much the list will need to grow when it becomes necessary. It may be good idea to let the compiler take care of this aspect. For this reason, this argument is optional. Here is an example of calling this member function: void CExampleDlg::OnBnClickedCollectionBtn()
{
CArray<int, int> iNumbers;
iNumbers.SetSize(5);
}
When a new item is being added to the list, the compiler increases the amount of memory allocated to the list. Sometimes, the amount increased may be more than was necessary. If you want to intervene in releasing any extra memory that is not used, you can call the CArray::FreeExtra() member function. Its syntax is: void FreeExtra(); This member function can be used to release unused memory that was allocated when the list of items was increased.
As mentioned already, one of the strengths of the CArray class over the regular C++ array is that its list can grow or shrink at will. This means that, when an item is added to, or inserted into, the list, the number of items in the list increases. To know the number of items in the list, you can call the CArray::GetCount() member function: INT_PTR GetCount() const; Since the list is zero-based, the actual number of items is the total - 1. Here is an example: void CExampleDlg::OnBnClickedCollectionBtn() { CArray<int, int> iNumbers; iNumbers.SetSize(5); iNumbers.Add(712003); iNumbers.Add(605729); iNumbers.Add(384759); iNumbers.Add(400284); iNumbers.Add(728802); iNumbers.SetAt(2, 100040); CString strNumber; strNumber.Format(L"The collection contains %d items", iNumbers.GetCount()); AfxMessageBox(strNumber); } Besides GetCount(), you can call the CArray::GetSize() member function to know the size of the list. The syntax of the GetSize() member function is: INT_PTR GetSize() const; This member function returns the size of the list. Like GetCount(), the GetSize() member function returns the total number of items - 1.
If you already have a CArray collection, you can assign it to another collection. To let you perform this operation, the CArray class provides the Copy() member function whose syntax is: void Copy(const CArray& src); This member function takes a CArray collection as argument. Here is an example of calling it: void CExampleDlg::OnBnClickedCollectionBtn()
{
CArray<int, int> iNumbers;
iNumbers.SetSize(5);
iNumbers.SetAt(0, 712003);
iNumbers.SetAt(1, 605729);
iNumbers.SetAt(2, 384759);
iNumbers.SetAt(3, 400284);
iNumbers.SetAt(4, 728802);
CArray<int, int> iValues;
iValues.Copy(iNumbers);
}
When this function has been called, the elements of the arguments are assigned to the variable that called it. After doing this, you can use the new collection as you see fit. You can even add new elements to it.
An alternative to copying a collection is to add an existing collection at the end of another collection. To support this operation, the CArray is equipped with a member function named Append. Its syntax is: INT_PTR Append(const CArray& src); This member function takes a CArray collection as argument. Here is an example: void CExampleDlg::OnBnClickedCollectionBtn()
{
CArray<int, int> iNumbers;
iNumbers.SetSize(5);
iNumbers.SetAt(0, 712003);
iNumbers.SetAt(1, 605729);
iNumbers.SetAt(2, 384759);
iNumbers.SetAt(3, 400284);
iNumbers.SetAt(4, 728802);
CArray<int, int> iValues;
iValues.Append(iNumbers);
}
After assigning the old collection to the new one, you can add new elements to the new list. Here is an example: void CExampleDlg::OnBnClickedCollectionBtn()
{
CArray<int, int> iNumbers;
iNumbers.SetSize(5);
iNumbers.SetAt(0, 712003);
iNumbers.SetAt(1, 605729);
iNumbers.SetAt(2, 384759);
iNumbers.SetAt(3, 400284);
iNumbers.SetAt(4, 728802);
CArray<int, int> iValues;
iValues.Append(iNumbers);
iValues.Add(209749);
}
Probably the most regularly performed operation on a collection consists of retrieving the values of its members. In C++, you access an element of an array using its index. To let you perform the same operation, the CArray class provides various options. One way you can locate an item in the list consists of using the square brackets, the same way you would proceed for a C++ array. The square bracket operator was overloaded with the following syntaxes: TYPE& operator[](INT_PTR nIndex); const TYPE& operator[](INT_PTR nIndex) const; As you can see, this operator takes the index of the desired item and returns the value that is stored at that index. Because the list is zero-based like a C++ array, the first item has an index of 0; the second has an index of 1, and so on. Here is an example of using the square brackets: void CExampleDlg::OnBnClickedCollectionBtn()
{
CArray<int, int> iNumbers;
iNumbers.Add(712003);
iNumbers.Add(605729);
iNumbers.Add(384759);
iNumbers.Add(400284);
iNumbers.Add(728802);
CString strNumber;
for(int i = 0; i < iNumbers.GetCount(); i++)
{
strNumber.Format(L"%d", iNumbers[i]);
AfxMessageBox(strNumber);
}
}
If you had declared a CArray variable and allocated memory for it, to assign the a value to an element, you can apply the square brackets [] to the variable, exactly as done on an array in C, and assign the desired value to it. Here is an example: void CExampleDlg::OnBnClickedCollectionBtn()
{
CArray<int, int> iNumbers;
iNumbers.SetSize(5);
iNumbers[0] = 712003;
iNumbers[1] = 605729;
iNumbers[2] = 384759;
iNumbers[3] = 400284;
iNumbers[4] = 728802;
CString strNumber;
for(int i = 0; i < iNumbers.GetCount(); i++)
{
strNumber.Format(L"%d", iNumbers[i]);
AfxMessageBox(strNumber);
}
}
If you had created a collection and had added a few items to it, to change the value of an element, apply the square brackets [] to the variable, pass the desired index to the square brackets, and assign the desired value to it. An alternative to the square brackets is to call the CArray::SetAt() member function. Its syntax is: void SetAt(INT_PTR nIndex, ARG_TYPE newElement); The first argument of this member function is the index that the new item will occupy in the list. The second argument is the item that will be added to the list. Here are examples: void CExampleDlg::OnBnClickedCollectionBtn()
{
CArray<int, int> iNumbers;
iNumbers.SetSize(5);
iNumbers.SetAt(0, 712003);
iNumbers.SetAt(1, 605729);
iNumbers.SetAt(2, 384759);
iNumbers.SetAt(3, 400284);
iNumbers.SetAt(4, 728802);
CString strNumber;
for(int i = 0; i < iNumbers.GetCount(); i++)
{
strNumber.Format(L"%d", iNumbers[i]);
AfxMessageBox(strNumber);
}
}
Besides the square brackets, the CArray class provides the ElementAt() member function used to get an item based on its index in the list. The CArray::Element() member function is overloaded with two versions whose syntaxes are: TYPE& ElementAt(INT_PTR nIndex); const TYPE& ElementAt(INT_PTR nIndex) const; In both cases, the function takes an index as argument and it produces the element located at that index. Here is an example of calling it: void CExampleDlg::OnBnClickedCollectionBtn()
{
CArray<int, int> iNumbers;
iNumbers.SetSize(5);
iNumbers.SetAt(0, 712003);
iNumbers.SetAt(1, 605729);
iNumbers.SetAt(2, 384759);
iNumbers.SetAt(3, 400284);
iNumbers.SetAt(4, 728802);
CString strNumber;
for(int i = 0; i < iNumbers.GetCount(); i++)
{
strNumber.Format(L"Number[%d]: %d", i + 1, iNumbers.ElementAt(i));
AfxMessageBox(strNumber);
}
}
Besides the ElementAt() member function, the CArray class provides the GetAt() function that is provided in two versions. Their syntaxes are: TYPE& GetAt(INT_PTR nIndex); const TYPE& GetAt(INT_PTR nIndex)const; This function produces the element at the index passed as argument.
The members of a CArray list are stored contiguously like the values of a C/C++ array. Each member of a CArray collection can be located by its index, which is of type INT_PTR. To know the index of the last member of the list, you can call the CArray::GetUpperBound() member function. Its syntax is: INT_PTR GetUpperBound() const; This member function produces the index of the last member of the array. Here is an example: void CExampleDlg::OnBnClickedCollectionBtn()
{
CArray<int, int> iNumbers;
iNumbers.SetSize(5);
iNumbers.SetAt(0, 712003);
iNumbers.SetAt(1, 605729);
iNumbers.SetAt(2, 384759);
iNumbers.SetAt(3, 400284);
iNumbers.SetAt(4, 728802);
CString strNumber;
strNumber.Format(L"The index of the last item is %d", iNumbers.GetUpperBound());
AfxMessageBox(strNumber);
}
To store all the members of the list in one pointer, you can call the CArray::GetData() member function that is overloaded with two versions whose syntaxes are: const TYPE* GetData() const; TYPE* GetData(); This member function can help you access all members of the list as one.
Inserting an element consists of adding one inside the collection at the index of your choice. To perform this operation, you can call the InsertAt() member function that is overloaded with two versions. The syntax of the first is: void InsertAt(INT_PTR nIndex, ARG_TYPE newElement, INT_PTR nCount = 1); The nIndex argument is the position where the new element will be located. The newElement will be added at that position. The third argument is optional. Here is an example of calling this function without the third argument: void CExampleDlg::OnBnClickedCollectionBtn()
{
CArray<int, int> iNumbers;
iNumbers.SetSize(5);
iNumbers[0] = 712003;
iNumbers[1] = 605729;
iNumbers[2] = 384759;
iNumbers[3] = 400284;
iNumbers[4] = 728802;
iNumbers.InsertAt(2, 500500);
CString strNumber;
for(int i = 0; i < iNumbers.GetCount(); i++)
{
strNumber.Format(L"%d", iNumbers[i]);
AfxMessageBox(strNumber);
}
}
If you don't pass the third argument, one sample of the newElement will be added. If you want to insert more than one sample, pass the third argument. The second version of this member function is: void InsertAt(INT_PTR nStartIndex, CArray* pNewArray); The second argument is a CArray collection that you want to insert inside the collection that called this function. The nStartIndex argument is the position where to insert the pNewArray collection. Here is an example of calling this version: void CExampleDlg::OnBnClickedCollectionBtn()
{
CArray<int, int> iNumbers;
iNumbers.SetSize(5);
iNumbers[0] = 712003;
iNumbers[1] = 605729;
iNumbers[2] = 384759;
iNumbers[3] = 400284;
iNumbers[4] = 728802;
iNumbers.InsertAt(1, 500500);
CArray<int, int> iValues;
iValues.Add(111333);
iValues.Add(222444);
iNumbers.InsertAt(3, &iValues);
CString strNumber;
for(int i = 0; i < iNumbers.GetCount(); i++)
{
strNumber.Format(L"%d", iNumbers[i]);
AfxMessageBox(strNumber);
}
}
If your CArray collection contains an item you don't need anymore, you can delete it. To delete an item from the list, you can call the RemoveAt() member function. Its syntax is: void RemoveAt(INT_PTR nIndex, INT_PTR nCount = 1); The first argument to this member function is the index of the item you want to delete from the list. The value of this argument must be between the bounds of the list. If you pass only one argument, only the item at that position would be deleted. Here is an example: void CExampleDlg::OnBnClickedCollectionBtn()
{
CArray<int, int> iNumbers;
iNumbers.SetSize(5);
iNumbers.SetAt(0, 712003);
iNumbers.SetAt(1, 605729);
iNumbers.SetAt(2, 384759);
iNumbers.SetAt(3, 400284);
iNumbers.SetAt(4, 728802);
CString strNumber;
strNumber.Format(L"The index of the last item is %d",
iNumbers.GetUpperBound());
AfxMessageBox(strNumber);
iNumbers.RemoveAt(2);
strNumber.Format(L"The index of the last item is %d",
iNumbers.GetUpperBound());
AfxMessageBox(strNumber);
}
If you want to delete more than one item, pass the desired number as the second argument.
To delete all items from a CArray list, call its RemoveAll() member function. Its syntax is: void RemoveAll( ); When called, this CArray::RemoveAll() member function will clear the list of all members. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|