my-server
← Wiki

C++ Standard Library

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.

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 "", but their use was deprecated (reverted the deprecation since C++23). 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 "". Features of the C++ Standard Library are declared within the <code>std</code> namespace.

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. Although the C++ Standard Library and the STL share many features, neither is a strict superset of the other. 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. For more features, some third-party libraries such as Boost libraries and POCO C++ Libraries, which offer additional features, may be used to supplement the 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. 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 log<sup>2</sup> n) for stable sort (to allow in-place merge 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), 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 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.

Implementations

Discontinued

Apache C++ Standard Library

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. 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.

See also

The following libraries implement much of the C++ Standard Library:

Namespace organisation

The C++ standard library is placed inside the <code>std</code> namespace. <code>std</code> was historically a flat namespace (unlike other languages like Java and C#, which extensively divide their respective standard libraries along nomenclatural, hierarchical lines), but since C++11 it has been further divided into sub-namespaces within it:

  • <code>std</code> (the main namespace of the standard library)
  • <code>std::chrono</code> (contains facilities for the date and time library)
  • <code>std::contracts</code> (contains facilities for reporting information on contract violations)
  • <code>std::execution</code> (contains the execution control library for asynchronous execution, including <code>task<T></code>)
  • <code>std::filesystem</code> (contains facilities for working with file system manipulation, including <code>directory_entry</code>, <code>path</code>, etc.)
  • <code>std::linalg</code> (contains facilities for linear algebra)
  • <code>std::literals</code> (inlined namespace containing various operators for creating literals of an object)
  • <code>std::literals::chrono_literals</code> (inlined namespace containing various operators for creating literals of duration objects)
  • <code>std::literals::complex_literals</code> (inlined namespace containing various operators for creating literals of imaginary numbers)
  • <code>std::literals::string_literals</code> (inlined namespace containing various operators for creating literals of string objects)
  • <code>std::literals::string_view_literals</code> (inlined namespace containing various operators for creating literals of string-view objects)
  • <code>std::meta</code> (contains facilities for static (compile-time) reflection with the <code>info</code> type representing reflection metadata)
  • <code>std::numbers</code> (contains all numeric constants, such as <code>pi</code>, <code>e</code>, etc.)
  • <code>std::placeholders</code> (contains all placeholder objects <code>_1</code>, <code>_2</code>, ..., up to an implementation-defined maximum <code>_N</code>)
  • <code>std::pmr</code> (contains symbols for polymorphic memory resources, for flexible runtime-configured allocation, as well as versions of the standard library types using <code>polymorphic_allocator</code> as the allocator)
  • <code>std::ranges</code> (contains symbols for working over ranges, which extend the algorithm/iterator libraries)
  • <code>std::ranges::views</code> (contains symbols for range adapters for creating views, for lazy collection manipulation without copies; also aliased as <code>std::views</code>)
  • <code>std::regex_constants</code> (contains various regular expression customisation constants, such as <code>ECMAScript</code>, <code>awk</code>, <code>grep</code>, etc.)
  • <code>std::rel_ops</code> (contains automatically generated comparison operators, deprecated since C++20)
  • <code>std::this_thread</code> (contains facilities for manipulating the current thread)

There is also a <code>std::experimental</code> namespace, with further sub-namespaces in <code>std::experimental::*</code> for various pre-standardised/technical specification features which may be implemented by some compiler vendors.

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 <code>std</code> 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, or can be alternatively exported as compile-time constants.

The C++ standard has reserved <code>std</code> and <code>std.*</code> as module names, however most compilers allow a flag to override this.

The current standard library modules defined by the standard as of C++23 are:

<code>std.compat</code> is not a submodule of <code>std</code>, but is named so to indicate the association the module bears to the <code>std</code> module (as a "compatibility" version of it). Despite there being only one module for the entire standard library, it has been proposed that additional modules providing other subsets of the standard library be added.

Standard headers

The following files contain the declarations of the C++ Standard Library.

Legend:<br>

Deprecated<br>
Removed

General

Components that C++ programs may use for increased features.

Language support

Components that C++ programs may use for support during development.

Containers/collections

Components that C++ programs may use for container data structures.

Iterators and ranges

Components that C++ programs may use to manipulate iterators, ranges, and algorithms over ranges and containers.

Localisation

Components that C++ programs may use for localisation and character encoding manipulation.

Strings

Components that C++ programs may use for string manipulation.

Streams, files, and input/output

Components that C++ programs may use for input/output manipulation and file manipulation.

Thread support library

Components that C++ programs may use for threading and concurrent programming.

Numerics library

Components that C++ programs may use to perform seminumerical or mathematical operations.

C standard library

Each header from the C Standard Library is included in the C++ Standard Library under a different name, generated by removing the file extension, and adding a at the start; for example, '<code>time.h</code>' becomes '<code>ctime</code>'. The only difference between these headers and the traditional C Standard Library headers is that where possible the functions should be placed into the <code>std::</code> 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 file extension is legal in C++ and used for compatibility.

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.

The C headers <code><stdnoreturn.h></code> and <code><threads.h></code> 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 "" at the beginning of the name, and all contain the suffix in the header name. Most headers in the POSIX library typically have a C++ equivalent implementation, such as <code><regex></code> rather than <code><regex.h></code>.

Networking TS library

The following headers are defined in the C++ networking TS, itself based on the <code>boost::asio</code> library from Boost, which is not standardised in C++ yet. Despite this, it is provided by some vendors, such as GCC. The symbols are placed in <code>std::extensions::net</code>.

See also

References

Further reading

External links