1 Basic concept
STL (Standard Template Library) is a collective term for a series of software developed by HP Labs. It appears mainly in C++, but the technology has existed for a long time before it was introduced into C++.
STL is broadly divided into three categories: algorithm (algorithm), container (container) and iterator (iterator). Containers and algorithms can be seamlessly connected through iterators. Almost all code uses template classes and template functions, which provides better code reuse opportunities than traditional libraries consisting of functions and classes. In the C++ standard, STL is organized into the following 13 header files:,,,,,,
2
In the actual development process of the container, the importance of the data structure itself will not be less important than the importance of the algorithm operating on the data structure. When there are parts that require high time in the program, the choice of data structure becomes even more important. important.
The number of classic data structures is limited, but we often repeat some codes written to implement structures such as vectors and linked lists. These codes are very similar, but they differ in details to adapt to changes in different data. The STL container provides us with such convenience. It allows us to reuse existing implementations to construct our own data structure under a specific type. By setting up some templates, the STL container provides support for the most commonly used data structures. Parameters allow us to specify the data type of the elements in the container, which can simplify many of our repetitive and tedious work.
The container part is mainly composed of header files,,,,
1.4 Algorithm
The selection of data types by the function library plays a vital role in its reusability. For example, a function for finding a square root is definitely more reusable when using a floating-point number as its parameter type than using an integer as its parameter type. And C++ through the template mechanism allows to postpone the selection of certain types, until you really want to use the template or specialize the template, STL takes advantage of this and provides quite a lot of useful algorithms. It completes these algorithms in an effective framework-all types can be divided into a few categories, and then one type can be used in the template parameters to replace other types in the same category.
STL provides about 100 template functions that implement algorithms. For example, the algorithm for_each will call the specified function for each element in the specified sequence, stable_sort will sort the sequence stably according to the rules you specify, and so on. In this way, as long as you are familiar with STL, many codes can be greatly simplified, and only by calling one or two algorithm templates, you can complete the required functions and greatly improve efficiency.
The algorithm part is mainly composed of header files, and. It is the largest of all STL header files (although it is well understood). It is composed of a large number of template functions. It can be considered that each function is largely independent, and the commonly used functions involve To compare, swap, find, traverse operations, copy, modify, remove, reverse, sort, merge, etc. The volume is very small, including only a few template functions that perform simple mathematical operations on the sequence, including some operations on the sequence of addition and multiplication. Some template classes are defined in, to declare function objects.
1.5
C ++ Standard Library The powerful function of C++ comes from its rich class library and library function resources. The content of the C++ standard library is defined in a total of 50 standard header files. In C++ development, the standard library should be used as much as possible. The direct benefits of this include: (1) Cost: It has been provided as a standard, so why bother to spend time and manpower to re-develop it; (2) Quality: The standard library is strictly tested and its correctness is guaranteed; (3) Efficiency: Human efficiency has already been reflected in the cost. Regarding code execution efficiency, we must believe in the level of the big cows who implement the standard library; (4) Good programming style: Use common practices in the industry for development.
In the C++ programming course, especially as the first programming course, we pay attention to the content of grammar and language mechanism. There is a process of cultivating program design ability. It is the common practice to directly enter the engineering through the basic principle knowledge, because the span determines its difficulty. Furthermore, on the basis of mastering the basic principles, the problem of understanding the standard library can be grasped step by step with practice. The study of the standard library does not require serious reading, but in-depth practice while understanding the general situation.
This task is to know this part of the content that is not covered in the C++ programming course, but is very important to programming. At least we need to be able to answer the "what's there" question first.
The content of the C++ standard library is divided into 10 categories, which are (it is recommended to mark out the header files you have used or heard of during reading):
C1. The header files related to language support functions in the standard library
Header file description
Define the macro 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 supplementary definition of C++ null pointer constants. The macro offsetof accepts structure or union type parameters, as long as they have no non-static members of member pointer types.
Provide definitions related to basic data types. For example, for each numeric data type, it defines the maximum and minimum values that can be represented and the number of binary digits.
Provides C style definitions related to basic integer data types. The C++ style definition of this information is
provided in the C style definition related to the basic floating-point data type. The C++ style of this information is defined in
Macros and functions that support program startup and termination. This header file also declares many other miscellaneous functions, such as search and sort functions, conversion from strings to numbers, and other functions. It is different from the corresponding standard C header file stdlib.h, which defines abort(void). The abort() function has additional functions. It does not call the destructor for static or automatic objects, nor does it call the function passed to the atexit() function. It also defines the extra function of the exit() function, which can release static objects and call the functions registered with atexit() in the reverse order of registration. Clear and close all open C streams, and return control to the host environment.
Support dynamic memory allocation
Support variable type identification during operation
Support exception handling, which is a way to handle errors that may occur in the program
Support functions that accept a variable number of parameters. That is, when the function is called, a unequal number of data items can be sent to the function. It defines the macro va_arg, va_end, va_start and va_list types
Provides functions for C-style non-local jumps. These functions are not commonly used in C++.
Provide C-style support for interrupt handling
. C2. Header files that support stream input/output. The
header file description
supports the input and output of standard streams cin, cout, cerr and clog. It also supports multi-byte character standards Stream wcin, wcout, wcerr and wclog.
Provide a manipulation program, allowing to change the state of the stream, thereby changing the output format.
Define the base class
of iostream. Define the template class
for managing the input of the output stream buffer. Define the template class for managing the output of the output stream buffer.
Support the stream input and output of strings. Support the stream input and output of
files.
Provide forward declaration
support for input and output objects. Cache
of stream input and output Provides C-style input and output for standard streams
Supports C-style input and output of multi-byte characters
C3. Header files related to diagnostic functions
Header file description
Defines standard exceptions. Exceptions are the way to handle errors.
Define assertion macros to check the situation during runtime.
Support C-style error messages
C4. The header file that defines the tool function. The header file
description
defines overloaded relational operators to simplify the writing of relational operators. It also defines the pair type, which is a template type that can store a pair of values. These functions are used elsewhere in the library.
Many function object types and functions that support function objects are defined. Function objects are arbitrary objects that support the operator()() function call operator.
Define standards for containers, memory management functions, and auto_ptr template classes. Memory allocator
supports system clock function
C5. Header file supporting string processing
Header file description
Provide support and definition for string types, including single-byte strings (composed of char) and multi-byte strings (composed of wchar_t)
single-byte character categories
The multibyte character category
provides functions for processing non-empty byte sequences and memory blocks. This is different from the corresponding standard C library header file. Several general C library functions of C style strings are replaced by function pairs that return const and non-const.
Provided for processing, performing I/O, and converting multi-byte character sequences Function, which is different from the corresponding standard C library header file. Several general C library functions for multi-byte C-style string manipulation are replaced by pairs of functions that return const and non-const.
Provides functions for converting single-byte strings to numeric values, and between multi-byte characters and multi-byte strings
C6. The header file that
defines the template of the container class defines the vector sequence template, which is an array type whose size can be reset. It is safer and more flexible to
define the list sequence template than ordinary arrays . This is a linked list of sequences, often in any position Insert and delete elements
Define deque sequence templates to support efficient insertion and deletion operations at the beginning and end.
Define sequence adapters queue and priority_queue
for queue (first in first out) data structure Define sequence adapter stack for stack (last in first out) data structure
C7. Header file supporting iterators
Header file description
Provide definition and support for iterators
C8. Header files related to algorithms
Header file description
Provides a set of algorithm-based functions, including substitution, sorting, merging and searching.
Declares the C standard library functions bsearch() and qsort(). Searching and sorting
allows using and instead of code. &&
C9. Header files related to numerical operations. Header file
description
Supports the definition and operation of complex numerical values.
Supports numerical vector operations
. Defines a set of general mathematical operations on numerical sequences, such as accumulate and inner_product.
This is a C math library with additional overloads. Functions to support C++ conventions. The
provided functions can extract the absolute value of an integer and perform the remainder operation on the integer
C10. Header files related to localization
Header file description
The localization provided includes character category, sorting sequence, currency and date representation.
Provide C style support for localization
All header files in the C++ standard library have no extension. The C++ standard library is provided in the form of standard header files. In the standard header file, the names related to the macro are defined in the global scope, and other names are declared in the std namespace. In C++, you can also use the standard C library header file name in the form of name.h
** A
brief review of 1.6 templates ** Templates are
a tool for implementing code reuse mechanisms. The essence is to implement type parameterization, that is, to define types as parameters .
C++ provides two kinds of templates: function template, class template
Introduction to
function template A function template is to create a general function. The function return type and formal parameter type are not specified specifically, but are represented by virtual types.
Any function with the same function body can be replaced by a function template, no need to define multiple functions, just define once in the template.
When calling a function, the system will replace the virtual type in the template according to the type of the actual parameter, thus realizing the functions of different functions.
Introduction to the class template
Let’s take a look at the following class first. The class for finding the maximum value.
Like the function template, the class template is to create a general class whose data member types, member function return types, and parameter types are all available Not specified specifically, but represented by virtual types.
When using a class template to create an object, the system will replace the virtual type in the class template according to the type of the actual parameter, so as to realize the functions of different classes.