Item M35: Let yourself use standard C language
Since published in 1990, "The Annotated C Reference Manual" (see the original book P285, Appendix: Recommended reading) is the most authoritative reference manual supply service to determine what is C owned and what is not. In these years after it is published, the C ISO / ANSI standard has changed significantly (mainly to expand). As an authority manual, it is not applicable.
After "ARM", the change in C has far affected the extent of the program written. Therefore, it is very important to familiarize with the criticality of the C standard declaration with the "ARM".
The ISO / ANSI C standard is that the manufacturer will consider when the compiler is to prepare for the book, which is the programmer used to find an authority when there is questions about C . The most important changes happened after "ARM" are the following:
* Added new features: RTTI, namespace, Bool, keyword Mutable, and Explicit, the overloaded operation of the enumeration, the Const Static member variable is initialized in the definition of the class.
* Templates are extended: The member template is now allowed to increase the grammar of forced templates, and the template function allows non-type parameters, template classes can use them as template parameters.
* Abnormal processing is detailed: Exception specifications Stated in compile periods to be more stringent, UNEXPECTED () functions can now throw a Bad_Exception object.
* The memory allocation function is improved: adds the Operator new [] and operator delete [] function, Operator new / new [] will throw an exception when the memory allocation fails, and has a return to 0 (not throwing it) Version options. (See Effective C Item 7)
* Added new type conversion form: static_cast, dynamic_cast, const_cast, and reinterpret_cast.
* Language rules redefine: When a virtual function is redefined, its return value does not need to be fully matched (if you originally return the base class object or pointer or reference, the derived class can return to the object, pointer or reference), The survival of the temporary object is precisely defined.
Most changes are described in "The Design and Evolution Of C " (see the original book P285, Appendix: Recommended Readings). The current C textbook (those writings after 1994) should also contain them. (If you find this, throw it away.) In addition, this book contains some examples to display any new features. If you want to know, please see the index table.
These changes in C will be eclipsed in front of the standard running library. In addition, the evolution of the standard running library has been promoted by the language itself. For example, "The Design and Evolution Of C " has little mention of standard runtime. Discussing the books of the running library have been out of time, because the runtime has changed very much after 1994.
The standard runtime library is divided into the following category (see Effective C Item 49):
* Support standard C runners. Don't worry, C still remembers its root. Some subtle adjustments have made the C version of the C run library comply with C more stringent type checks, but their functions and effects are in the C-run library, what is it in C ? * Support String type. Leader Mike Vilot in the Standard C Running Realization Group "If there is no standard String type, then bloodstream in the street!" (Some people are so excited.) Calm, put the ax and stick - Stand C The run library has a String type.
* Support localization. Different cultures have different character sets, as well as different habits when displaying date and time, sorting string, output currency value ... and so on. Standard Runners Support for localization to facilitate the development of procedures for use in different cultural areas.
* Support I / O operation. The stream library still has some reserved in C standards, but the Standards Committee made a small amount of repair. Although partial classes are removed (especially iostram and fstram), partial classes are replaced (for example, StringStream-based StringStream classes are replaced with char *-based strStream class, and now they are not advocated using the strstream class), However, the basic function of the standard stream contains a basic function of the previous implementation.
* Support mathematical operations. Multi-class, C teachings must be included in the standard runtime. In addition, the run library contains special array classes (Valarray) to avoid confusion. These arrays have better performance than the arrays of the built-in type, especially under the multi-CPU system. The runtime also provides some commonly used operational functions such as adding and subtraction.
* Support universal container and operations. The standard C running library has a set of template classes and template functions, called standard template libraries (STL). STL is the most revolutionary part of the standard C runs. I will outline its characteristics below.
Before introducing STL, you must first know the two features of the standard C runtime.
First, almost anything in the running library is template. In this book, I talked about the String class in the run library, actually there is no such class. In fact, there is a template class called Basic_String to describe the character sequence, which accepts the parameters of a character type to construct this sequence, which makes it possible to represent a char string, a Wide Char string, a Unicode Char string, and the like.
The String class we usually think is made from Basic_String
Typedef Basic_String
This is actually hidden in many details because the Basic_String Template has three parameters; in addition to the first extraordinary parameters. To fully understand the String type, you must face this unleavened Basic_String:
Template Class traits = string_char_traits Class allocator = allocator> Class Basic_String; Using String type does not need to understand this official article, because the type definition makes it a template class. Whether you need to customize the String character set, or adjust the behavior of these characters, or control the memory assigned to String, this Basic_String template allows you to complete these things. Approximation method used in design String type - summarizes the behavior and promotes the template - widely used by standard C runners. Iostream? They are templates; a type parameter determines the characteristics of this stream. plural? Also template; a type parameter determines how the number is stored. Valarray? Template; a type parameter indicates what exists in the array. If you are not familiar with the template, it is now a familiar opportunity. It is also necessary to know that the standard run library will be included in the namespace STD will be included in the standard runtime. To use the standard running library without specifically specify the name of the run library, you can use using instructions or use (more convenient) USING declaration (see Effective C Item 28). Fortunately, this duplication is automatically carried out in your #include's proper header file. * Standard Template Library The biggest news in the standard C runs is STL, standard template library. (Because almost everything in the C running is template, this STL is not very suitable. However, the part of the library is in the library, the name is good or bad, anyway, now it is this.) STL is likely to have a lot of effects - I am afraid that the structure of most -c runs, so it is important to familiarize it. They are not difficult to understand. STL is based on three basic concepts: a Container, a selector, and algorithms. The bag container is a package that is encompassed. The choice is an object of the class pointer to allow you to operate STL's package container as an array that uses a pointer to operate the type of build. The algorithm is a function of processing the package container, and uses the selection Implemented. Lenovo's rules of C (and C) in the rules of the inner groups are too easy to understand the idea of STL. It really only needs to be clear: a pointer pointing to an array correctly indicates any element of array or that is just exceeding the array range. If pointing to the super-range element, it will only be addressed with other pointers pointing to this array; in response to it, the result is undefined. We can use this rule to achieve a function of finding a particular value in an array. For a whole array, the function may be like this: INT * Find (int * begin, int * end, int value) { While (Begin! = End && * Begin! = Value) Begin Return Begin; } This function is found between BeGin and END (excluding end - end points to elements that have just exceeded array ranges) to find Value, return the first value of value; if not found, it returns end. Returning end to indicate that you didn't find it, it looked a little coming. Returns 0 (NULL pointer) Isn't it better? It does not look more natural, but it doesn't mean more "good". The Find () function must return special pointer values to indicate that the lookup fails, and the END pointer is the same as the NULL pointer. However, as we will see, the END pointer is better than the NULL pointer when promoting to other bag container types. Honestly, this may not be a method of finding a Find () function, but it is not unrealistic, its promotion is very good. After reading the example below, you will master the thinking of the STL. You can use the Find () function like this: Int Values [50]; ... INT * firstfive = find (Values, // search the Range VALUES 50, // Values [0] - Values [49] 5); // for the value 5 IF (FirstFive! = VALUES 50) {// DID The Search Succeed? ... // YES } Else { ... // No, The Search Failed } You can also search only part of the array: INT * firstfive = find (Values, // search the Range VALUES 10, // Values [0] - Values [9] 5); // for the value 5 INT AGE = 36; ... INT * firstvalue = find (Values 10, // Search the Range VALUES 20, // Values [10] - Values [19] AGE); // for the value in agn The Find () function is not limited to it can only operate the INT type array, so it can actually be a template: Template T * Find (T * Begin, T * End, Const T & Value) { While (Begin! = End && * Begin! = Value) Begin Return Begin; } When turning into a template, note that we are changed by "pass value" to "pass CONST type reference". Because any type can now be handled, we have to consider the expense of the value. During each call, the parameters of each pass value must have the overhead of constructor and destructive functions. Through the pass reference (it does not need to construct and destruct any object), we avoid this overhead (see Effective C Item 22). This template is very beautiful, but it can also be more popular. Note the operation on Begin and End. Only "do not equal" comparison, reverse reference, prefixed (see more Effective C Item M6), copy (see more Effective C Item M19) for the function return value. This is all the operations we involved, so why do you limit Find () only use pointers? Why not allow other objects that support these operations? This will be liberated from the embedded pointer. For example, we can define a pointer object for a linked list, which will give yourself to the next element of the linked list. This is the concept of STL's choice. The selector is a class pointer object that is designed to operate STL. They are the tangible brother of the dexterous pointer in Item M28, but the function of the smart pointer is more powerful. However, from the technical point of view, their implementation uses the same skills. With the concept of chosen as a pointer object, we can replace the pointer in the Find () with the selection. Recovered Find () similar to: Template Iterator Find (Iterator Begin, Iterator End, Const T & Value) { While (Begin! = End && * Begin! = Value) Begin Return Begin; } Congratulations! You happen to write part of the standard template library. The STL contains many algorithms that use the package container and the selection, one of them. The package containers in STL include BitSet, Vector, List, Deque, Queue, Priority-Queue, Stack, SET, and MAP, you can use Find (), for example: List // for Holding Chars ... // Find the first occurrence of 'x' in charlist List Charlist.end (), 'x'); "Hey!", I heard you yell, "This is not like the example of using the array in front!" Ha, but it is the same; you just have to know what input. To call Find () to the List object, you must provide a selection of the first element in the list and a selection of the last element in the List. If the List class does not provide help, this will be a bit difficult because you can't know how List is implemented. Fortunately, List (like all other STL package containers) is ordered to provide member functions Begin () and end (). These member functions returns to the choices you need, for you to pass (). When Find () is executed, it returns a selection sub-object to point to the found element (if the element is either) or charlist.end () (if not). Because you don't know how List is implemented, it is impossible to know how the choice of the List is implemented. So, how do you know what type of object of Find () returns? Like all other STL bag containers, the list provides help: it provides a type of redefine, and the Iterator is the type of selected subsequent in the LIST. Since Charlist is a List that is inclusive, its internal selection subtype is List The same method is also entirely applicable to other STL bag containers. In addition, the C pointer is also a STL selector, so the example of the initial array can also apply STL's Find () function: Int Values [50]; ... INT * firstfive = find (Values, Values 50, 5); // Fine, Calls // STL FIND STL is actually very simple. It just collected class templates and function templates that follow the same rules. The class of STL provides functions like begin () and end (), which return the type defined in such a selection sub-object. STL's algorithm function operates these classes using the selection sub-object. STL's selection is similar to a pointer. This is all of STL. It does not have a huge inheritance, and there is no virtual function. Just affect the class template and function template, and the rules they have complied with. This results in another found: STL is expandable. You can add your own bag container, algorithm, and selector in the STL family. You always have to follow the STL rules, and the package container in STL will be able to use your algorithm, and your package container can also use STL algorithm. Of course, your template does not become part of the standard C run library, but they will be compiled with the same rules and can be reused. There are more things I have not written in the C run library. Before you can use the running library, you must jump out of the box in this child to learn more; before you write your own STL compatible template, you have to master more STL rules. The standard C running library is much more rich than the C library, take time to familiarize it is worth it (see Item E49). In addition, grasp the principles of design runners (common, expandable, customizable, efficient, and reuse) is also worth it. By studying standard C runtime, you don't lift knowledge knowing what is ready-made in your own software, you will also learn how to use C features more efficiently, you can also grasp how to design better code base.