使用阵列而不是std的优点::矢量?

时间:2021-06-25 16:10:16

I'm currently seeing a lot of questions which are tagged C++ and are about handling arrays.
There even are questions which ask about methods/features for arrays which a std::vector would provide without any magic.

目前我看到很多问题都被标记为c++,并且是关于处理数组的。甚至还有关于数组的方法/特性的问题,std::vector不会提供任何魔法。

So I'm wondering why so much developers are chosing arrays over std::vector in C++?

所以我想知道为什么这么多的开发人员在c++中选择std::vector数组?

5 个解决方案

#1


9  

In general, I strongly prefer using a vector over an array for non-trivial work; however, there are some advantages of arrays.

一般来说,对于非平凡工作,我更喜欢使用向量而不是数组;然而,数组也有一些优点。

  • arrays are slightly more compact: the size is implicit
  • 数组稍微紧凑一些:大小是隐式的。
  • arrays are non-resizable; sometimes this is desireable
  • 数组是non-resizable;有时这有必要
  • arrays don't require parsing extra STL headers (compile time)
  • 数组不需要解析额外的STL头(编译时)
  • it can be easier to interact with straight-C code with an array (e.g. if C is allocating and C++ is using)
  • 可以更容易地使用数组与直接C代码进行交互(例如,如果C正在分配,而c++正在使用)
  • fixed-size arrays can be embedded directly into a struct or object, which can improve memory locality and reducing the number of heap allocations needed
  • 固定大小的数组可以直接嵌入到struct或对象中,这可以改善内存位置并减少所需的堆分配数量。

#2


4  

Because C++03 has no vector literals. Using arrays can sometime produce more succinct code.

因为c++ 03没有向量。使用数组有时可以生成更简洁的代码。

Compared to array initialization:

相对于数组初始化:

char arr[4] = {'A', 'B', 'C', 'D'};

vector initialization can look somewhat verbose

向量初始化看起来有点冗长

std::vector<char> v;
v.push_back('A');
v.push_back('B');
...

#3


2  

I'd go for std::array available in C++0x instead of plain arrays which can also be initialized like standard arrays with an initializer list

我将使用c++ 0x中可用的std::数组,而不是普通数组,普通数组也可以像标准数组一样通过初始化器列表进行初始化

http://www2.research.att.com/~bs/C++0xFAQ.html#std-array

http://www2.research.att.com/ bs / c++ 0 xfaq.html # std-array

#4


1  

I think this is because a lot of C++ programmers come from C and don't yet understand the advantages of using vector and all the extra STL goodies that come for free with its containers.

我认为这是因为许多c++程序员来自C,他们还不了解使用vector的好处,也不了解免费使用其容器所带来的所有额外的STL好处。

#5


1  

You have much more control with arrays

您可以使用数组进行更多的控制

How about:

如何:

1) you are dealing with colossal data sets where the data has to be mapped files and not allocated with malloc or new because of it's size. Under this scenario worrying about what to do if you didn't reserve enough address space at the beginning may be moot, though I suppose you could unmap - extend - remap the file, unless prohibited by address fragmentation or my second point.

1)您正在处理庞大的数据集,其中数据必须被映射到文件中,并且由于数据的大小而不能分配给malloc或new。在这种情况下,如果在开始时没有预留足够的地址空间,那么担心该怎么做可能是没有意义的,尽管我认为您可以取消映射——扩展——重新映射文件,除非地址分段或我的第二点禁止。

2) Code that uses lockless multiprocessing. Performance hits of stopping the threads for re-allocation (or any other "STL goodie") may be unacceptable, hence use arrays, you have got much more control, you may need to call a lot of functions to pause other threads before you resize anything.

2)使用无锁多处理的代码。停止线程进行重新分配(或任何其他“STL goodie”)的性能打击可能是不可接受的,因此使用数组,您有了更多的控制,您可能需要调用许多函数来暂停其他线程,然后再调整大小。

BTW, I'm usually dealing with 1 and 2 at the same time. Arrays of structures + pointers work wonderfully, Compiling with C++ because you you can still use some C++ features elsewhere in the code. I'm sure I could think of many more examples if I tried hard enough

