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 中);
  • 内部动态数组不是在对象本身内部分配的(它只包含一些“簿记”字段),而是由相关模板参数中指定的分配器动态分配的;默认从freestore(所谓的堆)获取内存,与实际对象的分配方式无关;
  • 因此,对于小型、短暂的本地数组,它们的效率可能低于“常规”数组;
  • 在重新分配时,对象被复制(移动,在 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/15079057/

相关文章:

c++ - std::sort 在 C++ 中?

C++ vector : push_back Objects vs push_back Pointers performance

c++ - 使用 swap-and-pop 迭代时删除 vector 中的元素

c++ - 在 for 循环中使用 erase() 只删除第一行

javascript - 下划线 map 对象

php - 我如何对多个 mysql foreach 数组进行排序并需要它们在 php 中保持一致?

java - 按字母顺序对字符串数组进行排序。如何按每行中间的数字对同一数组进行排序

C++ Xcode 9 - 无法从 Cimg 调用函数 display()

c++ - 无限 std::chrono::duration 对象合法吗?

工作线程中的 C++ 未捕获异常