使用数组代替 std::vector 的优点? [英] Advantages of using arrays instead of std::vector?

查看:45
本文介绍了使用数组代替 std::vector 的优点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前看到很多标记为 C++ 并且与处理数组有关的问题.
甚至还有一些问题会询问 std::vector 无需任何魔法就可以提供的数组的方法/特性.

I'm currently seeing a lot of questions which are tagged C++ and are about handling arrays.
There even are questions which ask about methods/features for arrays which a std::vector would provide without any magic.

所以我想知道为什么这么多开发人员在 C++ 中选择数组而不是 std::vector?

So I'm wondering why so much developers are chosing arrays over std::vector in C++?

推荐答案

一般来说,我非常喜欢使用向量而不是数组来完成重要的工作;然而,数组有一些优点:

In general, I strongly prefer using a vector over an array for non-trivial work; however, there are some advantages of arrays:

  • 数组稍微紧凑一些:大小是隐式的.
  • 数组不可调整大小;有时这是可取的.
  • 数组不需要解析额外的 STL 标头(编译时).
  • 通过数组与直接 C 代码交互会更容易(例如,如果 C 正在分配而 C++ 正在使用).
  • 固定大小的数组可以直接嵌入到结构体或对象中,这可以提高内存局部性并减少所需的堆分配次数.

这篇关于使用数组代替 std::vector 的优点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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