Is std::string part of the STL?

2019-01-22 02:07发布

问题:

(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?

回答1:

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.



回答2:

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.



回答3:

It is part of STL indeed. And std::string is just basic_string 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.