“STL”和“C++标准库”有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5205491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:40:00  来源:igfitidea点击:

What's the difference between "STL" and "C++ Standard Library"?

c++stlstdc++-standard-libraryc++-faq

提问by Pieter

Someone brought this articleto my attention that claims (I'm paraphrasing)the STL term is misused to refer to the entire C++ Standard Library instead of the parts that were taken from SGI STL.

有人让我注意到这篇文章声称(我正在解释)STL 术语被误用于指代整个 C++ 标准库,而不是取自 SGI STL 的部分。

(...) it refers to the "STL", despite the fact that very few people still use the STL (which was designed at SGI).

Parts of the C++ Standard Library were based on parts of the STL, and it is these parts that many people (including several authors and the notoriously error-ridden cplusplus.com) still refer to as "the STL". However, this is inaccurate; indeed, the C++ standard never mentions "STL", and there are content differences between the two.

(...) "STL" is rarely used to refer to the bits of the stdlib that happen to be based on the SGI STL. People think it's the entire standard library. It gets put on CVs. And it is misleading.

(...) 它指的是“STL”,尽管事实上很少有人仍然使用 STL(它是在 SGI 设计的)。

C++ 标准库的部分内容基于 STL 的部分内容,许多人(包括几位作者和臭名昭著的错误缠身的 cplusplus.com)仍然将这些部分称为“STL”。然而,这是不准确的;事实上,C++ 标准从未提及“STL”,两者之间存在内容差异。

(...) “STL”很少用于指碰巧基于 SGI STL 的 stdlib 位。人们认为它是整个标准库。它被放在简历上。这是误导。

I hardly know anything about C++'s history so I can't judge the article's correctness. Should I refrain from using the term STL? Or is this an isolated opinion?

我对 C++ 的历史几乎一无所知,所以我无法判断文章的正确性。我应该避免使用术语 STL 吗?或者这是一个孤立的意见?

回答by Lightness Races in Orbit

The "STL" was written by Alexander Stepanovin the days long before C++ was standardised. C++ existed through the 80s, but what we now call "C++" is the language standardised in ISO/IEC 14882:2014 (and earlier versions, such as ISO/IEC 14882:2011).

在 C++ 标准化之前很久,“STL”是由 Alexander Stepanov 编写的。C++ 存在于 80 年代,但我们现在所说的“ C++”是 ISO/IEC 14882:2014(以及更早的版本,例如 ISO/IEC 14882:2011)中标准化的语言。

The STL was already widely used as a library for C++, giving programmers access to containers, iterators and algorithms. When the standardisation happened, the language committee designed parts ofthe C++ Standard Library (which is part of the language standard) to veryclosely match the STL.

STL 已经被广泛用作 C++ 的库,让程序员可以访问容器、迭代器和算法。当发生标准化,语言委员会设计的部分的C ++标准库(这是语言标准的一部分),以非常STL的密切配合。

Over the years, many people — including prominent book authors, and various websites — have continued to refer to the C++ Standard Library as "the STL", despite the fact that the two entities are separate and that there are some differences. These differences are even more pronounced in the upcoming new C++ standard, which includes various features and significantly alters some classes.

多年来,许多人——包括著名的书籍作者和各种网站——继续将 C++ 标准库称为“STL”,尽管这两个实体是分开的并且存在一些差异。这些差异在即将到来的新 C++ 标准中更加明显,其中包括各种特性并显着改变了一些类。

The original STL is now often called "an implementation of the C++ Standard Template Library" (rather backwards to actual history!), in the same way that your Microsoft Visual Studio or GCC ships an implementation of the C++ Standard Library. But the "Standard Template Library" and the "Standard Library" are not the same thing.

最初的 STL 现在通常被称为“C++ 标准模板库的实现”(而不是真正的历史!),就像您的 Microsoft Visual Studio 或 GCC 提供 C++ 标准库的实现一样。但是“标准模板库”和“标准库”不是一回事。

The battle is about whether the current Standard Library should be called "the STL" in whole or in part, and/or whether it matters what it's called.

争论的焦点是当前的标准库是否应该全部或部分称为“STL”,和/或它的名称是否重要。

For "STL"

对于“STL”

There is a school of thought that says that everybody knows now that "STL" means the standard library, just as everybody now knows that "C++" is the ISO-standardised language.

有一种学派认为现在每个人都知道“STL”意味着标准库,就像现在每个人都知道“C++”是 ISO 标准化语言一样。

It also includes those who believe that it doesn't really matteras long as all parties understand what is being talked about.

它还包括那些认为只要各方都理解所谈论的内容并不重要的人

It's a term made even more prevalent by the nature of the beast, much of which makes heavy use of the C++ feature known as "templates".

由于野兽的本性,这个术语变得更加流行,其中大部分都大量使用了被称为“模板”的 C++ 特性。

For "C++ Standard Library" (or stdlib)

对于“C++ 标准库”(或 stdlib)

However, there is another school of thought — to which I subscribe — that says that this is confusing. People learning C++ for the first time do notknow this distinction, and may not notice small language differences.

然而,还有另一种思想流派——我赞同——认为这令人困惑。第一次学习 C++ 的人不知道这种区别,并且可能不会注意到细微的语言差异。

The author of that article has numerous times encountered people who believe that the entire C++ Standard Library isthe STL, including features that were never part of the STL itself. Most vocal proponents of "the STL", in contrast, know exactly what they mean by it and refuse to believe that not everybody "gets it". Clearly, the term's usage is not uniform.

那篇文章的作者曾无数次遇到认为整个 C++ 标准库都是STL 的人,包括从未属于 STL 本身的特性。相比之下,“STL”的大多数声音支持者确切地知道他们的意思,并且拒绝相信不是每个人都“明白”。显然,该术语的用法并不统一。

In addition, there are some STL-like libraries that are in fact implementations of the original STL, not the C++ Standard Library. Until recently, STLPortwas one of them (and even there, the confusionabounds!).

此外,还有一些类似 STL 的库实际上是原始 STL 的实现,而不是 C++ 标准库。直到最近,STLPort还是其中之一(即使在那里,混乱也比比皆是!)。

Further, the C++ Standard does not contain the text "STL" anywhere, and some people habitually employ phrases like "the STL is includedin the C++ Standard Library", which is plain incorrect.

此外,C++ 标准在任何地方都没有包含文本“STL”,有些人习惯性地使用诸如“STL包含在 C++ 标准库中”之类的短语,这显然是不正确的。

It's my belief that continuing to propagate the usage of the term in this way will just lead to the misunderstanding going on forever. Alas, it may be entirely counter-productive to attempt to change things, even if it's supposed to be for the better. We may just be stuck with double-meanings forever.

我相信,继续以这种方式传播该术语的用法只会导致误解永远持续下去。唉,试图改变事情可能完全适得其反,即使它应该是更好的。我们可能会永远陷入双重含义。

Conclusion

结论

I appreciate that this post has been a little biased: I wrote the article you linked to. :) Anyway, I hope this helps to explain the battle a bit better.

