C++ std::array 和 std::vector 有什么区别?你什么时候用一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6632971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between std::array and std::vector? When do you use one over other?
提问by Alok Save
What is the difference between std::array
and std::vector
? When do you use one over other?
std::array
和 和有std::vector
什么区别?你什么时候用一个?
I have always used and considered std:vector
as an C++ way of using C arrays, so what is the difference?
我一直使用并认为std:vector
是使用 C 数组的 C++ 方式,那么有什么区别呢?
回答by Nemo
std::array
is just a class version of the classic C array. That means its size is fixed at compile time and it will be allocated as a single chunk (e.g. taking space on the stack). The advantage it has is slightly better performance because there is no indirection between the object and the arrayed data.
std::array
只是经典 C 数组的类版本。这意味着它的大小在编译时是固定的,它将作为单个块分配(例如在堆栈上占用空间)。它的优点是性能稍好,因为对象和数组数据之间没有间接关系。
std::vector
is a small class containing pointers into the heap. (So when you allocate a std::vector
, it always calls new
.) They are slightly slower to access because those pointers have to be chased to get to the arrayed data... But in exchange for that, they can be resized and they only take a trivial amount of stack space no matter how large they are.
std::vector
是一个包含指向堆的指针的小类。(因此,当您分配 a 时std::vector
,它总是调用new
。)它们的访问速度稍慢,因为必须追逐这些指针才能访问数组数据……但作为交换,它们可以调整大小,并且只需要一点点堆栈空间量,无论它们有多大。
[edit]
[编辑]
As for when to use one over the other, honestly std::vector
is almost always what you want. Creating large objects on the stack is generally frowned upon, and the extra level of indirection is usually irrelevant. (For example, if you iterate through all of the elements, the extra memory access only happens once at the start of the loop.)
至于何时使用一个而不是另一个,老实说std::vector
几乎总是你想要的。在堆栈上创建大对象通常是不受欢迎的,额外的间接级别通常是无关紧要的。(例如,如果您遍历所有元素,则额外的内存访问仅在循环开始时发生一次。)
The vector's elements are guaranteed to be contiguous, so you can pass &vec[0]
to any function expecting a pointer to an array; e.g., C library routines. (As an aside, std::vector<char> buf(8192);
is a great way to allocate a local buffer for calls to read/write
or similar without directly invoking new
.)
向量的元素保证是连续的,所以你可以传递&vec[0]
给任何需要指向数组的指针的函数;例如,C 库例程。(std::vector<char> buf(8192);
顺便说一句,这是一种在read/write
不直接调用 的情况下为调用或类似方法分配本地缓冲区的好方法new
。)
That said, the lack of that extra level of indirection, plus the compile-time constant size, can make std::array
significantly faster for a very small array that gets created/destroyed/accessed a lot.
也就是说,缺少额外的间接级别,加上编译时常量大小,可以std::array
显着加快创建/销毁/访问很多的非常小的数组。
So my advice would be: Use std::vector
unless (a) your profiler tells you that you have a problem and(b) the array is tiny.
所以我的建议是:std::vector
除非(a)你的分析器告诉你有问题并且(b)数组很小,否则使用。
回答by Nicol Bolas
I'm going to assume that you know that std::array is compile-time fixed in size, while std::vector is variable size. Also, I'll assume you know that std::array doesn't do dynamic allocation. So instead, I'll answer whyyou would use std::array instead of std::vector.
我将假设您知道 std::array 是编译时大小固定的,而 std::vector 是可变大小。另外,我假设您知道 std::array 不进行动态分配。所以相反,我会回答为什么你会使用 std::array 而不是 std::vector。
Have you ever found yourself doing this:
你有没有发现自己这样做:
std::vector<SomeType> vecName(10);
And then you never actually increase the size of the std::vector? If so, then std::array is a good alternative.
然后你从来没有真正增加 std::vector 的大小?如果是这样,那么 std::array 是一个不错的选择。
But really, std::array (coupled with initializer lists) exists to make C-style arrays almost entirely worthless. They don't generally compete with std::vectors; they compete more with C-style arrays.
但实际上,std::array(与初始化列表结合)的存在使 C 风格的数组几乎完全没有价值。它们通常不与 std::vectors 竞争;它们与 C 风格的数组竞争更多。
Think of it as the C++ committee doing their best to kill off almost all legitimate use of C-style arrays.
可以将其视为 C++ 委员会竭尽全力消除几乎所有 C 样式数组的合法使用。
回答by Luc Danton
std::array
std::array
- is an aggregate
- is fixed-size
- requires that its elements be default constructible (vs copy (C++03) or move (C++0x) constructible)
- is linearly swappable (vs constant time)
- is linearly movable (vs constant time)
- potentially pays one less indirection than
std::vector
- 是一个聚合
- 是固定大小
- 要求其元素是默认可构造的(vs 复制(C++03)或移动(C++0x)可构造)
- 是线性可交换的(相对于恒定时间)
- 可线性移动(相对于恒定时间)
- 可能比
std::vector
A good use case is when doing things 'close-to-the-metal', while keeping the niceties of C++ and keeping all the bad things of raw arrays out of the way.
一个很好的用例是在做“接近金属”的事情时,同时保持 C++ 的优点并避免原始数组的所有不好的东西。
回答by Xeo
回答by Etienne de Martel
std::array
has a fixed (compile time) size, while std::vector
can grow.
std::array
具有固定(编译时间)大小,但std::vector
可以增长。
As such, std::array
is like using a C array, while std::vector
is like dynamically allocating memory.
因此,std::array
就像使用 C 数组,而std::vector
就像动态分配内存。
回答by Xiang
I use my own personal hand coded Array<>
template class, which has a simpler API compared with std::array
or std::vector
. For example:
我使用我自己的个人手工编码Array<>
模板类,与std::array
or相比,它具有更简单的 API std::vector
。例如:
To use a dynamic Array:
使用动态数组:
Array<> myDynamicArray; // Note array size is not given at compile time
myDynamicArray.resize(N); // N is a run time value
...
To use a static Array, fixed size at compile time:
要使用静态数组,在编译时固定大小:
Array<100> myFixedArry;
I believe it has a better syntax than std::array
, or std::vector
. Also extremely efficient.
我相信它比std::array
, or有更好的语法std::vector
。也极其高效。