应始终选择C数组上的容器 [英] Should always choose containers over C arrays
问题描述
我读过这篇文章: https://isocpp.org/wiki/faq/containers# array-are-evil [ ^ ]
我是否几乎总是在标准C阵列上使用C ++容器?
另一个问题,作为一个新手,我想询问是否vector之类的容器总是将它们的元素放在堆上?因此不需要new和delete []?
没有理由使用C样式数组,除非你使用的是C ++'98或'03编译器。您考虑使用内置阵列的任何地方(即,您希望O(1)复杂度随机可访问的东西)使用代码> vector< t>如果你想要它可调整大小和数组< t,n>
否则。
大多数容器使用堆但是它们不必。我见过的array< t,n>
的每个实现都不使用堆,并且至少有一个vector< t><的实现。 / t>
我见过对小型物体的低计数向量进行了小矢量优化,这些小型物体没有靠近堆。一般来说,如果你假设一个容器(除了array< t,n>
)使用堆,你就不会出错。
< blockquote>马匹的课程...一般来说,stl容器更好(IMO)*只要你选择合适的工作正确的工作*即了解deque和vector之间的行为差异
是 - 他们堆积 - 如果你打算将它们用于课程,理解复制构造函数
我有点迟了但是...... 。
提供的wiki链接有一些不好的例子。不好的是它们不能用于STL向量或数组。
维基文章还声称由于C阵列不知道它的大小,可以没有数组赋值。这是非常短视的。
数组是C中的指针。如果数组的声明是范围内的,则sizeof运算符可以推导出数组计数。不幸的是,这不能在函数调用中存活。
静态 void 悲伤( const int array [ 30 ])
{
/ * 这不符合你的想法。 * /
printf( 数组大小为%d, sizeof array );
}
解决这个问题的一种方法是将数组声明包装在struct中。只要结构定义在范围内,sizeof运算符就可以工作,复制赋值也是如此。
struct buffer
{
int array [ 30 ];
};
静态 void 幸福( const buffer * buf)
{
printf( 数组大小为%d , sizeof buf-> array);
}
也就是说,如果你真的需要可变大小的数组 - 一个STL向量是要走的路。我经常将STL向量类成员用于数组,因此类析构函数可以为我清理以避免内存泄漏。
在某些时候你可能会发现你自己挑战在大数据/向量中处理大量数据时会挤出更多性能。
使用裸指针的STL数组运算符较慢,所以我经常发现自己抓住了一个指向数据的指针,并在计算密集循环中使用它。
I've read this article: https://isocpp.org/wiki/faq/containers#arrays-are-evil[^]
Should I almost always use C++ containers overs standard C arrays?
Another question, as a novice, I want ask whether containers such as vector always put their elements on the heap? thus there's no need for new and delete[]?
There's no reason to use a C style array unless you're using a C++'98 or '03 compiler. Anywhere you'd consider using a built in array (i.e. you want something that's O(1) complexity random accessible) use code>vector<t> if you want it resizable andarray<t,n>
otherwise.
Most containers use the heap but they don't have to. Every implementation I've seen ofarray<t,n>
doesn't use the heap and at least one implementation ofvector<t></t>
I've seen had a small vector optimisation for low count vectors of small objects that didn't go near the heap. Generally though if you assume a container (apart fromarray<t,n>
) uses the heap you won't go far wrong.
horses for courses ... generally tho, stl containers are better (IMO) *as long as you choose the right one for the right job* i.e. understand the difference in behaviour between a deque and a vector
yes - they heapify - if you're going to use them for classes, understand the copy constructor
I'm a bit late but ...
The wiki link provided has some bad examples. Bad in that they wouldn't work for either STL vectors or arrays.
The wiki article also claims "Since a C array doesn't know its size, there can be no array assignment". That's very short-sighted.
An array is a pointer in C. If the array's declaration is in-scope, the sizeof operator can deduce the array count. Unfortunately this does not survive a function call.
static void sadness(const int array[30]) { /* this doesn't do what you think. */ printf("array size is %d", sizeof array); }
One way around this is to wrap the array declaration inside a struct. As long as the struct definition is in-scope, the sizeof operator will work, as will copy assignment.
struct buffer { int array[30]; }; static void happiness(const buffer *buf) { printf("array size is %d", sizeof buf->array); }
That said, if you truly need variable sized arrays - an STL vector is the way to go. I often use STL vector class members for arrays just so the class destructor can take care of clean-up for me to avoid memory leaks.
At some point you may find yourself challenged to squeeze more performance when crunching lots of data in large arrays / vectors.
The STL array operator is slower that using a bare pointer so I often find myself grabbing a pointer to the data and using that inside a compute intensive loop.
这篇关于应始终选择C数组上的容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!