阵列VS载体:初级异同 [英] Arrays vs Vectors: Introductory Similarities and Differences

查看:94
本文介绍了阵列VS载体:初级异同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是数组和C ++中的向量之间的区别是什么?的差异的例子可能包括图书馆,象征主义,能力等。

阵列


  

阵列包含特定类型的元件的具体数目。这样在程序编译时,编译器可以保留所需要的空间量,则必须指定它定义为当数组将包含元素的类型和数量。编译器必须能够确定这个值的程序进行编译时。一旦阵列已经定义,您可以使用标识符数组的索引以及阵列内访问特定的元素。 [...]数组索引从零开始;也就是说,第一个元素是索引0这个索引方案表明在C ++指针和数组与规则之间的语言定义指针运算的密切关系。


  
  

- C ++袖珍参考


矢量


  

一个载体,提供了阵列式运算符[] 随机访问对象的动态调整序列。成员函数的push_back通过复制拷贝构造函数它的参数,添加了副本中的向量的最后一个项目,并且由一个增加它的大小。 pop_back 做的正好相反,通过删除最后一个元素。插入或从矢量的末尾删除项目需要分期常量时间,和插入或任何其他位置删除需要线性时间。这些载体的基础知识。还有很多更给他们。在大多数情况下,载体应该是在一个C数组的第一选择。首先,它们是动态大小的,这意味着它们可以根据需要增加。你不必做各种研究,以找出最佳的静态大小,如C数组的情况;矢量生长着一种需要,一种可以调整大小更大或更小,如果手动你需要。第二,途径为界与该成员函数检查(但没有与运算符[] ),因此,如果您引用一个不存在的指标,而不是简单地看着你可以做一些你程序崩溃或更糟的是,继续执行与腐败的数据。


  
  

- C ++食谱



解决方案

数组:


  • 是一个内建的语言结构;

  • 从C89来几乎未经修改

  • 只提供一个连续的,元素的可转位序列;没有花俏;

  • 是固定的大小;你不能调整C中的数组++(除非它是POD的数组,它与的malloc 分配);

  • 它们的大小必须是一个编译时间常数,除非它们是动态分配的;

  • 他们从那里你声明它们的范围取决于他们的存储空间;

  • 如果动态分配的,必须明确释放他们;

  • 如果他们是动态分配的,你只是得到一个指针,你不能确定它们的大小;否则,你可以使用的sizeof (因此常见的成语的sizeof(ARR)/ sizeof的(* ARR),那不过在指针无意中使用时)静静地失败;

  • 自动衰减到在大多数情况下一个指针;特别是,这种情况将它们传递给一个功能,通常需要经过一个独立的参数为它们的大小时;

  • 不能从一个函数返回;

  • 无法复制/直接分配;

  • 对象的动态数组需要一个默认的构造函数,因为所有的元素都必须首先构建;

的std ::矢量


  • 是一个模板类;

  • 是一个C ++只建造;

  • 被实现为的动态数组的;

  • 的增长和动态缩小;

  • 自动管理他们的记忆,这是在毁灭中解脱出来;

  • 可以传递到/从功能(按价值计算)返回;

  • 可以复制/分配(此执行所有的存储元素的深层副本);

  • 不衰减的指针,但你的可以的明确获得一个指向他们的数据(&放大器; VEC [0] 是保证如预期的工作);

  • 总是与内部动态数组带来沿着它的尺寸的(有多少个元素目前储存)和容量的(有多少元素的可以存储在当前分配的块);

  • 内部动态阵列不​​是对象本身内分配(其只包含几个簿记域),而是由在相关的模板参数指定的分配器动态分配;默认的,从如何在实际对象被分配得到从的FreeStore存储器(所谓的堆),独立地;

  • 因为这个原因,他们可能会比正规的阵列为小型,短暂的,局部阵列效率较低;

  • 重新分配时,这些对象的复制的(移动,在C ++ 11);

  • 不需要对所存储的对象的默认构造函数;

  • 与所谓的STL(它提供了开始() / 端()方法,常用的STL 的typedef S,...)

另外还要考虑现代另类到数组 - 的std ::阵列;我在<一个已经说明href=\"http://stackoverflow.com/questions/4424579/stdvector-versus-stdarray-in-c/4424658#4424658\">another回答 的std ::向量之间的差异的std ::阵列,你可能希望有一个看看吧。

What are the differences between an array and a vector in C++? An example of the differences might be included libraries, symbolism, abilities, etc.

Array

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 within 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 a needed, an it can be resized larger or smaller manually if you need to. second, vectors offer bounds checking with the at member function (but no 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

解决方案

arrays:

  • are a builtin language construct;
  • come almost unmodified from C89;
  • provide just a contiguous, indexable sequence of elements; no bells and whistles;
  • are of fixed size; you can't resize an array in C++ (unless it's an array of POD and it's allocated with malloc);
  • their size must be a compile-time constant unless they are allocated dynamically;
  • they take their storage space depending from the scope where you declare them;
  • if dynamically allocated, you must explicitly deallocate them;
  • if they are dynamically allocated, you just get a pointer, and you can't determine their size; otherwise, you can use sizeof (hence the common idiom sizeof(arr)/sizeof(*arr), that however fails silently when used inadvertently on a pointer);
  • automatically decay to a pointers in most situations; in particular, this happens when passing them to a function, which usually requires passing a separate parameter for their size;
  • can't be returned from a function;
  • can't be copied/assigned directly;
  • dynamical arrays of objects require a default constructor, since all their elements must be constructed first;

std::vector:

  • is a template class;
  • is a C++ only construct;
  • is implemented as a dynamic array;
  • grows and shrinks dynamically;
  • automatically manage their memory, which is freed on destruction;
  • can be passed to/returned from functions (by value);
  • can be copied/assigned (this performs a deep copy of all the stored elements);
  • doesn't decay to pointers, but you can explicitly get a pointer to their data (&vec[0] is guaranteed to work as expected);
  • always brings along with the internal dynamic array its size (how many elements are currently stored) and capacity (how many elements can be stored in the currently allocated block);
  • the internal dynamic array is not allocated inside the object itself (which just contains a few "bookkeeping" fields), but is allocated dynamically by the allocator specified in the relevant template parameter; the default one gets the memory from the freestore (the so-called heap), independently from how where the actual object is allocated;
  • for this reason, they may be less efficient than "regular" arrays for small, short-lived, local arrays;
  • when reallocating, the objects are copied (moved, in C++11);
  • does not require a default constructor for the objects being stored;
  • is better integrated with the rest of the so-called STL (it provides the begin()/end() methods, the usual STL typedefs, ...)

Also consider the "modern alternative" to arrays - std::array; I already described in another answer the difference between std::vector and std::array, you may want to have a look at it.

这篇关于阵列VS载体:初级异同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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