尽管有关performance \ beastion \ std ::数组vs std :: vector或c样式数组的属性有多个问题,但我仍然从未有关于
可以使用的实际实际用法和类型可以通过STD :: Array解决)。我要问的原因是因为我本人和在C ++工作的所有项目中,我从未见过任何STD ::数组的用法,但是大多数使用STD :: vector完成的工作是有些情况,是我们使用的情况: Deque。
在编写属性之后,似乎是中间的数组,就像C样式数组一样(不那么薄 - 具有大量的API )抽象层,但不像STD :: vector那样重。觉得表现真的很重要,或者由于嵌入式设备而有限制,我仍然会使用C样式数组。
我试图比较其他一些相对较大的C ++项目,例如, and they very seldom use std::array only in json.hpp which is third party of its own, so to summarize
-
What are actual practical usage that can be solved by std::array better then other STL or native数组?
-
现代C ++编译器可能可以优化所有抽象,从而基本上像天然C数组一样。
-
我在json.hpp中注意到了Notepad ++源代码STD :: Array与Char_traits和模板一起使用的dray使用的sTD ::数组可用于实现其他正在使用模板和模板meta-emplate meta-的库来实现在本机C数组中进行大量编程,需要更多具有性能罚款的适配器或解决方法?
编辑:我了解STD ::数组属性(如固定尺寸和尺寸)是已知的,但是为什么仍然很少使用它。
多谢 :)
Despite the multiple questions about performance\benefits\properties of std::array vs std::vector or C style array, I still never got a clear answer about
actual practical usage and types of problems that can be (and should be solved with std::array). the reason i am asking is because me myself and in all the projects that i was working in C++ i never saw any usage of std::array but most of the work done with std::vector, is some are cases we used std:deque.
After compering the properties seems like array is something in the middle that like a C style array that have a thin(not so thin - have a lot of APIs) layer of abstraction but not heavy as std::vector. feel like if performance really matter or i have limitation because of embedded device i would still use C style array.
I tried to compare some other relatively large C++ project for example Notepad++ source code, and they very seldom use std::array only in json.hpp which is third party of its own, so to summarize
-
What are actual practical usage that can be solved by std::array better then other STL or native array?
-
is it possible that modern C++ compiler can optimize away all the abstraction leaving it basically like native C array.
-
I noticed in json.hpp that used by notepad++ source code that std::array used with char_traits and templates, is it possible that std::array can be used in implementation of other Libraries that are using templates and template meta-programming heavily while native C array would require more adapters or workaround that have performance penalty?
EDIT: I understand that std::array properties like fixed size and the sizes is known, but why it is still relatively seldom used.
Thanks a lot :)
发布评论
评论(1)
This question is somewhat broad and seems likely to go down the opinionated route, still:
ad.1
std ::数组
为C风格数组提供类似于STL的API。此外,它可以防止阵列到指针衰减。后者通常是需要的,例如,由于传递时会保留大小信息。AD.2
至于一般情况 - 是的,
std ::数组
是其C风格对应物周围的简单薄包装器。对于特定情况 - 传递std ::数组
按值可能会导致副本,而对于C风格数组而言,这种情况并非如此,因为简单地不能按值传递。另一方面,这也提供了更一致的接口。通常 - 如果您担心性能 - 衡量。
我敢说,在大多数情况下,这根本无关紧要,如果需要静态数组的形式,
std :: Array
应该是C风格的首选类型,请参阅 c ++核心指南在这个主题上。AD.3
不确定我跟随。再次:什么
std ::数组
是,大多是围绕C风格的便利包装。如果您对血腥的详细信息感兴趣,则可以查找boost :: Array
,它应该非常接近STD。没错,使用C风格阵列可能需要更多的修补,或者仅编写与
std :: Array等效的STH
类模板。至于潜在的性能较低的解决方法,我能想到的是矢量或其他形式的动态分配存储。请注意,我写了“潜在的”:当代编译器被允许进行堆肥操作,因此再次 - 首先衡量,以后进行审判。
此外,数组与矢量困境通常降低到固定尺寸和可分解的尺寸,即程序员的意图。当然,此规则有很多例外,但是对于最一般的情况,我选择先表现力。
This question is somewhat broad and seems likely to go down the opinionated route, still:
ad.1
std::array
provides STL-like API for C-style array. Moreover it prevents the array to pointer decay. The latter is oftentimes desired, e.g. due to the fact that size information is preserved when passing it.ad.2
As for a general case - yes,
std::array
is a simple thin wrapper around its C-style counterpart. For specific cases - passingstd::array
by value can cause copies, which won't be the case for C-style array as the simply cannot be passed by value. On the other hand, this also provides a more consistent interface.Generally - if you are concerned about performance - measure.
I dare say, in most cases it won't matter at all and if a form of static array is needed,
std::array
should be a go-to type over the C-style one, see C++ core guidelines on that topic.Ad.3
Not sure I follow. Again: what
std::array
is, is mostly a convenience wrapper around C-style one. If you're interested in the gory details, you can look upboost::array
, it should be pretty close to the std one.True, using C-style arrays would probably require more tinkering or just writing sth equivalent to the
std::array
class template. As for potentially less performant workarounds, the main one I can think of is vector or some other form of dynamically allocated storage.Note, I wrote "potentially": contemporary compilers are allowed to elide heap operations, so again - measure first, judge later.
Also, the array vs vector dilemma often goes down to fixed-size vs. resizable , i.e. the intent of the programmer. Of course there is a plethora of exceptions to this rule, but for a most general case I'd opt for being expressive first.