[英]MFC, How to get the element in CArray, which element type is CString
I am confuse in GetAT and aryString[n], as follow code 我对GetAT和aryString [n]感到困惑,如下所示
CArray <CString, CString> arySctring;
aryString.SetSize(3);
aryString.Add(_T("a1"));
aryString.Add(_T("a222"));
aryString.Add(_T("a3"));
TRACE(_T("%d %s"), aryString.GetCount(), aryString[0]);
the TRACE result is "6 ", it means aryString[0] is no data, I instead of aryString.GetAt(0), the result is same. TRACE结果为“ 6”,表示aryString [0]没有数据,我代替aryString.GetAt(0),结果相同。
Why? 为什么?
.SetSize(3);
reserve 3 "rooms".
保留3个“房间”。
Calling
呼唤
Add();
three times, reserve another 3 "rooms", hence you get 6 at count and your array is as follow:
3次,再保留3个“房间”,因此您得到6个房间,数组如下:
. 。 To get the result I guess you want, once you've set size, you can do: 要获得想要的结果,可以在设置大小后执行以下操作:\n
\n
- "" ”
\n- "" ”
\n- "" ”
\n- "a1" “ a1”
\n- "a222" “ a222”
\n- "a3" “ a3”
\n
aryString.SetSize( 3 ); aryString[0] = "a1"; aryString[1] = "a222"; aryString[2] = "a3";
As side note, MFC provides you with CStringArray class, so you haven't to do: 附带说明,MFC为您提供了CStringArray类,因此您无需这样做:
CArray<CString,CString>
when you do aryString.SetSize(3);
当你做aryString.SetSize(3);
aryString
reserves 3 items with empty string. aryString
保留3个带有空字符串的项目。 when you Add three new strings at the end , the item count of the array is 6. the first item is empty string, aryString.GetAt[3]
will return a1, function add will auto increase the size of the array ,you do not have to SetSize(3)
to reserve space 当您在末尾添加三个新字符串时,数组的项目数为6。第一个项目为空字符串aryString.GetAt[3]
将返回a1,函数add将自动增加数组的大小,您无需必须设置SetSize(3)
来保留空间
I use as follow code to asign the element 我使用以下代码来分配元素
aryString.SetAtGrow(0, _T("a"));
aryString.SetAtGrow(1, _T("a"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.