C + + standard library and Standard Template Library

Source: Internet
Author: User
Tags bitset

transfer from original texthttp://blog.csdn.net/sxhelijian/article/details/7552499

The powerful features of C + + are derived from its rich library of libraries and function resources. The contents of the C + + standard library are defined in a total of 50 standard header files. In C + + development, use the standard library as much as you can. The immediate benefits of this include: (1) Cost: It has been provided as a standard, why bother to spend time and manpower to re-develop it; (2) Quality: The standard library is strictly tested, the correctness is guaranteed, (3) Efficiency: about human efficiency has been reflected in the cost, About the efficiency of the code implementation of the standard library to achieve the level of Daniel, (4) Good programming style: Using the industry's common practice to develop.

In the course of C + + programming, especially as the first programming course, we pay attention to the content of grammar, language mechanism and so on. The development of program design ability has a process, which crosses the basic principle knowledge directly into the general practice of engineering, because the span determines its difficulty. Moreover, on the basis of mastering the basic principles, in the understanding of the standard library problems can be fully based on practice, gradually mastered. Standard library learning does not need to read seriously, it is necessary to understand the general situation, in practice in-depth.

This task is to know what the C + + programming course does not say, but is very important to the design of this part. At least we should be able to answer the "what" question first.

One, C + + standard library

The content of the C + + standard library is divided into 10 categories, namely (it is recommended to read the header files you have used or heard):

C1. Header files in the standard library that are related to language support features

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. Header files that support stream input/output

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

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. The header file that defines the tool function

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

stroke         

<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. Header file that defines the template for the container class

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 files that support iterators

Header file

Description

<iterator>

Providing definitions and support for iterators

C8. Header file about the algorithm

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

Header file

Description

<complex>

Supports the definition and operation of complex numeric values

<valarray>

Operations that support numeric vectors

<numeric>

Define a set of general mathematical operations on a numeric sequence, such as accumulate and inner_product

<cmath>

This is the C math library, which also attaches overloaded functions to support C + + conventions

<cstdlib>

The provided function extracts the absolute value of an integer and takes a remainder operation on an integer.

C10. For a localized header file

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

Second, Standard Template Library STL introduction [1]

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), and almost all of the code uses the template class and the mode function, This provides a better opportunity for code reuse than traditional libraries that consist of functions and classes. In the C + + standard, STL is organized into the following 13 header files:<algorithm>, <deque>, <functional>, <iterator>, <vector>, <list>, <map>, <memory>, <numeric>, <queue>, <set>, <stack> and <utility >.

1. Algorithm

The library's choice of data types plays a key role in its 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 accomplishes these algorithms in an effective 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.

The STL provides about 100 template functions for implementing algorithms, 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. In this way, as long as you are familiar with the STL, many of the code can be greatly reduced, only by invoking one or two algorithm templates, you can complete the required functions and greatly improve efficiency.

The algorithm part mainly consists of the file <algorithm>,<numeric> and <functional>. <algorithm> 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, which commonly used in the scope of functions related to the comparison, Exchange, find, traverse operation, copy, modify, Remove, invert, 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. Container

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 specific types, and by setting up some template classes, STL containers support the most commonly used data structures, which allow us to specify the data type of the elements in the container. Can simplify many of our repetitive and tedious work.

The container part mainly consists of the file <vector>,<list>,<deque>,<set>,<map>,<stack> and <queue>. For some commonly used containers and container adapters (which can be thought of as containers implemented by other containers), the following table summarizes the correspondence between them and the corresponding header files.

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.

The iterator section mainly consists of the header file <utility>,<iterator> and <memory>. <utility> is a very small header file that includes many of the methods used by iterators in the declaration,<iterator> that run through several templates used in STL, and the description of <memory> is very difficult, It allocates storage space in an unusual way for elements in a container, and also provides a mechanism for temporary objects that are generated during some algorithm execution,<memory> the main part of the template class is allocator, which is responsible for generating the default allocator in all containers.

Third, PostScript

For the use of STL, there are also two kinds of viewpoints. The first is that the most important function of STL is to serve as a classical data structure and algorithm textbook, because its source code involves a lot of specific implementation problems. The second is that the purpose of STL is to simplify the design, avoid duplication of labor, improve programming efficiency, it should be "application-oriented", and the source code should not be delve into. For beginners, it is also significant to improve the understanding of their applications by analyzing source code.

Once thought to design several on-machine topics, let the students appreciate the use of STL programming. Write a suitable for beginners, the scale can not be too large, but also enough to guide the topic, it is a very time-consuming and laborious thing, plus there are other things to emergency, the account owed, and later. To the students to mention the suggestion is that a lot of C + + classic textbooks for STL have a very good explanation, you can choose one to read. When reading, you should start to learn to read, jump and read, do not have to read from beginning to end, pages by page. At this stage, you can first learn the iterator utility, the vector that proposes to replace the array in C + + programming, and the list that implements the doubly linked list. The vectors and lists seem somewhat relevant to task 1 and Task 2 this week. Furthermore, we will carry forward the spirit that we have always been particularly able to practice, and find some topics or self-editing problems in time.

C + + standard library and Standard Template Library

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.