CStringArray : CObject
The CStringArray class supports arrays of CString objects.
The member functions of CStringArray are similar to the member functions of class CObArray. Because of this similarity, you can use the CObArray reference documentation for member function specifics. Wherever you see a CObject pointer as a return value, substitute a CString (not a CString pointer). Wherever you see a CObject pointer as a function parameter, substitute a LPCTSTR.
CObject* CObArray::GetAt( int <nIndex> ) const;
for example, translates to
CString CStringArray::GetAt( int <nIndex> ) const;
and
void SetAt( int <nIndex>, CObject* <newElement> )
translates to
void SetAt( int <nIndex>, LPCTSTR <newElement> )
CStringArray incorporates the IMPLEMENT_SERIAL macro to support serialization and dumping of its elements. If an array of CString objects is stored to an archive, either with an overloaded insertion operator or with the Serialize member function, each element is serialized in turn.
Note Before using an array, use SetSize to establish its size and allocate memory for it. If you do not use SetSize, adding elements to your array causes it to be frequently reallocated and copied. Frequent reallocation and copying are inefficient and can fragment memory.
If you need a dump of individual string elements in the array, you must set the depth of the dump context to 1 or greater.
When a CString array is deleted, or when its elements are removed, string memory is freed as appropriate.
For more information on using CStringArray, see the articleCollections in Visual C++ Programmer’s Guide.
#include <afxcoll.h>
CStringArray Class Members
Construction
CStringArray | Constructs an empty array for CString objects. |
Bounds
GetSize | Gets number of elements in this array. |
GetUpperBound | Returns the largest valid index. |
SetSize | Sets the number of elements to be contained in this array. |
Operations
FreeExtra | Frees all unused memory above the current upper bound. |
RemoveAll | Removes all the elements from this array. |
Element Access
GetAt | Returns the value at a given index. |
SetAt | Sets the value for a given index; array not allowed to grow. |
ElementAt | Returns a temporary reference to the element pointer within the array. |
GetData | Allows access to elements in the array. Can be NULL. |
Growing the Array
SetAtGrow | Sets the value for a given index; grows the array if necessary. |
Add | Adds an element to the end of the array; grows the array if necessary. |
Append | Appends another array to the array; grows the array if necessary. |
Copy | Copies anolther array to the array; grows the array if necessary. |
Insertion/Removal
InsertAt | Inserts an element (or all the elements in another array) at a specified index. |
RemoveAt | Removes an element at a specific index. |
Operators
operator [] | Sets or gets the element at the specified index. |
例1,直接add,查找并提取F:\VBA下文件名:
CString strPath = "F:\\VBA\\*.*";
CString strFileName;
CStringArray strResult;
strResult.RemoveAll(); //文件列表清空
CFileFind finder;
BOOL bWorking = finder.FindFile(strPath);
while (bWorking)
{
bWorking = finder.FindNextFile();
if(finder.IsDirectory()) //若确定找到的文件是否是一个目录,结束本次循环。
continue;
strFileName = finder.GetFileName();
//cout << (LPCTSTR)strFileName << endl;
strResult.Add(strFileName);
}
finder.Close();
int iCount = strResult.GetSize();
if(iCount > 0)
{
int i;
for(i = 0;i < iCount;i++)
{
cout << (LPCTSTR)strResult.GetAt(i) << "\n";
}
cout << "最大索引是:" << strResult.GetUpperBound() << "\n";
cout << "元素总数是:" << strResult.GetSize() << "\n";
system("pause");
}
else
{
;
}
例2,设置CStringAarry大小再增加:
// FifoCStringArray.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "afxcoll.h"
#include "iostream.h"
int main(int argc, char* argv[])
{
CStringArray caTest;
caTest.SetSize(6,1); //申请大小6个,设置递增进度为1,防止初始分配空间不足时产生的溢出错误
int iSize = caTest.GetSize();
int iMaxIndex = caTest.GetUpperBound();
cout << "iSize:" << iSize << endl;
cout << "iMaxIndex:" << iMaxIndex << endl;
caTest.SetAt(0,"0aa");
caTest.SetAt(1,"1aa");
caTest.SetAt(4,"4aa");
caTest.SetAt(5,"5aa");
cout << "iSize:" << iSize << endl;
cout << "iMaxIndex:" << iMaxIndex << endl;
CString csIndex;
int i = 0;
for(i = 0;i <= iMaxIndex;i++)
{
csIndex = caTest.GetAt(i);
if(csIndex.IsEmpty())
{
cout << "index " << i << " is null." << endl;
}
else
{
cout << "index " << i << ":" << csIndex << endl;
}
}
cout << "***************************************" << endl;
caTest.SetAt(4,"");
for(i = 0;i <= iMaxIndex;i++)
{
csIndex = caTest.GetAt(i);
if(csIndex.IsEmpty())
{
cout << "index " << i << " is null." << endl;
}
else
{
cout << "index " << i << ":" << csIndex << endl;
}
}
cout << "***************************************" << endl;
caTest.SetAt(4,"4aabb");
for(i = 0;i <= iMaxIndex;i++)
{
csIndex = caTest.GetAt(i);
if(csIndex.IsEmpty())
{
cout << "index " << i << " is null." << endl;
}
else
{
cout << "index " << i << ":" << csIndex << endl;
}
}
return 0;
}