在什么情况下是ATL CSimpleArray比CAtlArray更好的选择 [英] In what situations is the ATL CSimpleArray a better choice than CAtlArray

查看:902
本文介绍了在什么情况下是ATL CSimpleArray比CAtlArray更好的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说CSimpleArray是处理对象的小数字。这是小的在这方面?是有史以来CSimpleArray一个不错的选择,或者我应该总是使用不同的集合类,如CAtlArray?

The documentation says that CSimpleArray is for dealing with small numbers of objects. What is small in this context? Is CSimpleArray ever a good choice or should I always use a different collection class such as CAtlArray?

推荐答案

小是一个经验法则,它从两个阶级内部管理自己的内存的方式造成的。基本上,CAtlArray提供其占用的内存更细粒度的控制,并CSimpleArray记忆简单而天真地交易。

"Small" here is a rule of thumb, which stems from the way the two classes manage their memory internally. Basically, CAtlArray provides more fine-grained control of the memory it uses, and CSimpleArray deals with memory simply but naively.

具体地讲,当一个元件被添加到CSimpleArray,如果阵列已经使用所有已分配的存储器,这将增加一倍其大小,这是一个相当昂贵的操作。新创建的CSimpleArray将空间开始为0件。所以我们说要5件事情添加到一个数组。它看起来是这样的:

Specifically, when an element is added to a CSimpleArray, if the array already uses all the memory it has allocated, it will double its size, which is a fairly expensive operation. A newly created CSimpleArray will start with space for 0 items. So let's say you want to add 5 things to an array. It will look like this:


  • 添加第一个项目 - 没有空间,因此对于总
  • 1项重新分配空间
  • 添加第二个项目 - 没有空间,因此对于总
  • 2项重新分配空间
  • 添加第三个项目 - 没有空间,因此对于总
  • 4项重新分配空间
  • 添加4项 - 有房,所以只需要添加

  • 添加第五项 - 没有空间,因此对于总
  • 8个项目重新分配空间
  • 等等...

  • Add 1st item - there is no room, so reallocate space for 1 item in total
  • Add 2nd item - there is no room, so reallocate space for 2 items in total
  • Add 3rd item - there is no room, so reallocate space for 4 items in total
  • Add 4th item - there is room, so just add
  • Add 5th item - there is no room, so reallocate space for 8 items in total
  • and so on...

另外请注意,有没有办法指定CSimpleArray的初始大小,所以这彭定康总是会发生。

Also note that there is no way to specify the initial size of a CSimpleArray, so this patten will always happen.

在另一方面,一个CAtlArray让你一次全部指定分配的内存,通过SetCount()方法。使用与上述相同的例子,添加的物品之前,调用SetCount(5)。然后,总会有空间5个项目,也没有重新分配需要做的工作。

On the other hand, a CAtlArray allows you to specify the allocated memory all at once, via the SetCount( ) method. Using the same example as above, before adding the items, call SetCount(5). Then there will always be room for 5 items and no reallocations need to be done.

因此​​,要回答这个问题:使用CAtlArray如果你关心内存管理,特别是如果你担心ABOT性能。使用CSimpleArray如果你只是想保持列表中的几个项目并不在乎如何列表占用存储管理。要回答什么小和大的意思是在这方面的具体问题,小是指几足够的元素,这是确定和你一起重新分配每一个长度超过2下次上电时间。

So to answer the question: use CAtlArray if you care about memory management, especially if you are worried abot performance. Use CSimpleArray if you just want to keep a few items in a list and don't care how the memory the list occupies is managed. To answer the specific question about what "small" and "large" mean in this context, "small" means few enough elements that it's ok with you to reallocate every time the length exceeds the next power of 2.

这也是值得指出的是CSimpleArray可以让你通过数组使用Find()方法搜索,CAtlArray没有。

It's also worthwhile to note that CSimpleArray lets you search through the Array using the Find( ) method, and CAtlArray does not.

(注:我的答案仅仅是基于通过ATL源$ C ​​$ C看)

(Note: my answer is based only on looking through the ATL source code.)

这篇关于在什么情况下是ATL CSimpleArray比CAtlArray更好的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