我很欣赏这篇文章有点偏颇:我写了你链接的文章。:) 无论如何,我希望这有助于更好地解释这场战斗。

Update 13/04/2011

2011 年 4 月 13 日更新

Here are threeperfectexamplesof someone who is using "the STL" to refer to the entire C++ Standard Library. It continues to baffle me that so many people swear blind that nobody ever does this, when it's plain to see almost on a daily basis.

以下是使用“STL”来引用整个 C++ 标准库的人的三个完美示例。令我感到困惑的是,这么多人发誓说没有人这样做过,因为几乎每天都可以看到。

回答by Jerry Coffin

There is no one answer that's really correct. Alexander Stepanov developed a library he called STL (working for HP at the time). That library was then proposed for inclusion in the C++ standard.

没有一个答案是真正正确的。Alexander Stepanov 开发了一个他称为 STL 的库(当时为 HP 工作)。然后建议将该库包含在 C++ 标准中。

That basically "forked" development. The committee included some parts, rejected others completely, and redesigned a few (with Alexander's participation). Development of the original library was later moved to Silicon Graphics, but continued separately from the C++ standard library.

这基本上是“分叉”的发展。委员会包括了一些部分,完全拒绝了其他部分,并重新设计了一些(在亚历山大的参与下)。原始库的开发后来转移到 Silicon Graphics,但继续与 C++ 标准库分开。

After those pieces were added to the standard library, some other parts of the standard library were modified to fit better with what was added (e.g., begin, end, rbeginand rendwere added to std::stringso it could be used like a container). Around the same time, mostof the library (even pieces that were completelyunrelated) were made into templates to accommodate different types (e.g., standard streams).

在这些部分被添加到标准库之后,标准库的其他部分被修改以更好地适应所添加的内容(例如beginendrbeginrend被添加到其中,std::string以便它可以像容器一样使用)。大约在同一时间,大部分库(甚至是完全不相关的部分)被制作成模板以适应不同的类型(例如,标准流)。

Some people also use STL as just a short form of "STandard Library".

有些人还使用 STL 作为“标准库”的简称。

That means when somebody uses the term "STL" they could be referring to any of about half a dozen different things. For better or worse, most people who use it seem to ignore the multiplicity of meanings, and assume that everybody else will recognize what they're referring to. This leads to many misunderstandings, and at least a few serious flame-wars that made most of the participants look foolish because they were simply talking about entirely different things.

这意味着当有人使用术语“STL”时,他们可能指的是大约六种不同事物中的任何一种。不管是好是坏,大多数使用它的人似乎都忽略了含义的多样性,并假设其他人都会认出他们所指的是什么。这导致了许多误解,至少有一些严重的火焰War让大多数参与者看起来很愚蠢,因为他们只是在谈论完全不同的事情。

Unfortunately, the confusion is likely to continue unabated. It's much more convenient to refer to "STL" than something like "the containers, iterators, and algorithms in the C++ standard library, but not including std::string, even though it can act like a container." Even though "C++ standard library" isn't quite as long and clumsy as that, "STL" is still a lot shorter and simpler still. Until or unless somebody invents terms that are more precise (when necessary), andjust as convenient, "STL" will continue to be used and confusion will continue to result.

不幸的是,混乱可能会继续有增无减。引用“STL”比“C++ 标准库中的容器、迭代器和算法,但不包括std::string,即使它可以像容器一样”更方便。尽管“C++ 标准库”没有那么长和笨拙,但“STL”仍然更短、更简单。直到或除非有人发明了更精确(必要时)同样方便的术语,“STL”将继续使用,并且将继续导致混淆。

回答by In silico

The term "STL" or "Standard Template Library" does not show up anywhere in the ISO 14882 C++ standard. So referring to the C++ standard library as STL is wrong. The term "C++ Standard Library" or "standard library" is what's officially used by ISO 14882:

术语“STL”或“标准模板库”没有出现在 ISO 14882 C++ 标准的任何地方。所以将 C++ 标准库称为 STL 是错误的。ISO 14882 正式使用术语“C++ 标准库”或“标准库”:

ISO 14882 C++ Standard:

17 - Library introduction [lib.library]:

  1. This clauses describes the contents of the C++ Standard Library, how a well-formed C++ program makes use of the library, and how a conforming implementation may provide the entities in the library.

...

ISO 14882 C++ 标准:

17 - 库介绍[lib.library]:

  1. 本节描述了C++ 标准库的内容,格式良好的 C++ 程序如何使用库,以及符合要求的实现如何提供库中的实体。

...

STL is a library originally designed by Alexander Stepanov, independent of the C++ standard. However, some components of the C++ standard library include STL components like vector, listand algorithms like copyand swap.

STL 是一个最初由 Alexander Stepanov 设计的库,独立于 C++ 标准。但是,C++ 标准库的某些组件包括 STL 组件(如vectorlist和算法(如copy和 )swap

But of course the C++ standard includes much more things outside the STL, so the term "C++ standard library" is more correct (and is what's actually used by the standards documents).

但是当然,C++ 标准包括 STL 之外的更多东西,所以术语“C++ 标准库”更正确(并且是标准文档实际使用的)。

回答by Mark Ransom

I've made this same argument recently, but I believe a little tolerance can be allowed. If Scott Meyersmakes the same mistake, you're in good company.

我最近也提出了同样的论点,但我相信可以允许一点宽容。如果Scott Meyers犯了同样的错误,那么你就很好。

回答by vstepaniuk

From the GNU Standard C++ Library (libstdc++) FAQ:

来自GNU 标准 C++ 库 (libstdc++) 常见问题解答



The STL (Standard Template Library) was the inspiration for large chunks of the C++ Standard Library, but the terms are not interchangeable and they don't mean the same thing. The C++ Standard Library includes lots of things that didn't come from the STL, and some of them aren't even templates, such as std::localeand std::thread.

STL(标准模板库)是大量 C++ 标准库的灵感来源,但这些术语不可互换,它们的意思也不相同。C++ 标准库包括许多不是来自 STL 的东西,其中一些甚至不是模板,例如std::localestd::thread

Libstdc++-v3 incorporates a lot of code from the SGI STL(the final merge was from release 3.3). The code in libstdc++ contains many fixes and changes compared to the original SGI code.

Libstdc++-v3 合并了许多来自SGI STL的代码(最终合并来自3.3 版)。与原始 SGI 代码相比,libstdc++ 中的代码包含许多修复和更改。

In particular, stringis not from SGI and makes no use of their "rope" class (although that is included as an optional extension), neither is valarraynor some others. Classes like vector<>were from SGI, but have been extensively modified.

特别是,string不是来自 SGI 并且不使用他们的“绳索”类(尽管它作为可选扩展包含在内),也不是,也不是valarray其他一些。像vector<>来自 SGI 的类,但已被广泛修改。

More information on the evolution of libstdc++ can be found at the API evolutionand backwards compatibilitydocumentation.

有关 libstdc++ 演变的更多信息,请参见API 演变向后兼容性文档。

The FAQfor SGI's STL is still recommended reading.

SGI 的 STL的FAQ仍然推荐阅读。



FYI, as of March 2018 even the official STL web site www.sgi.com/tech/stl/ is gone.

仅供参考,截至 2018 年 3 月,甚至 STL 官方网站www.sgi.com/tech/stl/ 也不见了