在现实世界中的C ++ STL向量VS阵 [英] C++ STL vector vs array in the real world

查看:94
本文介绍了在现实世界中的C ++ STL向量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).

作者有在每章的最后一Q / A,其中之一是:

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: 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书籍的载体,但我们并没有在我的简介比较覆盖它们。科学。一流的,也不是我的数据结构的大专班。我还从来没有见过他们在任何编程作业(Java和C)使用。这让我觉得他们没有用得非常多,虽然我知道那个学校code和真实世界的code可以是非常不同的。

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?

谢谢,

杰拉德

推荐答案

一个最好的理由使用矢量,而不是一个数组是在 RAII 成语。基本上,为了使C ++ code是异常安全的,任何动态分配的内存或其他资源应该对象中封装。这些对象应该有一个释放这些资源的析构函数。

One of the best reasons to use a vector as opposed to an array is the RAII idiom. Basically, in order for c++ code to be exception-safe, any dynamically allocated memory or other resources should be encapsulated within objects. These objects should have destructors that free these resources.

当一个异常得不到处理,被gaurenteed被调用的唯一的东西是在栈上对象的析构函数。如果动态对象之外分配内存,并删除之前未捕获的异常某处抛出,你有内存泄漏。

When an exception goes unhandled, the ONLY things that are gaurenteed to be called are the destructors of objects on the stack. If you dynamically allocate memory outside of an object, and an uncaught exception is thrown somewhere before it is deleted, you have a memory leak.

这也是不必记住用一种很好的方式删除

It's also a nice way to avoid having to remember to use delete.

您也应该检查的std ::算法,它提供了很多通用算法为矢量等STL容器。

You should also check out std::algorithm, which provides a lot of common algorithms for vector and other STL containers.

我有写code与矢量的是,现在回想起来,也许就已经与本地阵列好几次。但在所有这些情况下,无论是的boost :: multi_array的突击::阵列本来好过它们中的。

I have on a few occasions written code with vector that, in retrospect, probably would have been better with a native array. But in all of these cases, either a Boost::multi_array or a Blitz::Array would have been better than either of them.

这篇关于在现实世界中的C ++ STL向量VS阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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