I am a beginner programmer in C++, and I still learning the bases, but I have a simple question about The Standard library and STL in C++, I think this is not from The Core of the language, I mean this library just make programming and portability easier, and that mean if I learned and mastered C++ rules, I can build my own, I mean in general this call experience by practicing. Example: cout is the standard output function, but if I want to program a GUI software for Windows for example, I will never just look at it!! My Question:Is that True?and if not, Why?
4 Answers
You can't really be said to be a C++ programmer without a good knowledge of the standard library. And writing your own is a very bad idea. I strongly recommend you get a copy of Nicolai Josuttis's book and embrace the power it will give you. You will learn, for example, that streams are not only good for performing I/O in console applications.
And maybe you should also take a look at Good Idea / Bad Idea Should I Reimplement Most Of C++?, which discusses the pros & cons of writing your own standard library.

- 1
- 1
-
2I don't routinely use streams (where I work, people swear by C stdio; it's not my call), but containers are huge, huge time savers. While it's not out of the question to make your own linked list templates, `std::vector` and `std::map` are huge time savers, since you don't have to go crazy managing memory, and red-black trees are not a trivial chunk of code, respectively. Josuttis' book and Strostrup's *The C++ Programming Language* are what we make sure every C++ programmer has on their shelf. – Mike D. Mar 07 '10 at 14:27
-
1To add to what @Mike said, in the majority of cases the containers in the standard library will be far faster and more robust than anything you can write within the applications deadline. – Yacoby Mar 07 '10 at 14:34
You are correct that the C++ standard library are not part of the "core language" and that most of what the standard library offers you can be achieved independently using only the features core language.
However, it is much to your advantage to learn the standard library. Why waste time rewriting functions that are already there for you? If you chose to rewrite them anyway, your versions will likely be buggy. That's just one of the truths of software development, no matter how good you are, but more so if you're a beginner to the language. In contrast, the implementations out there are tried and tested in the field.

- 39,212
- 14
- 67
- 75
Looking for Your sample that std::cout is not useful withing GUI programming is not entirely true. Even if You will not use cout it is worth to know std::basic_ostream for simple string formatting abilities.

- 434
- 4
- 11
You won't use everything from the STL, but MAN are those string and container classes, and sometimes the algorithms, useful!

