简体   繁体   English

C ++中的STL容器实际上是如何实现的..

[英]How are STL containers in C++ actually implemented..

I get asked about this a lot. 我被问到这个问题很多。 I have read various sources including the book from Bjarne Stroustrup. 我读过各种来源,包括Bjarne Stroustrup的书。 It says that the standard does not mandate any implementation for the STL containers. 它表示该标准并未要求对STL容器进行任何实施。 But I am still not clear. 但我还不清楚。 If there is no mandate on the implementation, does it mean that when using a vector or a list in my code , it may use a different data structures at different times?? 如果实现没有授权,是否意味着在我的代码中使用向量或列表时,它可能在不同的时间使用不同的数据结构? And if it is the case, how is it decided which data structure will be used at which time?? 如果是这样的话,如何决定在哪个时间使用哪种数据结构? I mean should it not be fixed that all STL containers will use a specific DS everytime?? 我的意思是,如果不修复所有STL容器每次都会使用特定的DS?

该标准没有规定应该使用哪些数据结构来实现STL容器,但它确实提供了复杂性保证,有时还提供其他保证(如使用连续内存的vector ),这通常会限制合理使用的数据结构的选择。实施少数选择。

it may use a different data structures at different times? 它可能在不同的时间使用不同的数据结构?

Yes, it may. 是的,可能。 But we need to clearly define a "different time". 但我们需要明确定义“不同的时间”。 If you compile it for platform P with standard library implementation I, then it will use the same data structure for as long as this program is run. 如果使用标准库实现I为平台P编译它,那么只要运行该程序,它将使用相同的数据结构。 If you change P or I and recompile, then another data structure may be used. 如果更改P或I并重新编译,则可以使用其他数据结构。 "Different times" are separated by builds. “不同时间”由构建分隔。

how is it decided which data structure will be used at which time? 如何决定在哪个时间使用哪种数据结构?

The standard imposes asymptotic complexity requirements, so that's a big criteria right there. 该标准强加了渐近复杂性要求,因此这是一个很大的标准。 Beyond that, an implementer may choose between different data structures that meet those requirements. 除此之外,实现者可以在满足这些要求的不同数据结构之间进行选择。 Since implementers are also aware of platforms, they may choose a data structure that behaves better on the particular platform you are building on. 由于实施者也了解平台,因此他们可能会选择在您构建的特定平台上表现更好的数据结构。

I mean should it not be fixed that all STL containers will use a specific DS everytime? 我的意思是,如果不修复所有STL容器每次都会使用特定的DS吗?

No. What if someone comes up with a really cool and efficient data structure tomorrow, one that can completely meet and exceed the requirements of some standard library container? 不会。如果有人明天提出一个非常酷和高效的数据结构,那么可以完全满足并超出某些标准库容器的要求呢? If the data structure is set fixed in the specification, instead of the complexity, it would not be allowed to use this better implementation. 如果数据结构在规范中设置固定,而不是复杂性,则不允许使用这种更好的实现。 Not until the C++ standard is perhaps updated, a process that can take years. 直到C ++标准可能更新,这个过程可能需要数年时间。

Software engineering is about balancing specification and abstraction. 软件工程是关于平衡规范和抽象。 You must specify what is required to solve a problem, and abstract the details that are not relevant to the solution. 您必须指定解决问题所需的内容,并抽象出与解决方案无关的详细信息。 If you over specify, you lose flexibility. 如果过度指定,则会失去灵活性。 But be careful not to over abstract and lose performance. 但要注意不要过度抽象而失去性能。 The standard library is a great case study of an attempt to balance the two. 标准库是一个很好的案例研究,试图平衡两者。

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

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