的std ::向量与性病::用C ++数组 [英] std::vector versus std::array in C++

查看:144
本文介绍了的std ::向量与性病::用C ++数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是C ++中的的std ::矢量的std ::阵列之间的区别?之一,当应pf​​erred对另一$ P $?各有什么利弊?我所有的教材确实是他们名单怎么都是一样的。

What are the difference between a std::vector and an std::array in C++? When should one be preferred over another? What are the pros and cons of each? All my textbook does is list how they are the same.

推荐答案

的std ::矢量是一个模板类封装了一个动态数组 1 ,存储在堆,即增大和收缩,如果自动元素被添加或移除。它提供了所有使得它做工精细用钩(开始()端(),迭代器等) STL中的其余部分。它也有一些有用的方法,使您可以执行操作一个正常的阵列上会很麻烦,例如像插入在载体中的中间元件(其处理移动幕后下列元素的所有工作)。

std::vector is a template class that encapsulate a dynamic array1, stored in the heap, that grows and shrinks automatically if elements are added or removed. It provides all the hooks (begin(), end(), iterators, etc) that make it work fine with the rest of the STL. It also has several useful methods that let you perform operations that on a normal array would be cumbersome, like e.g. inserting elements in the middle of a vector (it handles all the work of moving the following elements behind the scenes).

由于它存储在堆中分配的内存中的元素,它有一些开销相对于静态数组。

Since it stores the elements in memory allocated on the heap, it has some overhead in respect to static arrays.

的std ::阵列是一个模板类,封装了静态大小的数组,存储对象本身,这意味着里面,如果你实例化堆栈中的类,阵列本身将在堆栈上。它的大小在编译时(它作为模板参数传递)是已知的,它不能扩大或缩小。

std::array is a template class that encapsulate a statically-sized array, stored inside the object itself, which means that, if you instantiate the class on the stack, the array itself will be on the stack. Its size has to be known at compile time (it's passed as a template parameter), and it cannot grow or shrink.

这比的std ::矢量较为有限,但它往往更有效,特别是对小尺寸,因为实际上它主要是围绕着C数组一个轻量级的包装。但是,它更安全,因为隐式转换为指针被禁用,并提供和其它容器的大部分的std ::矢量的STL相关的功能,所以你可以用STL算法和放大器轻松地使用它;合。总之,对于固定大小的限制非常它比的std ::矢量

It's more limited than std::vector, but it's often more efficient, especially for small sizes, because in practice it's mostly a lightweight wrapper around a C-style array. However, it's more secure, since the implicit conversion to pointer is disabled, and it provides much of the STL-related functionality of std::vector and of the other containers, so you can use it easily with STL algorithms & co. Anyhow, for the very limitation of fixed size it's much less flexible than std::vector.

有关介绍的std ::阵列,看一看的这篇文章;一个简单的介绍的std ::矢量,并给那些可能在它的操作,你可能想看看它的文档

For an introduction to std::array, have a look at this article; for a quick introduction to std::vector and to the the operations that are possible on it, you may want to look at its documentation.


  1. 其实,我认为,在标准,他们在最大的不同操作的复杂性(在固定时间如随机访问,迭代方面比线性时间的所有元素描述,添加和删除的元素结束恒定摊销时间,等),但AFAIK有履行比使用动态阵列其它这种要求的任何其他方法。正如@Lucretiel指出,标准实际上需要的元素被连续存储,所以<青霉>是的动态数组,存储位置相关的分配器如是说。

  1. Actually, I think that in the standard they are described in terms of maximum complexity of the different operations (e.g. random access in constant time, iteration over all the elements in linear time, add and removal of elements at the end in constant amortized time, etc), but AFAIK there's no other method of fulfilling such requirements other than using a dynamic array. As stated by @Lucretiel, the standard actually requires that the elements are stored contiguously, so it is a dynamic array, stored where the associated allocator puts it.

这篇关于的std ::向量与性病::用C ++数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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