CKTIS / C++
`std` — the C++ standard library namespace
Learn how to read names such as `std::cout` and `std::chrono::seconds`, make declarations available, and shorten names safely. Curriculum examples show what the namespace controls and what comes from each API’s own contract.
Version: ISO C++23; implementation support may vary
Syntax
#include <iostream>
#include <iomanip>
// In a function:
std::cout << std::fixed << std::setprecision(2) << 3.5 << '\n';
// Alternatively, introduce one name into the block:
using std::setprecision;
std::cout << std::fixed << setprecision(2) << 3.5 << '\n';std is a namespace, not a keyword or a header. In std::cout, :: qualifies the name; ::std::cout explicitly selects std in the global namespace. std::chrono is nested. Not every library name starts with std::: the global allocation and deallocation functions operator new and operator delete are exceptions. Include the appropriate header rather than relying on incidental transitive includes. In C++23, import std; imports a library module, not a namespace; implementation support varies, and module import does not expose header macros.
using std::cout; makes one name available unqualified in its scope. using namespace std; affects unqualified lookup more broadly and can cause ambiguity; avoid it at global scope in headers. Neither form of using replaces a header or an import. Keep your own declarations and definitions out of std and its nested namespaces: adding them generally causes undefined behavior. A permitted class-template specialization, such as std::hash for a program-defined type, must meet the original template’s requirements and must not be prohibited.
Name qualification is separate from an API’s requirements. For example, both input ranges to std::set_union must be sorted according to the same ordering that the call uses.
Examples
#include <iomanip>
#include <iostream>
int main() {
using std::setprecision;
std::cout << std::fixed << setprecision(2) << 3.5 << '\n';
}Result
3.50
`using std::setprecision;` shortens just that name inside `main`. `std::fixed` remains qualified, and the output is `3.50` followed by a newline.
Related lessons
- C++ · Lesson 2 · Intermediate · Version: 2
- C++ · Lesson 10 · Intermediate · Version: 2
- C++ · Lesson 11 · Intermediate · Version: 2
- C++ · Lesson 16 · Intermediate · Version: 2
- C++ · Lesson 18 · Intermediate · Version: 2
- C++ · Lesson 19 · Intermediate · Version: 2
- C++ · Lesson 20 · Intermediate · Version: 2
- C++ · Lesson 26 · Intermediate · Version: 2
- C++ · Lesson 30 · Intermediate · Version: 2
- C++ · Lesson 32 · Intermediate · Version: 2
- C++ · Lesson 36 · Intermediate · Version: 2
- C++ · Lesson 38 · Intermediate · Version: 2
- C++ · Lesson 41 · Intermediate · Version: 2
- C++ · Lesson 42 · Intermediate · Version: 2
- C++ · Lesson 46 · Intermediate · Version: 2
- C++ · Lesson 47 · Intermediate · Version: 2
- C++ · Lesson 48 · Intermediate · Version: 2
- C++ · Lesson 50 · Intermediate · Version: 2
- C++ · Lesson 51 · Intermediate · Version: 2
- C++ · Lesson 52 · Intermediate · Version: 2
- C++ · Lesson 55 · Intermediate · Version: 2
- C++ · Lesson 56 · Intermediate · Version: 2
- C++ · Lesson 57 · Intermediate · Version: 2
- C++ · Lesson 58 · Intermediate · Version: 2
- C++ · Lesson 59 · Intermediate · Version: 2
- C++ · Lesson 61 · Intermediate · Version: 2
- C++ · Lesson 65 · Intermediate · Version: 2
- C++ · Lesson 66 · Intermediate · Version: 2
- C++ · Lesson 67 · Intermediate · Version: 2
Sources
- WG21 N4950 — Working Draft, Standard for Programming Language C++
- Library contents [contents]
- Namespace std [namespace.std]
- Using namespace directive [namespace.udir]
- The using declaration [namespace.udecl]
- Modules [std.modules]
- Input/output library [input.output]
- General utilities library [utilities]
- Strings library [strings]
- Iterators library [iterators]
- Time library [time]
- Concurrency support library [thread]