顺便说一句,我通常同时处理1和2。结构+指针的数组非常有用,使用c++编译,因为您仍然可以在代码的其他地方使用c++特性。如果我足够努力的话,我肯定能想出更多的例子

#1


9  

In general, I strongly prefer using a vector over an array for non-trivial work; however, there are some advantages of arrays.

一般来说,对于非平凡工作,我更喜欢使用向量而不是数组;然而,数组也有一些优点。

  • arrays are slightly more compact: the size is implicit
  • 数组稍微紧凑一些:大小是隐式的。
  • arrays are non-resizable; sometimes this is desireable
  • 数组是non-resizable;有时这有必要
  • arrays don't require parsing extra STL headers (compile time)
  • 数组不需要解析额外的STL头(编译时)
  • it can be easier to interact with straight-C code with an array (e.g. if C is allocating and C++ is using)
  • 可以更容易地使用数组与直接C代码进行交互(例如,如果C正在分配,而c++正在使用)
  • fixed-size arrays can be embedded directly into a struct or object, which can improve memory locality and reducing the number of heap allocations needed
  • 固定大小的数组可以直接嵌入到struct或对象中,这可以改善内存位置并减少所需的堆分配数量。

#2


4  

Because C++03 has no vector literals. Using arrays can sometime produce more succinct code.

因为c++ 03没有向量。使用数组有时可以生成更简洁的代码。

Compared to array initialization:

相对于数组初始化:

char arr[4] = {'A', 'B', 'C', 'D'};

vector initialization can look somewhat verbose

向量初始化看起来有点冗长

std::vector<char> v;
v.push_back('A');
v.push_back('B');
...

#3


2  

I'd go for std::array available in C++0x instead of plain arrays which can also be initialized like standard arrays with an initializer list

我将使用c++ 0x中可用的std::数组,而不是普通数组,普通数组也可以像标准数组一样通过初始化器列表进行初始化

http://www2.research.att.com/~bs/C++0xFAQ.html#std-array

http://www2.research.att.com/ bs / c++ 0 xfaq.html # std-array

#4


1  

I think this is because a lot of C++ programmers come from C and don't yet understand the advantages of using vector and all the extra STL goodies that come for free with its containers.

我认为这是因为许多c++程序员来自C,他们还不了解使用vector的好处,也不了解免费使用其容器所带来的所有额外的STL好处。

#5


1  

You have much more control with arrays

您可以使用数组进行更多的控制

How about:

如何:

1) you are dealing with colossal data sets where the data has to be mapped files and not allocated with malloc or new because of it's size. Under this scenario worrying about what to do if you didn't reserve enough address space at the beginning may be moot, though I suppose you could unmap - extend - remap the file, unless prohibited by address fragmentation or my second point.

1)您正在处理庞大的数据集,其中数据必须被映射到文件中,并且由于数据的大小而不能分配给malloc或new。在这种情况下,如果在开始时没有预留足够的地址空间,那么担心该怎么做可能是没有意义的,尽管我认为您可以取消映射——扩展——重新映射文件,除非地址分段或我的第二点禁止。

2) Code that uses lockless multiprocessing. Performance hits of stopping the threads for re-allocation (or any other "STL goodie") may be unacceptable, hence use arrays, you have got much more control, you may need to call a lot of functions to pause other threads before you resize anything.

2)使用无锁多处理的代码。停止线程进行重新分配(或任何其他“STL goodie”)的性能打击可能是不可接受的,因此使用数组,您有了更多的控制,您可能需要调用许多函数来暂停其他线程,然后再调整大小。

BTW, I'm usually dealing with 1 and 2 at the same time. Arrays of structures + pointers work wonderfully, Compiling with C++ because you you can still use some C++ features elsewhere in the code. I'm sure I could think of many more examples if I tried hard enough

顺便说一句,我通常同时处理1和2。结构+指针的数组非常有用,使用c++编译,因为您仍然可以在代码的其他地方使用c++特性。如果我足够努力的话,我肯定能想出更多的例子