c++ - 数组与 vector : Introductory Similarities and Differences

标签 c++ arrays vector

C++ 中的数组和 vector 有什么区别?差异的一个例子可能包括图书馆、符号、能力等。

数组

Arrays contain a specific number of elements of a particular type. So that the compiler can reserve the required amount of space when the program is compiled, you must specify the type and number of elements that the array will contain when it is defined. The compiler must be able to determine this value when the program is compiled. Once an array has been defined, you use the identifier for the array along with an index to access specific elements of the array. [...] arrays are zero-indexed; that is, the first element is at index 0. This indexing scheme is indicative of the close relationship in C++ between pointers and arrays and the rules that the language defines for pointer arithmetic.

— C++ Pocket Reference

vector

A vector is a dynamically-sized sequence of objects that provides array-style operator[] random access. The member function push_back copies its arguments via copy constructor, adds that copy as the last item in the vector and increments its size by one. pop_back does the exact opposite, by removing the last element. Inserting or deleting items from the end of a vector takes amortized constant time, and inserting or deleting from any other location takes linear time. These are the basics of vectors. There is a lot more to them. In most cases, a vector should be your first choice over a C-style array. First of all, they are dynamically sized, which means they can grow as needed. You don't have to do all sorts of research to figure out an optimal static size, as in the case of C arrays; a vector grows as needed, and it can be resized larger or smaller manually if you need to. Second, vectors offer bounds checking with the at member function (but not with operator[]), so that you can do something if you reference a nonexistent index instead of simply watching your program crash or worse, continuing execution with corrupt data.

— C++ Cookbook

最佳答案

数组:

  • 是内置语言结构;
  • 几乎未修改来自 C89;
  • 仅提供连续的、可索引的元素序列;没有风吹草动;
  • 具有固定大小;你不能在 C++ 中调整数组的大小(除非它是一个 POD 数组并且它是用 malloc 分配的);
  • 它们的大小必须是编译时常量,除非它们是动态分配的;
  • 它们根据您声明它们的范围占用它们的存储空间;
  • 如果是动态分配的,您必须显式释放它们;
  • 如果是动态分配的,你只是得到一个指针,并不能确定它们的大小;否则,您可以使用 sizeof(因此使用了常见的惯用语 sizeof(arr)/sizeof(*arr),但是当无意中用在指针上时会自动失败);<
  • 在大多数情况下自动衰减到指针;特别是,将它们传递给函数时会发生这种情况,这通常需要为它们的大小传递一个单独的参数;
  • 不能从函数返回; (除非是 std::array)
  • 不能直接复制/赋值;
  • 对象的动态数组需要默认构造函数,因为它们的所有元素都必须首先构造;

std::vector:

  • 是一个模板类;
  • 是仅 C++ 构造;
  • 被实现为一个动态数组
  • 动态增长和收缩;
  • 自动管理它们的内存,这些内存在销毁时释放;
  • 可以传递给函数/从函数返回(按值);
  • 可以被复制/分配(这执行所有存储元素的深拷贝);
  • 不会衰减为指针,但您可以显式获取指向其数据的指针(&vec[0] 保证按预期工作);
  • 总是与内部动态数组一起带来它的大小(当前存储了多少元素)和容量(可以存储多少元素 在当前分配的 block 中);
  • 内部动态数组不是在对象本身内部分配的(它只包含一些“簿记”字段),而是由相关模板参数中指定的分配器动态分配;默认的从自由存储区(所谓的堆)获取内存,与实际对象的分配方式无关;
  • 因此,对于小型、短命的局部阵列,它们的效率可能低于“常规”阵列;
  • 重新分配时,对象被复制(移动,在 C++11 中);
  • 不需要存储对象的默认构造函数;
  • 与其余所谓的 STL 更好地集成(它提供了 begin()/end() 方法,通常的 STL typedefs, ...)

还要考虑数组的“现代替代方案”——std::array;我已经在 another answer 中描述过std::vectorstd::array 的区别,你可能想看看。

关于c++ - 数组与 vector : Introductory Similarities and Differences,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49673915/

相关文章:

c++ - 如何在没有背景的情况下加载bmp

c++ - QCustomPlot 与图形上的单个点交互

c++ - 找到幂集的第 n 组

c++ - 关于C++的问题

python - Numpy 广播数组

ios - 以编程方式快速制作二维数组

java - 求凸多边形 vector 交点的度数

vector - 未定义的向量.New

C++ 比较 vector 迭代器与实例

c++ - gdb backtrace 不显示它来自的虚函数