📄 lib_cpp.html
字号:
<HTML><HEAD><TITLE>C++ Library Overview</TITLE></HEAD><BODY><H1><A NAME="C++ Library Overview">C++ Library Overview</A></H1><HR><P><B><A HREF="#Using C++ Library Headers">Using C++ Library Headers</A>· <A HREF="#C++ Library Conventions">C++ Library Conventions</A>· <A HREF="#iostreams">Iostreams Conventions</A>· <A HREF="#C++ Program Startup and Termination">ProgramStartup and Termination</A></B></P><P>All C++ library entities are declared or defined in one or more<A HREF="lib_over.html#standard headers">standard headers</A>.To make use of a library entity in a program, write an<A HREF="preproc.html#include directive"><I>include</I> directive</A>that names the relevant standard header.The Standard C++ library consists of 51 required headers.This <A HREF="index.html#implementation">implementation</A> also includesthree additional headers, <CODE><hash_map></CODE>,<CODE><hash_set></CODE>,and <CODE><slist></CODE>, not required by the C++ Standard,for a total of 54 headers.These 54<B><A NAME="C++ library headers">C++ library headers</A></B>(along with the additional 18<A HREF="lib_over.html#standard headers">Standard C headers</A>)constitute a<B><A NAME="hosted implementation">hosted implementation</A></B>of the C++ library:<CODE><A HREF="algorith.html"><algorithm></A></CODE>,<CODE><A HREF="bitset.html"><bitset></A></CODE>,<CODE><A HREF="cassert.html"><cassert></A></CODE>,<CODE><A HREF="cctype.html"><cctype></A></CODE>,<CODE><A HREF="cerrno.html"><cerrno></A></CODE>,<CODE><A HREF="cfloat.html"><cfloat></A></CODE>,<CODE><A HREF="ciso646.html"><ciso646></A></CODE>,<CODE><A HREF="climits.html"><climits></A></CODE>,<CODE><A HREF="clocale.html"><clocale></A></CODE>,<CODE><A HREF="cmath.html"><cmath></A></CODE>,<CODE><A HREF="complex.html"><complex></A></CODE>,<CODE><A HREF="csetjmp.html"><csetjmp></A></CODE>,<CODE><A HREF="csignal.html"><csignal></A></CODE>,<CODE><A HREF="cstdarg.html"><cstdarg></A></CODE>,<CODE><A HREF="cstddef.html"><cstddef></A></CODE>,<CODE><A HREF="cstdio.html"><cstdio></A></CODE>,<CODE><A HREF="cstdlib.html"><cstdlib></A></CODE>,<CODE><A HREF="cstring.html"><cstring></A></CODE>,<CODE><A HREF="ctime.html"><ctime></A></CODE>,<CODE><A HREF="cwchar.html"><cwchar></A></CODE>,<CODE><A HREF="cwctype.html"><cwctype></A></CODE>,<CODE><A HREF="deque.html"><deque></A></CODE>,<CODE><A HREF="exceptio.html"><exception></A></CODE>,<CODE><A HREF="fstream.html"><fstream></A></CODE>,<CODE><A HREF="functio2.html"><functional></A></CODE>,<CODE><A HREF="hash_map.html"><hash_map></A></CODE>,<CODE><A HREF="hash_set.html"><hash_set></A></CODE>,<CODE><A HREF="iomanip.html"><iomanip></A></CODE>,<CODE><A HREF="ios.html"><ios></A></CODE>,<CODE><A HREF="iosfwd.html"><iosfwd></A></CODE>,<CODE><A HREF="iostream.html"><iostream></A></CODE>,<CODE><A HREF="istream.html"><istream></A></CODE>,<CODE><A HREF="iterator.html"><iterator></A></CODE>,<CODE><A HREF="limits2.html"><limits></A></CODE>,<CODE><A HREF="list.html"><list></A></CODE>,<CODE><A HREF="locale2.html"><locale></A></CODE>,<CODE><A HREF="map.html"><map></A></CODE>,<CODE><A HREF="memory.html"><memory></A></CODE>,<CODE><A HREF="new.html"><new></A></CODE>,<CODE><A HREF="numeric.html"><numeric></A></CODE>,<CODE><A HREF="ostream.html"><ostream></A></CODE>,<CODE><A HREF="queue.html"><queue></A></CODE>,<CODE><A HREF="set.html"><set></A></CODE>,<CODE><A HREF="slist.html"><slist></A></CODE>,<CODE><A HREF="sstream.html"><sstream></A></CODE>,<CODE><A HREF="stack.html"><stack></A></CODE>,<CODE><A HREF="stdexcep.html"><stdexcept></A></CODE>,<CODE><A HREF="streambu.html"><streambuf></A></CODE>,<CODE><A HREF="string2.html"><string></A></CODE>,<CODE><A HREF="strstrea.html"><strstream></A></CODE>,<CODE><A HREF="typeinfo.html"><typeinfo></A></CODE>,<CODE><A HREF="utility.html"><utility></A></CODE>,<CODE><A HREF="valarray.html"><valarray></A></CODE>, and<CODE><A HREF="vector.html"><vector></A></CODE>.</P><P>A <B><A NAME="freestanding implementation">freestanding implementation</A></B>of the C++ library provides only a subset of these headers:<CODE><A HREF="cstddef.html"><cstddef></A></CODE>,<CODE><A HREF="cstdlib.html"><cstdlib></A></CODE>(declaring at least the functions<A HREF="stdlib.html#abort"><CODE>abort</CODE></A>,<A HREF="stdlib.html#atexit"><CODE>atexit</CODE></A>, and<A HREF="stdlib.html#exit"><CODE>exit</CODE></A>),<CODE><A HREF="exceptio.html"><exception></A></CODE>,<CODE><A HREF="limits2.html"><limits></A></CODE>,<CODE><A HREF="new.html"><new></A></CODE>,<CODE><A HREF="typeinfo.html"><typeinfo></A></CODE>, and<CODE><A HREF="cstdarg.html"><cstdarg></A></CODE>.</P><P>The C++ library headers have two broader subdivisions,<A HREF="#iostreams">iostreams</A> headers and<A HREF="index.html#STL">STL</A> headers.</P><H2><A NAME="Using C++ Library Headers">Using C++ Library Headers</A></H2><P>You include the contents of a standard header by naming it in an<A HREF="preproc.html#include directive"><I>include</I> directive</A>,as in:</P><PRE>#include <iostream> /* include I/O facilities */</PRE><P>You can include the standard headers in any order, a standardheader more than once, or two or more standard headers that definethe same macro or the same type.Do not include a standard header within a declaration. Do notdefine macros that have the same names as keywords before you includea standard header.</P><P>A C++ library header includes any other C++ library headersit needs to define needed types. (Always include explicitly anyC++ library headers needed in a translation unit, however, lestyou guess wrong about its actual dependencies.) A Standard C headernever includes another standard header. A standard header declaresor defines only the entities described for it in this document.</P><P>Every function in the library is declared in a standard header.Unlike in Standard C, the standard header never provides a<A HREF="lib_over.html#masking macro">masking macro</A>,with the same name as the function, that masks the functiondeclaration and achieves the same effect.</P><P>All names other than <CODE>operator delete</CODE> and<CODE>operator new</CODE> in the C++ library headers are defined in the<B><CODE><A NAME="std namespace">std</A></CODE></B> namespace,or in a namespace nested within the <CODE>std</CODE> namespace.You refer to the name<CODE><A HREF="iostream.html#cin">cin</A></CODE>, for example,as <CODE>std::cin</CODE>. Note, however, that macro names are not subject tonamespace qualification, so you always write<CODE><A HREF="complex.html#__STD_COMPLEX">__STD_COMPLEX</A></CODE>without a namespace qualifier.</P><P>In some translation environments, including a C++ library headermay hoist external names declared in the <CODE>std</CODE> namespaceinto the global namespace as well, with individual <I>using</I>declarations for each of the names. Otherwise, the header does<I>not</I> introduce any library names into the current namespace.</P><P>The C++ Standard requires that the<A HREF="lib_over.html#standard headers">C Standard headers</A> declareall external names in namespace <CODE>std</CODE>, then hoist them intothe global namespace with individual <I>using</I> declarationsfor each of the names.But in some translation environments the C Standard headersinclude no namespace declarations,declaring all names directly in the global namespace.Thus, the most portable way to deal with namespaces is to followtwo rules:</P><UL><LI>To assuredly declare in namespace <CODE>std</CODE>an external name that is traditionally declaredin <CODE><stdlib.h></CODE>, for example,include the header <CODE><cstdlib></CODE>. Know thatthe name might also be declared in the global namespace.</LI><LI>To assuredly declare in the global namespacean external name declared in <CODE><stdlib.h></CODE>,include the header <CODE><stdlib.h></CODE> directly. Know thatthe name might also be declared in namespace <CODE>std</CODE>.</LI></UL><P>Thus, if you want to call<CODE>std::<A HREF="stdlib.html#abort">abort</A>()</CODE>to cause abnormal termination,you should include <CODE><cstdlib></CODE>.And if you want to call <CODE>abort()</CODE>,you should include<CODE><stdlib.h></CODE>.</P><P>Alternatively, you can write the declaration:</P><PRE>using namespace std;</PRE><P>which assuredly hoists all library names into the current namespace.If you write this declaration immediately after all <I>include</I>directives, you hoist the names into the global namespace.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -