std::string 是 STL 的一部分吗?

发布于 2024-11-06 19:52:02 字数 923 浏览 0 评论 0原文

(当我说 STL 时,我指的是围绕容器、迭代器、算法和函子的模板库。)
在考虑 std::string 主要表现得像一个普通容器,具有 beginend 函数(包括迭代器)后,我想到了这个问题,一个 size 函数,以及将所有这些用于通过迭代器作用于容器/范围的普通 STL 算法的可能性(例如 transformsort查找等)。

但与此同时,它本身并不是一个容器,因为它不适合存储任意数据的容器。此外,它主要通过成员函数(例如 substrfind_first_of 等)对包含的数据进行操作,而真正的容器不会这样做,而是让算法来处理。

此外,cplusplus 参考站点 和 C++ 标准未列出 std::string与真实的容器一起,但属于不同的类别。
然而,在 SGI 的 STL 站点 上,basic_string(因此 string typedef)与其他容器和 basic_string 参考站点< /a> 表明它属于“容器”类别。

现在我的问题是,string 实际上是 STL 的一部分,还是它本身就是一个不同的库?
如果它现在属于STL,那么它与Stepanov最初开发的STL有什么不同吗?

(When I say STL, I'm talking about the template library that revolves around containers, iterators, algorithms and functors.)
This question came to mind after thinking that a std::string mostly behaves like a normal container, with begin and end functions (including iterator), a size function and the possibility to use all of those for normal STL algorithms that work on containers / ranges through their iterators (e.g. transform, sort, find, etc.).

At the same time, however, it is not a container itself, as it doesn't fit in picture of containers that store arbitary data. Also, it operates on the contained data mostly through member functions, like substr, find_first_of, etc., while true container don't do that and let the algorithms handle that.

Additionally, the cplusplus reference site and the C++ standard don't list std::string together with the real containers, but in a distinct category.
However, on SGI's STL site, basic_string (and consequently the string typedef) are mentioned with the other container and the basic_string reference site states that it belongs to the "containers" category.

Now my question is, is string actually part of the STL or is it a distinct library itself?
And if it belongs to the STL now, did it differ in the original STL developed by Stepanov?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦行七里 2024-11-13 19:52:02

不,不是真的。是的,有点。

“STL”有不同的定义,包括:

  • 实际的 HP/SGI STL,原始库,C++ 标准库所基于的部分内容。它们包括容器、迭代器和算法。字符串不是其中的一部分。

  • C++ 标准库中基于 SGI STL 库的部分:容器、迭代器和算法。仍然没有字符串。

  • 所有 C++ 标准库。这个定义绝对没有逻辑或现实基础,但如果遵循的话,std::string 将被包含在内。

请注意,实际的 STL 自从 C++ 标准化以来就已经发展起来(大约 13 年前,请记住),并且他们向后采用了标准中的一些内容,例如字符串。这并不意味着它们最初是在 1998 年存在的……但它们现在是出于“兼容性”原因而存在的。


总结

STL 是容器、算法和迭代器。

字符串和流被 C++98 采用,然后又被现代 SGI STL 采用。

字符串是否是“STL的一部分”取决于你是否遵循逻辑和推理,或者称标准库为“STL”。

希望这有帮助。

No, not really. And yes, kind of.

There are varying definitions of "the STL", including:

  • The actual HP/SGI STL, the original library, parts of which the C++ Standard Library was based on. They included containers, iterators and algorithms. Strings were not a part of this.

  • The parts of the C++ Standard Library that were based on the SGI STL library: containers, iterators and algorithms. Still no strings.

  • All of the C++ Standard Library. This definition has absolutely no grounding in logic or reality though, if followed, std::string would be included.

Note that the actual STL has developed since C++ was standardised (some 13 years ago, remember), and they've backwards-adopted some of the stuff that went into the standard, like strings. This does not mean that they were originally there in 1998... but they are there now for "compatibility" reasons.


Summary

The STL was containers, algorithms and iterators.

Strings and streams were adopted for C++98, then backwards-adopted for the modern SGI STL.

Whether strings are "part of the STL" or not depends on whether you follow logic and reason, or call the standard library "STL".

Hope this helps.

不可一世的女人 2024-11-13 19:52:02

对此没有真正的答案。一方面,std::string 的开发完全独立于其他容器。另一方面,它有足够的添加来满足随机访问容器的所有要求。你是否选择将其归类为“STL”的一部分完全取决于你——最终,它只是表明“STL”缺乏一个单一的、商定的定义,并且它突然出现的可能性获得明确的含义是遥远的(说得好听)。

IOW,“STL”是一个糟糕的缩写,因为人们用它来表示至少三种不同的事物——但是,不幸的是,也没有更好的缩写具有更好的定义含义,因此 STL 仍在使用中,并且可能会继续无限期地这样做(并继续阻碍沟通)。

There is no real answer to this. On one hand, std::string was developed entirely independently from the other containers. On the other hand, it's had enough added on to meet all the requirements of a random-access container. Whether you choose to classify that as part of "STL" or not is entirely up to you -- in the end, it just points to the fact that "STL" lacks a single, agreed-upon definition, and the chances of it suddenly gaining a clear meaning is remote (to put it nicely).

IOW, "STL" is a lousy abbreviation to use because people use it to mean at least three different things -- but, unfortunately, there's no better abbreviation around with a better defined meaning either, so STL remains in use and will probably continue to do so (and continue to obstruct communication) indefinitely.

梦明 2024-11-13 19:52:02

它确实是STL的一部分。 std::string 只是 basic_string typedef。它是容器,专门用于具有字符串语义的数据存储(不是 C++ 中的“专业化”含义:))。但对斯捷潘诺夫一无所知。

值得一提的是,STL是“标准模板库”,而不仅仅是容器子部分。这包括算法、流和一些特征。

It is part of STL indeed. And std::string is just basic_string<char> typedef. It is container, specialized (not in C++ "specialization" meaning :) ) for data storage with string semantics. No idea about Stepanov though.

Worth mentioning is that STL is "Standard Template Library", not only container subpart. That includes algorithms, streams and some traits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文