C++ 的一般用例容器

发布于 2024-09-29 08:47:44 字数 245 浏览 3 评论 0原文

C++ 标准库容器的一般用例是什么?

  • 通常
  • 适合
  • 例如
  • 搜索
  • 配对
  • 映射

What are the general use cases for the C++ standard library containers?

  • bitset
  • deque
  • list
  • map
  • multimap
  • multiset
  • priority_queue
  • queue
  • set
  • stack
  • vector

For example, a map is generally better for a paired search.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光与爱终年不遇 2024-10-06 08:47:44

一张图胜过一千个字。

容器选择流程图

它可以从 nolyc 获得,它是 Freenode 上 ##C++ 的信息机器人,使用命令“container choice”或“containerchoice”。您收到的回复中这张图片的链接托管在 adrinael.net 上,这表明我们应该感谢 Adrinael,Freenode ##C++ 社区的成员。

A picture is worth a thousand words.

container choice flowchart

It's available from nolyc, the informative bot of ##C++ on Freenode, using the command "container choice" or "containerchoice". The link to this picture you receive in response is hosted at adrinael.net, which suggests we should thank Adrinael, member of Freenode's ##C++ community.

幻想少年梦 2024-10-06 08:47:44

bitset - 用于存储位。通用 - 存储一些标志的值。为此,您不需要超过 1 位。

deque - 双端队列 - push_back、push_front、pop_back 和 pop_front - 基本类的方法。 “未排序”(无序)容器。

list - 链接列表。该容器不是内存连续的。添加和删​​除元素的时间为 O(1),但查找特定元素的时间为 O(n)。无序容器。

map - 容器,存储对(std::pair)。第一个是键 - 地图中的每个元素都必须具有唯一的键。映射被表示为树,因此在映射中搜索元素是n*log(n)。这个容器总是排序的,这就是为什么添加和删除元素可能会导致更多时间 - 树(数据结构)是二元且平衡的。

multimap - 几乎与 std::map 相同,但允许使用相同的键配对。例如,多重映射可以包含元素: (666, "alabala"), (666, "asdfg"),而标准 std::map 则不能。这个容器也是排序的。

multiset - 再次 - 与 set 相同,但具有可重复的元素。 set - 嗯,这也是始终排序的 STL 容器。例如,一个集合是 { 1, 2, 3 },当您尝试将“1”添加到该集合中时,它将不会被添加,因为已经存在这样的元素。 (它类似于数学的集合)。因此,多重集允许多个元素具有相同的值,例如 { 1, 1, 1, 2, 3, 4, 4, 4, 4 } 是一个正确的多重集,但它不是集合。在 std::set 中添加和删除元素仍然是对数时间,因为它表示为二元、排序和平衡树。

priority_queue - 根据某些严格的弱排序条件,它的第一个元素始终是它包含的最大元素。基本功能——push_back 和 pop_back。

队列 - FIFO 结构 - 先进先出。 (或与 LILO - 后进 - 后出相同)​​。它类似于标准队列 - 当您去商店并开始排队时,第一个到的人将是第一个走的。你可以只push_back和pop_front。无序容器。

set - 我已经在多重集部分描述了它。

堆栈 - LIFO - 后进先出 - 堆栈。基本功能-push_back、pop_back。无序容器。

vector - 类似于标准 C++ 数组。它被视为常规数组,它是内存连续的,可以传递给 C 程序(传递第一个元素的地址)。无序容器。

重要提示:我描述了基本功能,而不是整个功能。请阅读 CPlusPlus.com 了解更多信息。

bitset - used to store bits. General purpose - store some flags' values. You don't need more that 1 bit for that.

deque - double ended queue - push_back, push_front, pop_back and pop_front - basic class' methods. "Not sorted" (unordered) container.

list - linked list. This container is not memory-continuous. Its time for adding and deleting elements is O(1), but looking for a specific element is O(n). Unordered container.

map - container, stores pairs (std::pair). The first one is the key - every element from the map must be with unique key. The map is represented as tree, so the searching for an element in the map is n*log(n). This container is always sorted, that's why adding and removing elements could cause more time - the tree(the data structure) is binary and balanced.

multimap - almost the same as std::map, but allows pairs with the same keys. For example, a multimap could contain elements: (666, "alabala"), (666, "asdfg"), while the standard std::map can't. This container is also sorted.

multiset - again - the same as set, but with repeatable elements. set - well, this is also always sorted STL container. For example, a set is { 1, 2, 3 } and when you try to add '1' into this set, it will not be added, as already there's such element. (it's analogical to the mathematical's set). So, multiset allows several elements with the same value, for example { 1, 1, 1, 2, 3, 4, 4, 4, 4 } is a correct multiset, while it's not a set. Adding and removing element into std::set is still logarithmic time, as it's represented as a binary, sorted and balanced tree.

priority_queue - its first element is always the greatest of the elements it contains, according to some strict weak ordering condition. Basic functionality - push_back and pop_back.

queue - FIFO structure - First in, first out. (or the same as LILO - Last In - Last Out). It's analogue to a standard queue - when you go to a shop and start waiting on the queue, the first one there will be the first one to go. You can just push_back and pop_front. Unordered container.

set - I already described it in the multiset section.

stack - LIFO - Last In - First Out - stack. Basic functionality - push_back, pop_back. Unordered container.

vector - analogue to a standard c++ array. It's treated as regular array, it's memory-continuous, could be passed to a C program(passing the address of the first element). Unordered container.

IMPORTANT NOTE: I described the basic functionality, not the whole one. Read CPlusPlus.com for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文