0

I am extremely new to C++, what is the difference between the C++ Standard Library and the std namespace. I get them confused and don't know when to use which. Also how does the header files correlate with all this?

Clu3l3ss
  • 23
  • 1
  • 7
  • Does this answer your question? [What is the difference between a class library and a namespace?](https://stackoverflow.com/questions/11427223/what-is-the-difference-between-a-class-library-and-a-namespace) – SuperStormer Apr 18 '20 at 21:58
  • Names defined by the standard library are generally placed into `std` namespace. I'm not sure what you mean by "when to use which" - whenever you wish to use a facility of the C++ standard library, that facility is generally found in `std` namespace. – Igor Tandetnik Apr 18 '20 at 21:58
  • 1
    @Clu3l3ss Start from reading a book on C++ for beginners. It will be useful. – Vlad from Moscow Apr 18 '20 at 22:07
  • By "when to use which," I meant that I don't know how each one works and what the purpose of the std namespace and the C++ standard library is. – Clu3l3ss Apr 18 '20 at 22:07
  • The purpose of the standard library is to give the programmer useful facilities to build upon. The purpose of `std` namespace is to hold the names defined by the standard library, so they don't conflict with the names defined by the program. – Igor Tandetnik Apr 18 '20 at 22:16
  • Possibly related: [https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – drescherjm Apr 18 '20 at 22:22

1 Answers1

2

The C++ standard [template] library is a collection of tools - specifically containers and algorithms - that are useful in a general sense. It is particularly powerful when the algorithms are applied to the containers - when sorting a vector, for example.

Namespaces serve to decorate the symbols defined within them and thus avoid collisions between symbols defined (usually) by different libraries. All standard library code is in namespace std. Thus we have, for example, std::vector, std::sort and a whole lot more.

To pick another library at random, the popular Nlohmann JSON library defines all its symbols in namespace nlohmann, and in the unlikely event that this defined a vector class, it would not clash with the standard library.

You will often see people on SO (and elsewhere) coding using namespace std;, sometimes in a header file, and perhaps you now understand why this practise is not wise.

Does any of that help at all?

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Oh I see now, so the namespace std only contains C++ standard library? Also how do we check which namespace is the library we use contained in? – Clu3l3ss Apr 18 '20 at 22:35
  • Yes, namespace `std` should contain the standard library and nothing else (i.e. don't put your own stuff in it), and to determine what namespace(s) is / are used by any particular library, consult the documentation or the source code. – Paul Sanders Apr 18 '20 at 22:41