In the C++ programming language, the C++ Standard Library is a collection of classes and functions, which are written in the core language and part of the C++ ISO Standard itself.[1]
Overview
The C++ Standard Library provides several generic containers, functions to use and manipulate these containers, function objects, generic strings and streams (including interactive and file I/O), support for some language features, and functions for common tasks such as finding the square root of a number. The C++ Standard Library also incorporates most headers of the ISO C standard library ending with ".h", but their use was deprecated (reverted the deprecation since C++23[2]).[3] C++23 instead considers these headers as useful for interoperability with C, and recommends against their usage outside of programs that are intended to be both valid C and C++ programs. No other headers in the C++ Standard Library end in ".h". Features of the C++ Standard Library are declared within the stdnamespace.
The C++ Standard Library is based upon conventions introduced by the Standard Template Library (STL), and has been influenced by research in generic programming and developers of the STL such as Alexander Stepanov and Meng Lee.[4][5] Although the C++ Standard Library and the STL share many features, neither is a strict superset of the other.[citation needed] The design of the C++ standard library, much like the C standard library, is minimalistic, and contains only core features for programming, lacking most of the more specialised features offered by the Java standard library or C# standard library.
A noteworthy feature of the C++ Standard Library is that it not only specifies the syntax and semantics of generic algorithms, but also places requirements on their performance.[6] These performance requirements often correspond to a well-known algorithm, which is expected but not required to be used. In most cases this requires linear time O(n) or linearithmic time O(n log n), but in some cases higher bounds are allowed, such as quasilinear time O(n log2n) for stable sort (to allow in-placemerge sort). Previously, sorting was only required to take O(n log n) on average, allowing the use of quicksort, which is fast in practice but has poor worst-case performance, but introsort was introduced to allow both fast average performance and optimal worst-case complexity, and as of C++11, sorting is guaranteed to be at worst linearithmic. In other cases requirements remain laxer, such as selection, which is only required to be linear on average (as in quickselect),[7] not requiring worst-case linear as in introselect.
The C++ Standard Library underwent ISO standardization as part of the C++ ISO Standardization effort in the 1990s. Since 2011, it has been expanded and updated every three years[8] with each revision of the C++ standard.
Since C++23, the C++ Standard Library can be imported using modules, which were introduced in C++20.
The Apache C++ Standard Library is another open-source implementation. It was originally developed commercially by Rogue Wave Software and later donated to the Apache Software Foundation.[9] However, after more than five years without a release, the board of the Apache Software Foundation decided to end this project and move it to Apache Attic.[10]
See also
The following libraries implement much of the C++ Standard Library:
A C++ library where everything can be executed at compile time
Standard modules
Although modules were first introduced in C++20, standard library modules were only standardised as part of the language in C++23. These named modules were added to include all items declared in both global and std namespaces provided by the importable standard headers. Macros are not allowed to be exportable, so users have to manually include or import headers that emit macros for use.
The C++ standard has reserved std and std.* as module names,[11] however most compilers allow a flag to override this.[12]
The current standard library modules defined by the standard as of C++23 are:
Name
Description
std
Exports all declarations in namespace std and global storage allocation and deallocation functions that are provided by the importable C++ library headers including C library facilities (although declared in standard namespace).
std.compat
Exports the same declarations as the named module std, and additionally exports functions in global namespace in C library facilities. It thus contains "compat" in the name, meaning compatibility with C.
The above modules export the entire C++ standard library, meaning that as of currently, the standard library must be imported in its entirety. Furthermore, modules do not allow for granular imports of specific namespaces, classes, or symbols within a module, unlike Java or Rust which do allow for the aforementioned. Importing a module imports all symbols marked with export, making it akin to a wildcard import in Java or Rust.
Like Java's packages, C++ modules do not have a hierarchical system, but typically use a hierarchical naming convention. In other words, C++ does not have "submodules", meaning the . symbol which may be included in a module name bears no syntactic meaning and is used only to suggest the association of a module. As an example, std.compat is not a submodule of std, but is named so to indicate the association the module bears to the std module (as a "compatibility" version of it).
It has been proposed that additional modules providing other subsets of the standard library be added, which may eventually be included in a future revision.[13][14] These include:
Name
Description
std.fundamental
Exports the utility facilities within the C++ standard library.
Exports facilities for interfacing with the operating system as well as filesystem manipulation.
std.concurrency
Exports facilities for concurrent programming.
std.math
Exports facilities for mathematics and pseudorandom number generation.
Standard headers
The following files contain the declarations of the C++ Standard Library.
Legend: : Deprecated : Removed
General
Components that C++ programs may use for increased features.
Name
Description
<any>
Added in C++17. Provides a type-erased class std::any.
<atomic>
Added in C++11. Provides class template std::atomic, its several template specializations, and more atomic operations.
<chrono>
Provides time elements, such as std::chrono::duration, std::chrono::time_point, and clocks. Since C++20, a hefty amount of temporal features were added: calendars, time zones, more clocks, and string chrono formatting.
<concepts>
Added in C++20. Provides fundamental library concepts.
<expected>
Added in C++23. Provides class template std::expected, a result type.
<functional>
Provides several function objects, designed for use with the standard algorithms.
<generator>
Added in C++23. Provides a coroutine generator that additionally supports nested yield operations on ranges.
<memory>
Provides facilities for memory management in C++, including the class template std::unique_ptr.
<memory_resource>
Added in C++17. Provides facilities for creating polymorphic memory allocators whose behaviors can change at runtime.[15]
<optional>
Added in C++17. Provides class template std::optional, an optional type.
<scoped_allocator>
Added in C++11. Provides std::scoped_allocator_adaptor.
Added in C++11 and TR1. Provides a class template std::tuple, a tuple.
<type_traits>
Added in C++11. Provides metaprogramming facilities working with types.
<utility>
The utility file provides various utilities: class template std::pair (two-member tuples), compile-time integer sequences, helpers in constructing vocabulary types, functions such as std::move and std::forward, and many more. The namespace std::rel_ops for automatically generating comparison operators is deprecated in C++20 in favor of new defaulted comparison operators.
Provides several types and functions related to exception handling, including std::exception, the base class of all exceptions thrown by the Standard Library.
<initializer_list>
Added in C++11. Provides initializer list support.
<limits>
Provides the class template std::numeric_limits, used for describing properties of fundamental numeric types.
<new>
Provides operators new and delete and other functions and types composing the fundamentals of C++ memory management.
<source_location>
Added in C++20. Provides capturing source location information as alternative to predefined macros such as __LINE__.
<stdfloat>
Added in C++23. Provides conditional support for extended floating-point types.
Provides C++ input and output fundamentals. See iostream.
<istream>
Provides std::istream and other supporting classes for input.
<ostream>
Provides std::ostream and other supporting classes for output.
<print>
Added in C++23. Provides formatted output utilities such as std::print supported for both C and C++ streams.
<spanstream>
Added in C++23. Provides std::spanstream and other fixed character buffer I/O streams.
<sstream>
Provides std::stringstream and other supporting classes for string manipulation.
<streambuf>
Provides reading and writing functionality to/from certain types of character sequences, such as external files or strings.
<strstream>
Provides input/output operations on array-backed streams. Deprecated in C++98, removed in C++26.
<syncstream>
Added in C++20. Provides std::osyncstream and other supporting classes for synchronized output streams.
Thread support library
Components that C++ programs may use for threading and concurrent programming.
Name
Description
<barrier>
Added in C++20. Provides std::barrier, a reusable thread barrier.
<future>
Added in C++11. In 32.9.1-1, this section describes components that a C++ program can use to retrieve in one thread the result (value or exception) from a function that has run in the same thread or another thread.
Added in C++14. Provides facility for shared mutual exclusion.
<semaphore>
Added in C++20. Provides semaphore that models non-negative resource count.
<stop_token>
Added in C++20. In 32.3.1-1, this section describes components that can be used to asynchronously request that an operation stops execution in a timely manner, typically because the result is no longer required. Such a request is called a stop request.
<thread>
Added in C++11. Provide class and namespace for working with threads.
Numerics library
Components that C++ programs may use to perform seminumerical or mathematical operations.
Name
Description
<bit>
Added in C++20. Provides bit manipulation facilities.
<complex>
Defines a class template std::complex, and numerous functions for representing and manipulating complex numbers.
Added in C++20. Provides mathematical constants defined in namespace std::numbers.
<simd>
Added in C++26. Provides data-parallel types and operations on these types.
<random>
Added in C++11. Facility for generating (pseudo-)random numbers and distributions.
<ratio>
Added in C++11. Provides compile-time rational arithmetic based on class templates.
<valarray>
Defines five class templates (std::valarray, std::slice_array, std::gslice_array, std::mask_array, and std::indirect_array), two classes (std::slice and std::gslice), and a series of related function templates for representing and manipulating arrays of values.
Each header from the C Standard Library is included in the C++ Standard Library under a different name, generated by removing the '.h' file extension, and adding a 'c' at the start; for example, 'time.h' becomes 'ctime'. The only difference between these headers and the traditional C Standard Library headers is that where possible the functions should be placed into the std:: namespace. In ISO C, functions in the standard library are allowed to be implemented by macros, which is not allowed by ISO C++.
Usage of the C headers with the '.h' file extension is legal in C++ and used for compatibility.
Name
Description
<cassert>
Related to <assert.h>. Declares the assert macro, used to assist with detecting logical errors and other types of bugs while debugging a program.
Related to <ctype.h>. Defines set of functions used to classify characters by their types or to convert between upper and lower case in a way that is independent of the used character set (typically ASCII or one of its extensions, although implementations utilizing EBCDIC are also known).
<cerrno>
Related to <errno.h>. For testing error codes reported by library functions.
Related to <iso646.h>. Defines several macros that implement alternative ways to express several standard tokens. For programming in ISO 646 variant character sets. Removed in C++20.
<climits>
Related to <limits.h>. Defines macro constants specifying the implementation-specific properties of the integer types.
Related to <wctype.h>. Defines set of functions used to classify wide characters by their types or to convert between upper and lower case.
The following headers are special C compatibility headers which do not have a corresponding C++ naming convention, meaning that the C headers must be used if the header is necessary.
Name
Description
<stdatomic.h>
Added in C++23. Related to <stdatomic.h>. For atomic operations on data shared between threads.
The C headers <stdnoreturn.h> and <threads.h> do not have C++ equivalents and their C headers are not supported in C++.
C++ does not provide the C POSIX library as part of any standard, however it is legal to use in a C++ program. If used in C++, the POSIX headers are not prepended with a "c" at the beginning of the name, and all contain the .h suffix in the header name. Most headers in the POSIX library typically have a C++ equivalent implementation, such as <regex> rather than <regex.h>.