Visual C++ 概念:添加功能
ATL 集合类
ATL 提供了许多存储和访问数据的类。在确定使用哪个类时有几个因素起决定作用,其中包括:
- 要存储的数据量
- 访问数据时的效率/性能比
- 按索引或按键访问数据的能力
- 数据的排序方式
- 个人喜好
小集合类
ATL 提供了下列处理少量对象的数组类。不过,这些类受到限制,仅供 ATL 在内部使用。建议您在程序中不要使用它们。
类 | 数据存储的类型 |
---|---|
CSimpleArray | 实现处理少量对象的数组类。 |
CSimpleMap | 实现处理少量对象的映射类。 |
通用用途集合类
下面的类实现数组、列表和映射,并且以通用用途集合类的形式提供:
类 | 数据存储的类型 |
---|---|
CAtlArray | 实现数组。 |
CAtlList | 实现列表。 |
CAtlMap | 实现映射结构,通过该结构可以按键或按值引用数据。 |
CRBMap | 使用“红/黑”算法来实现映射结构。 |
CRBMultiMap | 实现“红/黑”多映射结构。 |
在调试版本中使用时,这些类将捕获许多编程错误,但出于性能考虑,在零售版本中不执行这些检查。
专用集合类
还提供了其他一些管理内存指针和接口指针的专用集合类:
类 | 作用 |
---|---|
CAutoPtrArray | 提供在构造聪明指针数组时有用的方法。 |
CAutoPtrList | 提供在构造聪明指针列表时有用的方法。 |
CComUnkArray | 存储 IUnknown 指针,并且设计用作 IConnectionPointImpl 模板类的参数。 |
CHeapPtrList | 提供在构造堆指针列表时有用的方法。 |
CInterfaceArray | 提供在构造 COM 接口指针数组时有用的方法。 |
CInterfaceList | 提供在构造 COM 接口指针列表时有用的方法。 |
选择集合类
每个可用的集合类提供了不同的性能特性,如下表所示。
- 第 2 列和第 3 列描述了每个类的排序和访问特性。在表中,术语“已排序”表示按插入或删除项的顺序确定项在集合中的顺序,而不是指项按其内容排序。术语“已索引”表示集合中的项可以通过整数索引检索,这和典型数组中的项很相似。
- 第 4 列和第 5 列描述了每个类的性能。在需要对集合执行多次插入操作的应用程序中,插入速度可能特别重要;而对于其他应用程序,查找速度可能更重要。
- 第 6 列描述了每个形状是否允许重复元素。
- 给定集合类操作的性能是根据完成该操作所需要的时间与集合中的元素数目之间的关系来表示的。如果操作所用的时间量随元素数目的增加呈线形增长,则该操作被描述为 O(n) 算法。相反,如果操作所用的时间量随元素数目的增加而增加得越来越少,则该操作被描述为 O(log n) 算法。因此,就性能而言,随着元素数目的增加,O(log n) 算法越来越胜过 O(n) 算法。
集合形状特性
形状 | 已排序? | 已索引? | 插入元素 | 搜索指定的元素 | 重复的元素? |
---|---|---|---|---|---|
列表 | 是 | 否 | 快速 (恒定时间) |
慢 O(n) |
是 |
数组 | 是 | 按 int (恒定时间) |
慢 O(n),除非在末尾插入(在这种情况下是恒定时间) |
慢 O(n) |
是 |
映射 | 否 | 按键 (恒定时间) |
快速 (恒定时间) |
快速 (恒定时间) |
否(键) 是(值) |
“红/黑”映射 | 是 (按键) |
按键 O(log n) |
快 O(log n) |
快 O(log n) |
否 |
“红/黑”多映射 | 是 (通过键) |
按键 O(log n) (每个键有多个值) |
快 O(log n) |
快 O(log n) |
是(每个键有多个值) |
使用 CTraits 对象
由于 ATL 集合类可用于存储广泛的用户定义数据类型,因此能够重写像比较这样的重要功能会很有用。而这是通过 CTraits 类实现的。
CTraits 类似于(但更加灵活)ConstructElements()、DestructElements()、SerializeElements() 等(它们是 MFC 的集合类中的机制)。
当您构造集合类时,为您提供了指定 CTraits 类的选项。此类将包含被组成集合类的其他方法调用时将执行操作(如比较)的代码。例如,如果列表对象包含您自己的用户定义结构,您可能需要重新定义相等测试,以便只比较特定的成员变量。这样,列表对象的 Find 方法将以更有用的方式操作。
示例
- // Collection class / traits class example.
- // This program demonstrates using a CTraits class
- // to create a new comparison operator.
- #include "stdafx.h"
- #include "atlcoll.h"
- #define MAX_STRING 80
- // Define our own data type to store in the list.
- struct MyData {
- int ID;
- TCHAR name[MAX_STRING];
- TCHAR address[MAX_STRING];
- };
- // Define our own traits class, making use of the
- // existing traits and overriding only the comparison
- // we need.
- class MyTraits : public CElementTraits< MyData >
- {
- public:
- // Override the comparison to only compare
- // the ID value.
- static bool CompareElements( const MyData& element1, const MyData& element2)
- {
- if ( element1.ID == element2.ID )
- return true;
- else
- return false;
- };
- };
- // The main function
- int _tmain(int argc, _TCHAR* argv[])
- {
- // Declare the array, with our data type and traits class
- CAtlList < MyData, MyTraits > MyList;
- // Create some variables of our data type
- MyData add_item, search_item;
- // Add some elements to the list.
- add_item.ID = 1;
- wsprintf( add_item.name, "Adam" );
- wsprintf( add_item.address, "One Garden Way" );
- MyList.AddHead( add_item );
- add_item.ID = 2;
- wsprintf( add_item.name, "Eve" );
- wsprintf( add_item.address, "One Garden Way" );
- MyList.AddHead( add_item );
- add_item.ID = 3;
- wsprintf( add_item.name, "Cain" );
- wsprintf( add_item.address, "Two Garden Way" );
- MyList.AddHead( add_item );
- // Create an element which will be used
- // to search the list for a match.
- search_item.ID = 2;
- wsprintf( search_item.name, "Don't care" );
- wsprintf( search_item.address, "Don't care" );
- // Perform a comparison by searching for a match
- // between any element in the list, and our
- // search item. This operation will use the
- // (overridden) comparison operator and will
- // find a match when the IDs are the same.
- POSITION i;
- i = MyList.Find( search_item );
- if ( i != NULL )
- printf("Item found!\n");
- else
- printf("Item not found.\n");
- return 0;
- }
有关 CTraits 类的列表,请参见集合类。
下面的关系图显示了 CTraits 类的类层次结构。
集合类示例
以下示例说明了集合类:
- MMXSwarm 示例
- DynamicConsumer 示例
- UpdatePV 示例
- Marquee 示例
请参见
ATL | 集合类