CStringArray Class

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

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.

 

Insertion/Removal

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.

 

例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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值