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

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

问题描述

我是C ++的新手.我正在阅读通过游戏编程开始C ++",迈克尔·道森(Michael Dawson)着.但是,我一般都不是编程新手.我刚刚完成了有关矢量的章节,所以我对它们在现实世界中的使用提出了疑问(我是计算机科学专业的学生,​​所以我还没有太多的现实经验).

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书籍中学习过向量,但是在我的Comptro简介中根本没有涉及向量.科学班,也没有我在大学的数据结构"班.我也从未见过它们在任何编程任务(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 Colours 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变量的其他代码之前完成构造尝试使用该数据:您最终需要某种方式的Singleton(可能是线程安全的,这甚至会更慢)

  • 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,甚至为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 vector仍可以使用这样的共享内存.

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更改为其他标准容器通常也更容易,并且更安全/更容易应用标准算法(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与现实世界中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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