什么是容器/适配器? C++
什么是容器/适配器?我有 C++ 及其子主题(例如(类/模板/STL))的基本知识。
谁能用通俗易懂的语言解释一下,并给我一个容器/适配器应用的实际例子?
What are containers/adapters? I have basic knowledge of C++ and its sub-topics like (class/templates/STL).
Can anyone please explain in layman's language and give me a practical example of the application of containers/adapters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
容器是一种包含数据的特定数据结构,通常数量无限。每种容器类型在如何有效访问、添加或删除数据方面都有限制。
下面是一些使用 STL 类的容器示例。
序列容器
这里是序列容器,意味着数据是可靠排序的(也就是说,它们有正面和背面。我并不是说它们会自动排序!)。
关联容器
这些是关联容器,意味着元素不再排序,而是相互关联,用于确定唯一性或映射:
容器适配器
另一方面,容器适配器是通过限制预先存在的容器中的功能并提供一组不同的功能而创建的接口。当您声明容器适配器时,您可以选择指定哪些序列容器构成底层容器。它们是:
请参阅此参考页了解更多信息,包括每个操作的时间复杂度以及详细信息的链接每种容器类型的页面。
A container is a specific data structure that contains data, usually in an unbounded amount. Each container type has limitations on how to access, add, or remove data efficiently.
Below are a few examples of containers using STL classes.
Sequence Containers
Here are the sequence containers, meaning the data is reliably ordered (that is, there is a front and a back to them. I do NOT mean that they automatically sort themselves!).
Associative Containers
These are associative containers, meaning that elements are no longer ordered but instead have associations with each other used for determining uniqueness or mappings:
Container Adapters
Container adapters, on the other hand, are interfaces created by limiting functionality in a pre-existing container and providing a different set of functionality. When you declare the container adapters, you have an option of specifying which sequence containers form the underlying container. These are:
See this reference page for more information, including time complexity for each of the operations and links to detailed pages for each of the container types.
C++ 是技术性的,很难理解:-D容器是 STL 中可以包含数据的数据类型。
示例:
vector
作为动态数组适配器是 STL 中的数据类型,可调整容器以提供特定接口。
示例:
stack
在所选容器顶部提供堆栈接口(旁注:两者实际上都是模板而不是数据类型,但这样定义看起来更好)
<joke>
C++ is technical and hard to understand :-D</joke>
Containers are data types from STL that can contain data.
Example:
vector
as a dynamic arrayAdapters are data types from STL that adapt a container to provide specific interface.
Example:
stack
providing stack interface on top of the chosen container(side note: both are actually templates not data types, but the definition looks better this way)
SGI STL 文档中“容器”的技术定义非常好:
因此,容器是一种保存(“包含”)某种类型的对象集合的数据结构。关键思想是存在不同类型的容器,每种容器以不同的方式存储对象并提供不同的性能特征,但它们都有一个标准接口,以便您可以轻松地将一个容器替换为另一个容器,而无需进行太多修改使用容器的代码。这个想法是容器被设计成尽可能可互换。
容器适配器是提供容器功能子集的类,但也可以提供附加功能,使容器更容易用于某些场景。例如,您可以轻松地使用
std::vector
或std::deque
作为堆栈数据结构并调用push_back
,back
和pop_back
作为堆栈接口;std::stack
提供了一个可以使用std::vector
或std::deque
或其他序列容器的接口,但提供了更标准的用于访问成员的push
、top
和pop
成员函数。The technical definition of "container" from The SGI STL documentation is pretty good:
So, a container is a data structure that holds ("contains") a collection of objects of some type. The key idea is that there are different types of containers, each of which stores objects in a different way and provides different performance characteristics, but all of them have a standard interface so that you can swap one out for another easily and without modifying too much of the code that uses the container. The idea is that the containers are designed to be interchangeable as much as possible.
The container adapters are classes that provide a subset of a container's functionality but may provide additional functionality that makes it easier to use containers for certain scenarios. For example, you could easily use
std::vector
orstd::deque
for a stack data structure and callpush_back
,back
, andpop_back
as the stack interface;std::stack
provides an interface that can use astd::vector
orstd::deque
or other sequence container but provides the more standardpush
,top
, andpop
member functions for accessing members.