C++ std::vector vs 现实世界中的数组 [英] C++ std::vector vs array in the real world

查看:30
本文介绍了C++ std::vector vs 现实世界中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 C++ 的新手.我正在阅读通过游戏编程开始 C++"通过迈克尔道森.但是,我对编程并不陌生.我刚刚完成了处理向量的一章,所以我有一个关于它们在现实世界中使用的问题(我是一名计算机科学专业的学生,​​所以我还没有太多的现实世界经验).

I'm new to C++. I'm reading "Beginning C++ Through Game Programming" by Michael Dawson. However, I'm not new to programming in general. I just finished a chapter that dealt with vectors, so I've got a question about their use in the real world (I'm a computer science student, so I don't have much real-world experience yet).

作者在每一章的末尾都有一个问答,其中一个是:

The author has a Q/A at the end of each chapter, and one of them was:

问:我什么时候应该使用向量而不是数组?

Q: When should I use a vector instead of an array?

A:几乎总是.矢量是高效且灵活的.它们确实需要比数组多一点的内存,但这种权衡几乎总是值得的.

A: Almost always. Vectors are efficient and flexible. They do require a little more memory than arrays, but this tradeoff is almost always worth the benefits.

大家怎么看?我记得在一本 Java 书中学习了向量,但我们在 Comp 的介绍中根本没有涉及它们.科学.课,也不是我在大学的数据结构课.我也从未见过它们用于任何编程作业(Java 和 C).这让我觉得它们并没有被广泛使用,尽管我知道学校代码和现实世界的代码可能有很大的不同.

What do you guys think? I remember learning about vectors in a Java book, but we didn't cover them at all in my Intro to Comp. Sci. class, nor my Data Structures class at college. I've also never seen them used in any programming assignments (Java and C). This makes me feel like they're not used very much, although I know that school code and real-world code can be extremely different.

我不需要被告知这两种数据结构之间的区别;我非常了解他们.我只想知道作者是否在 Q/A 中给出了很好的建议,或者他是否只是试图让初学者避免因管理固定大小数据结构的复杂性而毁了自己.此外,无论您对作者的建议有何看法,您在现实世界中看到什么?

I don't need to be told about the differences between the two data structures; I'm very aware of them. All I want to know is if the author is giving good advice in his Q/A, or if he's simply trying to save beginner programmers from destroying themselves with complexities of managing fixed-size data structures. Also, regardless of what you think of the author's advice, what do you see in the real-world more often?

推荐答案

A:几乎总是[使用向量而不是数组].矢量是高效且灵活的.它们确实需要比数组多一点的内存,但这种权衡几乎总是值得的.

A: Almost always [use a vector instead of an array]. Vectors are efficient and flexible. They do require a little more memory than arrays, but this tradeoff is almost always worth the benefits.

这过于简单化了.使用数组相当普遍,并且在以下情况下可能很有吸引力:

That's an over-simplification. It's fairly common to use arrays, and can be attractive when:

  • 元素是在编译时指定的,例如const char project[] = "Super Server";, const Colors colours[] = { Green, Yellow };

  • 在 C++11 中,使用值初始化 std::vector 将同样简洁

元素的数量本质上是固定的,例如const char* const bool_to_str[] = { "false", "true" };, Piece chess_board[8][8];

the number of elements is inherently fixed, e.g. const char* const bool_to_str[] = { "false", "true" };, Piece chess_board[8][8];

首次使用性能至关重要:对于常量数组,编译器通常可以将完全预初始化的对象的内存快照写入可执行映像,然后将其直接进行页面错误处理以备使用,因此,运行时堆分配 (new[]) 通常要快得多,然后是对象的序列化构造

first-use performance is critical: with arrays of constants the compiler can often write a memory snapshot of the fully pre-initialised objects into the executable image, which is then page-faulted directly into place ready for use, so it's typically much faster that run-time heap allocation (new[]) followed by serialised construction of objects

  • 编译器生成的 const 数据表总是可以安全地被多个线程读取,而在运行时构造的数据必须在非功能构造函数触发的其他代码之前完成构造 -本地 static 变量尝试使用该数据:您最终需要某种单例模式(可能是线程安全的,但速度会更慢)

  • compiler-generated tables of const data can always be safely read by multiple threads, whereas data constructed at run-time must complete construction before other code triggered by constructors for non-function-local static variables attempts to use that data: you end up needing some manner of Singleton (possibly threadsafe which will be even slower)

