C++ standard library (std)


The C++ standard library (std) provides a set of commonly used functionality and data structures to build upon.

using namespace std;

Standard Library Organization

All functionality used from the standard library will be part of the std namespace.

If a feature from a namespace is used often, it can be importad into the global space with using:

using std::cout;

Here's an example on how that looks like:

#include <iostream>

using std::cout;
using std::endl;

int main(){
	cout << "Hello, world!" << endl;    // doesn't need std::
	return 0;
}

Other std sub-libraries

The C++ standard library is organized into many separate sub-libraries that can be #include'd in any C++ program.


The **iostream header includes operations for reading/writing to files and the console itself, including

std::cout

Here's a simple use of the cout command:

cpp-std/cout.cpp

#include <iostream>

int main(){
	std::cout << "Hello, world!" << std::endl;
	return 0;
}

Using an unique namespace

You can also create new namespaces to avoid naming conflicts.

namespace MyNamespace{
	class MyClass{
		// code goes here
	};
}

From our previous example, if we want to specify that this Cube class is our cube class, we might want to define a new namespace as follows:

cpp-std/Cube.h

#pragma once

namespace NewNamespace{
	class Cube {
		public:
			double getVolume();
			double getSurfaceArea();
			void setLength(double length);

		private:
			double length_;
	};
}