- 13,890
- 1
- 36
- 60
-
1A minor nitpick - std::strings have never been considered a part of what is known as the STL. – Mar 07 '10 at 14:40
-
@Neil: really? I mean, I believe that you and other people you talk to don't consider it part, but std::basic_string is in SGI's STL, it's documented alongside other template containers, etc. I certainly consider std::string to be part of the STL, to the extent that any such thing as the STL exists. Like vector
it's a specialization, but unlike vector – Steve Jessop Mar 07 '10 at 15:56it's useful ;-) -
@Steve I pluck my copy of "Generic Programming and the STL" by Matt Austern (about as good a source of STL knowledge as you are going to get outside of Mr Stepanov) from the bookcase, I look at the index, I find no mention of string. QED. – Mar 07 '10 at 16:03
-
Hmm. That's a very specific interpretation of "and" in the title. What if "string" were part of the STL, but not part of generic programming, since it's a (typedef for a) class, not a template? Would it then be mentioned in the index? Is basic_string? Is `vector
` mentioned in the index, and if not is `vector – Steve Jessop Mar 07 '10 at 16:47` therefore not part of the STL? Is `std::iota` in the index, and if not is `std::iota` not part of the STL? (btw, it is part of the STL, just not part of the C++ standard) And so on. I sort of see that string is a special case, but I don't think your Q is entirely D ;-) -
@Steve Sitting right next to Austern's book on my shelf is "STL Tutorial & Reference Guide" by Musser & Saini (both well known in STL circles). This does have an entry to string in the index, but the reference is to a couple of sentences on page 26 - there is no coverage of std::string in the remainder of the book. QED, again :-) – Mar 07 '10 at 16:58
-
1Again, hmm. I've explained why I think that your rule of inference, "if X has no index entry in a decent book on Y, then X is not part of Y", is dodgy. But unless I want to get really picky and say that the STL is a thing created by SGI, and something is part of it if and only if SGI says it is, then I can't stop you inventing your own definition of the STL. And I'm not going to be picky and say that, because SGIs product doesn't accord with what most people mean by STL, even though it is the only formal definition of the term. – Steve Jessop Mar 07 '10 at 17:02
-
@Steve Also, Josuttis in the book I recommend in my answer here, clearly distinguishes std::string from the STL. Obviously, STL is a vague and to an extent meaningless term, but trust me, strings have never been part of it. – Mar 07 '10 at 17:04
-
Btw, Stroustrup says in The C++PL, 16.2.3, that "The standard library containers and iterators (often called the STL framework)...", and string is undoubtedly a standard library container (standard, 21.3/2, "The class template basic_string conforms to the requirements of a Sequence"). As entertaining as this search for a common definition of STL is, I'm reluctant to conclude that Stroustrup and Josuttis are in significant disagreement on the issue. I'd guess instead that experts don't deeply care whether string is part of "the STL", and that RyanWilcox's usage gets the benefit of the doubt. – Steve Jessop Mar 07 '10 at 17:17
-
The practical point is that almost all the interesting things you need to know about string aren't about its nature as a container, they're about characters and locales and C-style string conversion and the myriad member functions of string that have nothing to do with generic programming. So whether strings are incidentally part of the STL or not, you still need a lengthy tutorial on them in particular (which of course is what Stroustrup does, despite having said perhaps accidentally that they are part of the STL, and which writers on generic programming wouldn't do even if he was right). – Steve Jessop Mar 07 '10 at 17:21
-
@Steve An array conforms to the requirements for a sequence, but I don't think you can say that arrays or part of the standard library, much less the STL. – Mar 07 '10 at 17:22
-
An array does not conform to the requirements for a Sequence. For example it does not implement the required expression `a.clear()` (23.1.1/4). A pointer does satisfy the requirements for an iterator, but I accept your implied argument that a pointer is not a "standard library iterator", and so is not part of what Stroustrup defines to be the STL. – Steve Jessop Mar 07 '10 at 17:25
-
-
Actually though, it's interesting that the Standard says (23.1.1/1) "A sequence is a kind of container ... the libraryu provides thee basic kinds of sequence containers: vector, list and deque". So somewhere between chapter 21 and chapter 23, `basic_string` isn't considered a Sequence any more. I guess maybe "conforms to the requirements for" a kind of container needn't be interpreted to mean that it *actually is* a container. For some definition of "actually is". I can only conclude even more strongly not to use the term "STL" myself, or acknowledge that it has a useful meaning. – Steve Jessop Mar 07 '10 at 17:32
-
"are we in agreement". Erm. I think you're probably right that string isn't truly considered part of the STL, although personally I think it "should" be ascribed STL-ness as a minor property. It easily could be in, on account of it is in SGI's STL, and if it's a container then Stroustrup says that it is part of STL. But he may have said it by accident, and quite possibly in the C++ standard it isn't "really" a container it just "conforms to the requirements" of one. Distinction without a difference, but a genuine distinction nonetheless. And that this discussion could only happen on a Sunday. – Steve Jessop Mar 07 '10 at 17:36
-
Come to think of it, probably the reason basic_string is excluded from STL books, and that it's sensible to consider it not part of the STL even though it's a standard library template, is that you'd have to be some kind of masochist to actually try to *use* it as a generic container. It works for `char` and `wchar_t`, and anything else is a major headscratch for no particular benefit you couldn't get with a vector and a few algorithms. – Steve Jessop Mar 07 '10 at 17:45
-
Interestingly, SGI's site says that STL includes the following containers: "Like many class libraries, the STL includes container classes: classes whose purpose is to contain other objects. The STL includes the classes vector, list, deque, set, multiset, map, multimap, hash_set, hash_multiset, hash_map, and hash_multimap." Also note that not all of the STL is part of the standard C++ library. – UncleBens Mar 07 '10 at 18:17