现在的位置: 首页 > 综合 > 正文

ATL 集合类

2014年10月14日 ⁄ 综合 ⁄ 共 3811字 ⁄ 字号 评论关闭
 
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 方法将以更有用的方式操作。

示例

  1. // Collection class / traits class example.
  2. // This program demonstrates using a CTraits class
  3. // to create a new comparison operator.
  4.  
  5. #include "stdafx.h"
  6. #include "atlcoll.h"
  7.  
  8. #define MAX_STRING 80
  9.  
  10. // Define our own data type to store in the list.
  11.  
  12. struct MyData {
  13.       int ID;
  14.       TCHAR name[MAX_STRING];
  15.       TCHAR address[MAX_STRING];
  16.    };
  17.  
  18. // Define our own traits class, making use of the
  19. // existing traits and overriding only the comparison
  20. // we need.
  21.  
  22. class MyTraits : public CElementTraits< MyData >
  23. {
  24. public:
  25.  
  26.    // Override the comparison to only compare
  27.    // the ID value.
  28.  
  29.    static bool CompareElements( const MyData& element1, const MyData& element2)
  30.    {
  31.       if ( element1.ID == element2.ID )
  32.             return true;
  33.       else
  34.          return false;
  35.    };
  36. };
  37.  
  38. // The main function
  39.  
  40. int _tmain(int argc, _TCHAR* argv[])
  41. {
  42.    
  43.    // Declare the array, with our data type and traits class 
  44.  
  45.    CAtlList < MyData, MyTraits > MyList;
  46.  
  47.    // Create some variables of our data type
  48.  
  49.    MyData add_item, search_item;
  50.  
  51.    // Add some elements to the list.
  52.    
  53.    add_item.ID = 1;
  54.    wsprintf( add_item.name, "Adam" );
  55.    wsprintf( add_item.address, "One Garden Way" );
  56.  
  57.    MyList.AddHead( add_item );
  58.  
  59.    add_item.ID = 2;
  60.    wsprintf( add_item.name, "Eve" );
  61.    wsprintf( add_item.address, "One Garden Way" );
  62.  
  63.    MyList.AddHead( add_item );
  64.  
  65.    add_item.ID = 3;
  66.    wsprintf( add_item.name, "Cain" );
  67.    wsprintf( add_item.address, "Two Garden Way" );
  68.  
  69.    MyList.AddHead( add_item );
  70.  
  71.    // Create an element which will be used
  72.    // to search the list for a match.
  73.  
  74.    search_item.ID = 2;
  75.    wsprintf( search_item.name, "Don't care" );
  76.    wsprintf( search_item.address, "Don't care" );
  77.  
  78.    // Perform a comparison by searching for a match
  79.    // between any element in the list, and our
  80.    // search item. This operation will use the
  81.    // (overridden) comparison operator and will
  82.    // find a match when the IDs are the same.
  83.  
  84.    POSITION i;
  85.  
  86.    i = MyList.Find( search_item );
  87.  
  88.    if ( i != NULL )
  89.       printf("Item found!\n");
  90.    else
  91.       printf("Item not found.\n");
  92.  
  93.    return 0;
  94. }

有关 CTraits 类的列表,请参见集合类。

下面的关系图显示了 CTraits 类的类层次结构。

集合类示例

以下示例说明了集合类:

请参见

ATL | 集合类

抱歉!评论已关闭.