数组、列表和映射类Array, List, and Map Classes

为了处理数据的聚合,类库为集合类(数组、列表和映射)提供了一个组,该组可保留各种对象和预定义类型。For handling aggregates of data, the class library provides a group of collection classes — arrays, lists, and maps — that can hold a variety of object and predefined types. 将动态调整集合的大小。The collections are dynamically sized. 这些类可在任何程序中使用,无论是否为 Windows 编写的都是如此。These classes can be used in any program, whether written for Windows or not. 但是,它们对实现数据结构(定义应用程序框架中的文档类)最有用。However, they are most useful for implementing the data structures that define your document classes in the application framework. 您可以轻松地从其中派生专用集合类,还可以基于模板类进行创建。You can readily derive specialized collection classes from these, or you can create them based on the template classes. 有关这些方法的详细信息,请参阅文章 集合For more information about these approaches, see the article Collections. 有关模板集合类的列表,请参阅 " 数组、列表和映射的模板类" 一文。For a list of the template collection classes, see the article Template Classes for Arrays, Lists, and Maps.

数组是连续存储在内存中的一维数据结构。Arrays are one-dimensional data structures that are stored contiguously in memory. 它们支持非常快的随机访问,因为任何给定元素的内存地址都可以通过将元素的索引乘以元素的大小并将结果添加到数组的基址来计算。They support very fast random access since the memory address of any given element can be calculated by multiplying the index of the element by the size of an element and adding the result to the base address of the array. 但是数组在您必须将元素插入数组时非常贵,因为插入元素的数组必须移动以为要插入的元素提供空间。But arrays are very expensive if you have to insert elements into the array, since the entire array past the element inserted has to be moved to make room for the element to be inserted. 数组可根据需要增长和收缩。Arrays can grow and shrink as necessary.

列表类似于数组,但其存储方式截然不同。Lists are similar to arrays but are stored very differently. 列表中的每个元素还包括指向上一个元素和下一个元素的指针,这使列表像双重链接列表。Each element in a list also includes a pointer to the previous and next elements, making it a doubly linked list. 项目的添加或删除非常快,因为这样做仅涉及更改一些指针。It is very fast to add or delete items because doing so only involves changing a few pointers. 但是,搜索列表可能非常贵,因为所有搜索都需要在列表结尾之一处启动。However, searching a list can be expensive since all searches need to start at one of the list's ends.

映射将一个键值与一个数据值相关联。Maps relate a key value to a data value. 例如,映射的键可以是字符串和指针指向的列表数据。For instance, the key of a map could be a string and the data a pointer into a list. 您将要求映射为您提供与特殊字符串关联的指针。You would ask the map to give you the pointer associated with a particular string. 映射查找非常快,因为映射将使用哈希表进行键查找。Map lookups are fast because maps use hash tables for key lookups. 项目的添加和删除也非常快。Adding and deleting items is also fast. 映射通常与作为辅助索引的其他数据结构一起使用。Maps are often used with other data structures as auxiliary indices. MFC 使用一种特殊的映射,称为 消息映射 ,用于将 Windows 消息映射到指向该消息的处理程序函数的指针。MFC uses a special kind of map called a message map to map Windows messages to a pointer to the handler function for that message.

请参阅See also

类概述Class Overview