您的位置:首页 > 其它

CStringArray Class

2015-12-06 22:23 260 查看

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
CStringArrayConstructs an empty array for CString objects.
Bounds
GetSizeGets number of elements in this array.
GetUpperBoundReturns the largest valid index.
SetSizeSets the number of elements to be contained in this array.
Operations
FreeExtraFrees all unused memory above the current upper bound.
RemoveAllRemoves all the elements from this array.
Element Access
GetAtReturns the value at a given index.
SetAtSets the value for a given index; array not allowed to grow.
ElementAtReturns a temporary reference to the element pointer within the array.
GetDataAllows access to elements in the array. Can be NULL.
Growing the Array
<
c41a
/table>
Insertion/Removal
SetAtGrowSets the value for a given index; grows the array if necessary.
AddAdds an element to the end of the array; grows the array if necessary.
AppendAppends another array to the array; grows the array if necessary.
CopyCopies anolther array to the array; grows the array if necessary.
InsertAtInserts an element (or all the elements in another array) at a specified index.
RemoveAtRemoves an element at a specific index.
Operators
operator []Sets or gets the element at the specified index.
例如查找并提取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
{
 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: