Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

3.3 The Standard Library Namespace

The standard library is defined in a namespace (§2.4) called std. That is why I wrote std: :cout rather than plain cout. I was being explicit about using the standard cout, rather than some other cout.

Every standard library facility is provided through some standard header similar to <iostream>. For example:

#include<string>
#include<list>

This makes the standard string and list available. To use them, the std: : prefix can be used:

std: :string s = "Four legs Good; two legs Baaad!";
std: :list<std: :string> slogans;

For simplicity, I will rarely use the std: : prefix explicitly in examples. Neither will I always #include the necessary headers explicitly. To compile and run the program fragments here, you must #include the appropriate headers(as listed in §3.7.5 and §3.8.6). In addition, you must either use the std: : prefix or make every name from std global. For example:

#include<string>         // make the standard string facilities accessible
using namespace std;       // make std names available without std:: prefix
string s = "Ignorance is bliss!"; // ok: string is std::string

It is generally in poor taste to dump every name from a namespace into the global namespace. However, to keep short the program fragments used to illustrate language and library features, I omit repetitive #includes and std: : qualifications. In this book, I use the standard library almost exclusively, so if a name from the standard library is used, it either is a use of what the standard offers or part of an explanation of how the standard facility might be defined.

  • + Share This
  • 🔖 Save To Your Account