在 C++03 中,以初始大小创建的 vector 将构造一个原型元素对象,然后复制构造每个数据成员.这意味着即使对于故意将构造保留为无操作的类型,复制数据元素仍然存在成本 - 复制它们在内存中留下的任何垃圾值.显然,未初始化元素的数组更快.

In C++03, vectors created with an initial size would construct one prototypical element object then copy construct each data member. That meant that even for types where construction was deliberately left as a no-operation, there was still a cost to copy the data elements - replicating their whatever-garbage-was-left-in-memory values. Clearly an array of uninitialised elements is faster.

C++ 的强大功能之一是,您通常可以编写一个 class(或 struct)来精确模拟特定协议所需的内存布局,然后将类指针指向您需要使用的内存,以方便地解释或分配值.无论好坏,许多此类协议通常嵌入固定大小的小型数组.

One of the powerful features of C++ is that often you can write a class (or struct) that exactly models the memory layout required by a specific protocol, then aim a class-pointer at the memory you need to work with to conveniently interpret or assign values. For better or worse, many such protocols often embed small fixed sized arrays.

在结构/类的末尾放置一个包含 1 个元素的数组(如果您的编译器允许它作为扩展,甚至是 0),这是一个有几十年历史的技巧,目的是指向某个结构类型的指针更大的数据区域,并根据内存可用性和内容的先验知识(如果在写入之前阅读)访问结构末尾的数组元素 - 请参阅零元素数组需要什么?

There's a decades-old hack for putting an array of 1 element (or even 0 if your compiler allows it as an extension) at the end of a struct/class, aiming a pointer to the struct type at some larger data area, and accessing array elements off the end of the struct based on prior knowledge of the memory availability and content (if reading before writing) - see What's the need of array with zero elements?

包含数组的类/结构仍然可以是 POD 类型

classes/structures containing arrays can still be POD types

数组有助于从多个进程访问共享内存(默认情况下,vector 的内部指针指向实际动态分配的数据不会在共享内存中或跨进程有意义,并且即使在指定自定义分配器模板参数时,也很难强制 C++03 vectors 像这样使用共享内存).

arrays facilitate access in shared memory from multiple processes (by default vector's internal pointers to the actual dynamically allocated data won't be in shared memory or meaningful across processes, and it was famously difficult to force C++03 vectors to use shared memory like this even when specifying a custom allocator template parameter).

嵌入数组可以本地化内存访问要求,提高缓存命中率,从而提高性能

embedding arrays can localise memory access requirement, improving cache hits and therefore performance

也就是说,如果使用 vector(在代码简洁、可读性或性能方面)不是一种积极的痛苦,那么你最好这样做:他们有 size()、通过 at() 检查随机访问、迭代器、调整大小(随着应用程序成熟",这通常变得必要)等.从 vector<更改通常也更容易/code> 到其他一些标准容器,如果有需要,更安全/更容易应用标准算法(x.end()x + sizeof x/sizeof x[0] 任何一天).

That said, if it's not an active pain to use a vector (in code concision, readability or performance) then you're better off doing so: they've size(), checked random access via at(), iterators, resizing (which often becomes necessary as an application "matures") etc.. It's also often easier to change from vector to some other Standard container should there be a need, and safer/easier to apply Standard algorithms (x.end() is better than x + sizeof x / sizeof x[0] any day).

更新:C++11 引入了一个 std::array<>,它避免了 vector 的一些成本——内部使用固定大小的数组避免额外的堆分配/释放 - 同时提供一些好处和 API 功能:http://en.cppreference.com/w/cpp/container/array.

UPDATE: C++11 introduced a std::array<>, which avoids some of the costs of vectors - internally using a fixed-sized array to avoid an extra heap allocation/deallocation - while offering some of the benefits and API features: http://en.cppreference.com/w/cpp/container/array.

这篇关于C++ std::vector vs 现实世界中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