简体   繁体   English

向量和动态分配数组之间的区别

[英]Difference between a vector and a dynamically allocated array

What are the internal differences of choosing to use an std::vector vs a dynamically allocated array?选择使用std::vector与动态分配数组的内部差异是什么? I mean, not only performance differences like in this matching title question .我的意思是,不仅仅是这个匹配标题问题中的性能差异。

I mean, I try to design a library.我的意思是,我尝试设计一个图书馆。 So I want to offer a wrapper over an StackArray , that is just a C-Style array with some member methods that contains as a member T array[N] .所以我想在StackArray上提供一个包装器,它只是一个C-Style数组,其中包含一些成员方法,其中包含作为成员T array[N] No indirections and the new operator removed to force the implementor to have a array type always stored in the stack.没有间接寻址并删除了new运算符以强制实现者将数组类型始终存储在堆栈中。

Now, I want to offer the dynamic variant.现在,我想提供动态变体。 So, with a little effort, I just can declare something like:所以,稍加努力,我就可以声明如下内容:

template <typename T>
class DynArray
{
    T* array,
    size_t size,
    size_t capacity
};

But... this seems pretty similar to a base approach to a C++ vector .但是......这似乎与C++ vector的基本方法非常相似。

Also, an array stored in the heap can be resized by copying the elements to a new mem location (is this true?).此外,可以通过将元素复制到新的内存位置来调整存储在堆中的数组的大小(这是真的吗?)。 That's pretty much the same that makes a vector when a push_back() operation exceeds its allocated capacity, for example, right?例如,当push_back()操作超出其分配的容量时,这与生成向量几乎相同,对吗?

Should I offer both API's if exists some notable differences?如果存在一些显着差异,我是否应该同时提供这两种 API? Or I am overcomplicated the design of the library and may I just have my StackArray and the Vector should be just the safe abstraction over a dynamically allocated array?或者我把库的设计过于复杂了,我可以只拥有我的StackArrayVector应该只是动态分配数组的安全抽象吗?

First there is a mindset (usually controversial) between the usage of the modern tools that the standard provides and the legacy ones.首先,在使用标准提供的现代工具和遗留工具之间存在一种心态(通常是有争议的)。

You usually must be studing and asking things about C++ modern features, not comparing them with the old ones.您通常必须研究和询问C++现代功能,而不是将它们与旧功能进行比较。 But, for learning purposes, I have to admit that it's quite interesting dive deep somethimes in this topics.但是,出于学习的目的,我不得不承认深入研究这个主题是非常有趣的。

With that in mind, std::vector is a collection that makes much more that just care about the bytes stored in it.考虑到这一点, std::vector是一个集合,它只关心存储在其中的字节。 There is a constraint really important, that the data must lie in contiguous memory , and std::vector ensures this in its internal implementation.有一个非常重要的约束,即数据must lie in contiguous memory中,而std::vector在其内部实现中确保了这一点。 Also, has an already well known, well tested implementation of the RAII pattern, with the correct usage of new[] and delete[] operators.此外,还有一个众所周知的、经过良好测试的RAII模式实现,正确使用new[]delete[]运算符。 You can reserve storage and emplace_abck() elements in a convenient and performant way which makes this collection really unique... there are really a lot of reasons that shows why std::vector is really different from a dynamically allocated array.您可以以一种方便且高效的方式reserve存储和emplace_abck()元素,这使得该集合真正独一无二......确实有很多原因可以说明为什么std::vector与动态分配的数组确实不同。

Not only is to worry about manual memory management, which almost an undesirable thing to do in modern C++ (embedded systems, or operating system themselves are a good point to discuse this last sentence).不仅要担心手动 memory 管理,这在现代 C++ 中几乎是一件不可取的事情(嵌入式系统或操作系统本身是讨论最后一句话的好点)。 It's about to have a tool, std::vector<T> that makes your life as developer easier, specially in a prone-error language like C++ .它即将拥有一个工具, std::vector<T>让您作为开发人员的生活更轻松,特别是在像C++这样容易出错的语言中。

Note: I say error-prone because it's a really hard to master language, which needs a lot of study and training.注意:我说error-prone是因为它是一门非常难掌握的语言,需要大量的学习和训练。 You can make almost everything in the world, and has an incredible amount of features that aren't begginer friendly.您几乎可以制作世界上的所有东西,并且拥有大量对初学者不友好的功能。 Also, the retrocompatibility constraint makes it really bigger, with literally thousand of things that you must care about.此外,逆向兼容性约束使它变得更大,实际上有成千上万个你必须关心的事情。 So, with a great power, always comes a great responsability.所以,能力越大,责任越大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM