如何在C ++ 11中有效地选择标准库容器? [英] How can I efficiently select a Standard Library container in C++11?

查看:145
本文介绍了如何在C ++ 11中有效地选择标准库容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个众所周知的图像(cheat sheet)叫做C ++容器选择。



有人知道是否已经有一个C ++ 11版本?



这是前一个:


解决方案

不是我知道的,但是它可以做文本我想。此外,图表略微偏离,因为 list 通常不是一个好的容器,也不是 forward_list



要创建这样的图表,您只需要两个简单的指南:




  • 首先选择语义

  • 如果有多个选择,请选择最简单的



担心性能通常是没有用的。



有两个大类别的容器:




  • 关联容器:他们有找到操作

  • 简单序列容器



,然后可以在其上构建几个适配器: stack 队列 priority_queue






问题1:关联




  • 如果您需要通过一个键轻松搜索, / li>
  • 如果您需要排序元素,则需要有序的关联容器

  • 否则,跳转到问题2。问题1.1:订购




      li>如果您不需要特定订单,请使用无序_ 容器,否则使用其传统的有序订单。


    问题1.2:分隔键




    • 该值,使用映射,否则使用设置



    问题1.3:重复次数




    • ,请使用,否则不要。



    示例:



    假设我有几个人具有与其相关联的唯一ID,并且我想尽可能简单地从其ID中检索个人数据。


    1. 我想要一个查找函数,因此是一个关联容器



      1.1。我不在乎关于订单,因此无序_ 容器



      1.2。我的键(ID)与它相关联的值是分开的,因此映射




    最终答案是:问题2:内存(内存)

    稳定




    • 如果元素在内存中应该是稳定的(即,容器本身修改时不应该移动),然后使用一些列表

    • 否则,跳转到问题3.

    $问题2.1:其中


    $ b

    $ c> list ; forward_list 只对较小的内存占用有用。







问题3:动态大小



    如果容器具有已知大小在编译时),,这个大小在程序过程中不会改变,元素是默认可构造的一个完整的初始化列表(使用 {...} 语法),然后使用数组
  • 否则,跳到问题4。






问题4:双端




  • 如果您希望能够从前面和后面删除项目,请使用 deque ,否则使用向量






你会注意到,默认情况下,除非你需要一个关联容器,你的选择将是向量。原来它也是 Sutter and Stroustrup的建议


There's a well known image (cheat sheet) called "C++ Container choice". It's a flow chart to choose the best container for the wanted usage.

Does anybody know if there's already a C++11 version of it?

This is the previous one:

解决方案

Not that I know of, however it can be done textually I guess. Also, the chart is slightly off, because list is not such a good container in general, and neither is forward_list. Both lists are very specialized containers for niche applications.

To build such a chart, you just need two simple guidelines:

  • Choose for semantics first
  • When several choices are available, go for the simplest

Worrying about performance is usually useless at first. The big O considerations only really kick in when you start handling a few thousands (or more) of items.

There are two big categories of containers:

  • Associative containers: they have a find operation
  • Simple Sequence containers

and then you can build several adapters on top of them: stack, queue, priority_queue. I will leave the adapters out here, they are sufficiently specialized to be recognizable.


Question 1: Associative ?

  • If you need to easily search by one key, then you need an associative container
  • If you need to have the elements sorted, then you need an ordered associative container
  • Otherwise, jump to the question 2.

Question 1.1: Ordered ?

  • If you do not need a specific order, use an unordered_ container, otherwise use its traditional ordered counterpart.

Question 1.2: Separate Key ?

  • If the key is separate from the value, use a map, otherwise use a set

Question 1.3: Duplicates ?

  • If you want to keep duplicates, use a multi, otherwise do not.

Example:

Suppose that I have several persons with a unique ID associated to them, and I would like to retrieve a person data from its ID as simply as possible.

  1. I want a find function, thus an associative container

    1.1. I couldn't care less about order, thus an unordered_ container

    1.2. My key (ID) is separate from the value it is associated with, thus a map

    1.3. The ID is unique, thus no duplicate should creep in.

The final answer is: std::unordered_map<ID, PersonData>.


Question 2: Memory stable ?

  • If the elements should be stable in memory (ie, they should not move around when the container itself is modified), then use some list
  • Otherwise, jump to question 3.

Question 2.1: Which ?

  • Settle for a list; a forward_list is only useful for lesser memory footprint.

Question 3: Dynamically sized ?

  • If the container has a known size (at compilation time), and this size will not be altered during the course of the program, and the elements are default constructible or you can provide a full initialization list (using the { ... } syntax), then use an array. It replaces the traditional C-array, but with convenient functions.
  • Otherwise, jump to question 4.

Question 4: Double-ended ?

  • If you wish to be able to remove items from both the front and back, then use a deque, otherwise use a vector.

You will note that, by default, unless you need an associative container, your choice will be a vector. It turns out it is also Sutter and Stroustrup's recommendation.

这篇关于如何在C ++ 11中有效地选择标准库容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