The powerful features of C + + are derived from its rich library of libraries and function resources. C + + the contents of the standard library are in total - defined in a standard header file .
In C + + development, use the standard library as much as you can. The immediate benefits of doing so include:
(1) Cost: has been provided as a standard, why bother to spend time, manpower re-development it;
(2) Quality: The standard library is strictly tested, the correctness is guaranteed;
(3) Efficiency: Regarding the human efficiency already embodied in the cost, regarding the code execution efficiency to believe realizes the standard library The bull's level;
(4) Good programming style: adopt the common practice in the industry to develop.
One, C + + standard library
C + + the contents of the standard library are divided into Ten class ,
They are: C1. Language support; C2. input/output; C3. diagnostics; C4. General tools; C5. String; C6. container; C7. iterator support; C8. algorithm; C9. numeric operations; C10. Localization.
C1. header files in the standard library that are related to language support features ( One each)
Header file |
Description |
<cstddef> |
Defines macros null and Offsetof, as well as other standard types size_t and ptrdiff_t. The difference from the corresponding standard C header file is that NULL is a supplemental definition of the C + + null pointer constant, and the macro Offsetof accepts the struct or union type parameter as long as they do not have a non-static member of the member pointer type. |
<limits> |
Provides definitions related to the base data type. For example, for each numeric data type, it defines the maximum and minimum values that can be represented and the number of digits in the binary digits. |
<climits> |
Provides a C-style definition that is related to the basic integer data type. The C + + style definition for this information is in <limits> |
<cfloat> |
Provides a C-style definition that is related to a basic floating-point data type. The C + + style definition for this information is in <limits> |
<cstdlib> |
Provides macros and functions that support program startup and termination. This header file also declares a number of other miscellaneous functions, such as search and sort functions, from strings to numeric functions. It differs from the corresponding standard C header file Stdlib.h, which defines abort (void). The abort () function has additional functionality that does not call destructors for static or automatic objects, nor does it call functions passed to the atexit () function. It also defines the extra functionality of the exit () function, which can release a static object and invoke a function registered with atexit () in reverse order of registration. Clears and closes all open C streams and returns control to the host environment. |
<new> |
Supports dynamic memory allocation |
<typeinfo> |
Support for type identification of variables during run time |
<exception> |
Supports exception handling, which is a way to handle errors that may occur in a program |
<cstdarg> |
Supports a function that accepts a variable number of arguments. That is, when you call a function, you can send data items of varying amounts to the function. It defines the macro va_arg, Va_end, Va_start, and va_list types |
<csetjmp> |
Provides functions for non-local jumps of C-style. These functions are not commonly used in C + + |
<csignal> |
Provide C-style support for interrupt handling |
C2. Stream Input Support / the output header file ( One each)
Header file |
Description |
<iostream> |
Supports the input and output of standard flow cin, cout, Cerr, and clog, and it also supports multibyte character standard flow wcin, Wcout, Wcerr, and Wclog. |
<iomanip> |
Provides a manipulation program that allows changing the state of the stream, thereby altering the format of the output. |
<ios> |
Defining the base class for iostream |
<istream> |
Defining template classes for input to manage output stream buffers |
<ostream> |
Define the template class for the output of the management output stream buffers |
<sstream> |
Stream input and output with string support |
<fstream> |
Stream input and output supporting files |
<iosfwd> |
Provide forward declarations for input and output objects |
<streambuf> |
Support for streaming input and output caching |
<cstdio> |
Provides C-style input and output for standard streams |
<cwchar> |
Support for C-style input and output with multibyte characters |
C3. header files related to diagnostic functions ( 3 each)
Header file |
Description |
<stdexcept> |
Defines a standard exception. Exceptions are the way to handle errors |
<cassert> |
Define an assertion macro to check for scenarios during run time |
<cerrno> |
Support for C-style error messages |
C4. defines the header file for the tool function ( 4 each)
Header file |
Description |
<utility> |
Defines overloaded relational operators, simplifies the writing of relational operators, and defines the pair type, which is a type of template that can store a pair of values. These functions are used elsewhere in the library |
<functional> |
Defines a number of function object types and functions that support function objects, which are arbitrary objects that support the operator () () function call operator |
<memory> |
Define a standard memory allocator for containers, functions that manage memory, and auto_ptr template classes |
<ctime> |
Support for system clock functions |
C5. header files that support string processing ( 6 each)
Header file |
Description |
<string> |
Provides support and definitions for string types, including single-byte strings (composed of char), string and multibyte strings (composed of wchar_t) |
<cctype> |
Single-byte character categories |
<cwctype> |
Multi-byte character category |
<cstring> |
Provides functions for processing non-empty byte sequences and memory blocks. This differs from the corresponding standard C library header file, where several C-style strings of generic C library functions are returned with a value of const and non-const function pairs instead of |
<cwchar> |
To provide functions for processing, performing I/O, and converting multibyte character sequences, which differs from the corresponding standard C library header file, the General C library functions of several multibyte C-style string operations are replaced by the return values of const and non-const function pairs. |
<cstdlib> |
Provides functions for converting a single-byte string to a numeric value, converting between multibyte characters and multibyte strings |
C6. the header file that defines the template for the container class ( 8 each)
Header file |
Description |
<vector> |
Defines a vector sequence template, which is a type of array that can be re-set in size, safer and more flexible than a normal array |
<list> |
Defines a list sequence template, which is a series of linked lists that often insert and delete elements at any location |
<deque> |
Define deque sequence templates to support efficient insert and delete operations at the start and end |
<queue> |
Define the sequence adapter queue and priority_queue for the Queues (FIFO) data structure |
<stack> |
Define a sequence adapter stack for a stack (last in, first out) data structure |
<map> |
Map is an associative container type that allows for uniqueness based on key values and is stored in ascending order. Multimap is similar to map, but the key is not unique. |
<set> |
Set is an associative container type used to store unique values in ascending order. Multiset is similar to set, but the value does not have to be unique. |
<bitset> |
Defines a bitset template for a fixed-length bit sequence, which can be considered a fixed-length compact bool array |
C7. header file that supports iterators ( 1 each)
Header file |
Description |
<iterator> |
Providing definitions and support for iterators |
C8. header file about the algorithm ( 3 each)
Header file |
Description |
<algorithm> |
Provides a set of algorithm-based functions, including permutation, sort, merge, and search |
<cstdlib> |
Declare C standard library functions bsearch () and qsort () to search and sort |
<ciso646> |
Allow use of and in code instead of && |
C9. header file for numeric operations ( 5 each)
|
|
<COMPLEX> |
supports definition and operation of complex numeric values |
<valarray>, |
supports operation of numeric vectors /p> |
<numeric>, |
|
<CMATH> |
This is the C math library, which also has overloaded functions attached to support C + + conventions |
<cstdlib> |
|
C10. for a localized header file ( 2 each)
Header file |
Description |
<locale> |
The localization provided includes character categories, sort sequences, and currency and date representations. |
<clocale> |
Provide C-style support for localization |
All header files for the C + + standard library do not have an extension. The C + + standard library is provided as a standard header file in the <cname> format.
In the <cname> form standard header file, macro-related names are defined in the global scope, and other names are declared in the Std namespace.
Standard C library header filenames in name.h form can also be used in C + +. In these 10 class header files, the,<cstdlib> header files appear in C5/c8/c9 respectively.
Second, the Standard Template Library STL Introduction
The STL (Standard Template Library) is a generic term for a range of software developed by HP Labs. It is now mostly in C + +, but the technology has been around for a long time before being introduced to C + +.
STL 's code is broadly divided into three categories :algorithm (algorithm),container (container) , and iterator (iterators), almost all The code takes the form of template classes and stencil functions , which provides a better chance of code reuse than a traditional library of functions and classes.
in the C + + Standard, STL be organized into the following - header file:
<algorithm>,<deque>,<functional>,<iterator>,<vector>,<list>,<map>,<memory>,<numeric>,<queue>,<set>,<stack>and the<utility>.
1 , Algorithm
The function library plays an important role in the choice of data types and their reusability. For example, a square root function is certainly more reusable in the case of using a floating-point number as its parameter type than using an integer as its parameter class.
The mechanism of C + + through templates allows you to defer certain types of choices until you really want to use a template or to make a template special, and the STL provides quite a few useful algorithms.
It is done in a valid framework, you can divide all types into a few categories, and then you can replace other types in the same category with one type in the template's parameters.
STL provides about - a template function that implements the algorithm , such as the algorithm For_each, invokes the specified function for each element in the specified sequence, stable_sort the sequence with the rules you specify, and so on.
This way, as long as you are familiar with the STL, many of the code can be greatly simplified, only by invoking one or two algorithm templates, you can complete the required functions and greatly improve efficiency.
algorithm part of the main headers file <algorithm> , <numeric> and the <functional> composition . <algorithm> is the largest of all STL header files (although it is well understood),
It is composed of a large stack of template functions, you can think that each function is largely independent, the most commonly used in the scope of functions related to the comparison, Exchange, find, traverse operation, copy, modify, remove, reverse, sort, merge and so on.
The <numeric> volume is small, and includes only a few template functions that perform simple mathematical operations on the sequence, including some operations on the sequence of addition and multiplication. <functional> defines a number of template classes that declare function objects.
2 , Containers
In the actual development process, the importance of the data structure itself is not inferior to the algorithm of manipulating data structures, the choice of data structure becomes more important when there is a high demand for time in the program.
The number of classic data structures is limited, but we often repeat some of the code that is written to implement the structure of vectors, lists, and so on, which are very similar to each other in order to accommodate changes in the data.
STL containers provide us with the convenience of allowing us to reuse existing implementations to construct our own data structures under a particular type, by setting up some template classes,
STL containers provide support for the most commonly used data structures, which allow us to specify the data type of the elements in the container, simplifying many of our repetitive and tedious work.
Container Part main header file <vector>,<list>,<deque>,<set>,<map>,<stack> and the <queue> composition . For some commonly used containers and container adapters (which can be thought of as containers implemented by other containers),
You can summarize the correspondence between them and the corresponding header file by using the following table.
Data |
Describe |
Implementing the header File |
Vectors (vector) |
Elements that are continuously stored |
<vector> |
Listing (list) |
A doubly linked list of nodes with one element per node |
<list> |
Dual Queue (deque) |
An array of continuously stored pointers to different elements |
<deque> |
Collection (SET) |
A red-black tree of nodes, each of which contains an element that is arranged between nodes in a predicate that acts on the pair of elements, and that no two different elements can have the same order |
<set> |
Multiple collections (Multiset) |
Allows a collection of two elements with equal order |
<set> |
Stacks (Stack) |
Arrangement of last-in-first-out values |
<stack> |
Queuing (queue) |
The arrangement of FIFO |
<queue> |
Priority Queue (Priority_queue) |
The order of the elements is a queue that is determined by a predicate acting on the stored value pair |
<queue> |
Mapping (MAP) |
A set consisting of {key, value} pairs, arranged in a predicate on a key pair of functions |
<map> |
Multi-mapping (Multimap) |
Allow mappings of key pairs to have equal order |
<map> |
3 , Iterators
Iterators are the most basic part of the work, but they are more difficult to understand than the first two. The basic principle of software design is that all problems can be simplified by introducing an indirect layer, which is done with an iterator in the STL.
In summary, iterators are used in STL to link algorithms and containers, and act as a binder.
Almost all of the algorithms provided by the STL are working through an iterator that accesses the sequence of elements, each of which defines itself as a proprietary iterator to access the elements in the container.
Iterator section main header file <utility>,<iterator> and the <memory> composition . <utility> is a very small header file that includes a declaration that runs through several templates used in the STL,
<iterator> provides many of the methods used by iterators, and the description of <memory> is very difficult, and it allocates storage space for the elements in the container in an unusual way.
It also provides a mechanism for temporary objects generated during some algorithm execution, and the main part of <memory> is the template class allocator, which is responsible for generating the default allocator in all containers .
Three, C + + standard library and STL's relation
The STL is a subset of the latest C + + standard library, which occupies approximately 80% of the entire library's component. Templates that play a key role in implementing STL are flooded with almost the entire C + + standard function library.
The C + + standard library provides a scalable, foundational framework for C + + programmers. We can get great convenience from it, and we can extend it by inheriting the existing classes and compiling the containers, algorithms, iterations, etc. that conform to the interface specification.
The C + + standard library is all of the contents of the Std namespace, those that do not have. h, such as <cstdio>, <iostream>, and so on. such as std::string, and IO streams are not part of the STL,
But they are STL compatible, can apply iterators, algorithms and so on. Although the std::string and IO streams are also template classes, they do not belong to the STL.
C + + standard library and Standard Template Library