可以(或应该)使用C++ std ::其他STL或C样式数组的阵列仪器

发布于 2025-01-22 09:38:02 字数 1108 浏览 0 评论 0 原文

尽管有关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

  1. What are actual practical usage that can be solved by std::array better then other STL or native数组?

  2. 现代C ++编译器可能可以优化所有抽象,从而基本上像天然C数组一样。

  3. 我在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

  1. What are actual practical usage that can be solved by std::array better then other STL or native array?

  2. is it possible that modern C++ compiler can optimize away all the abstraction leaving it basically like native C array.

  3. 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随遇而安 2025-01-29 09:38:02

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 - passing std::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 up boost::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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文