Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::string part of the 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?

like image 343
Xeo Avatar asked May 12 '11 01:05

Xeo


People also ask

What library is std::string in?

The string class is part of the C++ standard library.

What is included in C++ STL?

C++ STL (standard template library) is a software library for the C++ language that provides a collection of templates representing containers, iterators, algorithms, and function objects.

Is string part of STD C++?

The C++ string class, part of the std namespace, allows you to manipulate strings safely. using namespace std; string my_string( "starting value" );

Is there std::string in C?

The std::string class manages the underlying storage for you, storing your strings in a contiguous manner. You can get access to this underlying buffer using the c_str() member function, which will return a pointer to null-terminated char array. This allows std::string to interoperate with C-string APIs.


1 Answers

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.

like image 144
Lightness Races in Orbit Avatar answered Oct 16 '22 11:10

Lightness Races in Orbit