First, the preface
Looking at the 5th edition of C++primer recently, previously read the 4th edition, but found that the 5th version of the entire knowledge layout and the interpretation of individual knowledge with the 4th version of the difference is quite large, especially the new c++11 content, as Meng in the 5th Preface said: "Now can with the new C + + 11 Style development practice is rare, if you can skillfully use c++11 new features, new mechanisms, then can form a concise and elegant C + + programming wind, development will become more efficient, higher quality.
So just use the 5th edition to learn to consolidate C + + knowledge. These things in C + +, this series, will be in the form of knowledge fragments of my knowledge in the process of learning some of the key points.
Second, on C + + learning
The individual as C + + rookie, nature is without any experience, here excerpt c++primer5ed Foreword Some personally think very reasonable point of view.
1, one of the realms of language learning is to think of yourself as a compiler.
2, use the "two sides" view of C + + language. C + + is moving toward perfection, so the C + + language is worth learning (and even studying). This knowledge can be the basis of all programming. However, in practice, it is not necessary to use all the features of C + + language, but according to the actual situation of the project, appropriate trade-offs (such as dynamic type information, virtual inheritance, exceptions and other characteristics of the use). Usually only a subset of the C + + language is encouraged.
On the 2nd, the individual is quite agree, some of the advanced features of C + +, indeed only in some library routines in the embodiment, the general engineering development rarely can be used. So in the learning of C + +, the beginning does not have to some grammatical details or advanced techniques too much pursuit of perfection, can be the prophet of its probably, in the future work to learn again gradually deepen understanding.
Iii. Data and types
1, any programming language is composed of some basic components, C + + is the same.
Built-in types such as integral type, character type, etc.
Variable, which is used to name the object
Expressions and statements that manipulate the specific values of the above data type
If or while control structures that allow us to selectively execute some statements or to execute some statements repeatedly
Function that defines a calculated cell that can be invoked at any time
Custom data types: For example: Class
Standard library
Another point of C + + is the idea of programming, through the introduction of object-oriented programming ideas, and then through the template and other techniques to introduce universal or generic programming ideas.
In this article, it is mainly about the first 2 points: objects and types of objects.
2, type is the most basic concept in programming, a type not only defines the content of data elements, but also defines the operations that can be performed on such data. The built-in data types are the same for user-defined types, and a very important task for C + + is to make the type of user design as useful as the language built-in type.
3, the data processed by the program is stored in the variable, and each variable has its own type.
4, when performing arithmetic operations, the selection of data types
Short is generally rarely used and is prone to cross borders.
Char types are sometimes treated as signed in some implementations and sometimes as unsigned, so it is advisable to use char only to represent characters.
It is no problem to use int most of the time, the int is stored in 4 bytes on many machines, enough to meet most of the calculation requirements.
The choice of floating-point types is simple, with a double type with little or no error, and may be less expensive than the float calculation.
5, when using unsigned numbers as the index of a loop, note that the implicit data conversion between the unsinged int and the int type, as in the following procedure, may not actually be the result you want.
1//Error variable u will never be less than 0, the cycle condition has been set up
2 for (unsigned u = n > = 0;---u)
3 {
4 std::cout << u << std::endl;
5}
6,C++11 defines a new pointer literal nullptr, which replaces precompiled constants in the C language for null
7, note the difference between initialization and assignment in C + +, which is more evident in the definition of a class. Initialization is not an assignment, the meaning of initialization is to give an initial value when the variable is created, whereas the meaning of the assignment is to erase the current value of the object and replace it with a new value.
8, references and pointers are compound types defined by C + +, and references and pointers are defined by a basic data type and a list of declarations immediately following it.
1 The reference is a name for the variable, and when the reference is defined, the program binds the reference to its initial value rather than the initialization copy to the reference. Once the initialization is complete, the program binds the reference to its original value object. Because the reference cannot be rebind to another object, the reference must be initialized.
In fact, the reference is essentially a pointer, and is a constant pointer that occupies 4 bytes of space.
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/
2 Unlike references, the pointer itself is an object that allows the pointer to copy and assign values, and it can point to several different objects in the pointer's life cycle.
9, a constant expression is an expression that does not change the value and can get a computed result during compilation.
In a complex system, it is difficult to tell whether an initial value is a constant expression. C++11 allows the variable to be declared as a constexpr type so that the compiler can verify that the value of the variable is a constant expression. The variable declaring the constexpr must be a constant, and it has to be initialized with a constant expression:
1 constexpr int MF = 20;
2 constexpr int limit = MF + 1;
3 constexpr int sz = size (); A correct declaration statement is only when the size is a constexpr function
A pointer description decorated with CONSTEXPR is a constant pointer, which itself can no longer be changed after the definition is initialized, but the object that is referred to can be a variable.
10, note that pointers, constants, and type aliases are in one piece:
1 typedef char* pstring; Pstring is a pointer to a character
2 const pstring cstr=0; CStr is a constant pointer to a character
3 const pstring *ps; PS is a pointer whose object is a constant pointer to char.
11,auto type
C++11 introduces the auto type specifier, which allows the compiler to parse the type that the expression belongs to. The compiler calculates the type of the variable by its initial value, obviously, the variable that the auto defines must have an initial value.
If we use a reference type to initialize an auto type, the resulting type will be the type of the Reference object:
1 int i = 0, &r = i;
2 Auto a = R; A is an integer
Next auto will generally ignore the top level of the const, while the bottom of the const will be retained.
1 const int CI = i, &cr = CI;
2 Auto B = ci; B is an integer
3 Auto C = CR; CR is an integer (the top-level const feature of CI is ignored)
4 Auto D = &i; D is a pointer (pointer is a pointer to an integer)
5 Auto E = &ci; E is a pointer to an integer constant
If you want auto to infer that the auto type is a top-level const, you need to specify that:
1 Const Auto F = CI;
12,decltype Type Indicator
Sometimes you want to infer the type of the variable you want to define from the type of the expression, but do not want to initialize the variable with the value of the expression, and introduce a second type descriptor decltype for some c++11, which is to select and return the data type of the operand.
Decltype (f ()) sum = x; The type of sum is the return value type of f, and the compiler does not call F
Decltype handles the top-level const and references in a way that is different from auto.
1 const int CI = 0, &cj = ci;
2 Decltype (CI) x = 0; The type of x is a const int
3 Decltype (CJ) y = x; The type of y is const int&, Y is bound to the variable x
4 Decltype (CJ) Z; Error, Z is a reference that must be initialized
A reference is always made as a synonym for the object it refers to, except in the case of Decltype.
The result of Delctype ((variable)) (note that the double brackets) is always a reference, and decltype (variable) result is a reference only if the variable itself is a reference.
13, the standard library defines 2 very important abstract types, one of which is used to support variable-length strings, and the other is a vector that represents a variable-length collection.
14, there can be many ways to initialize string and vector objects, depending on the definition of the class, but both direct initialization and copy initialization exist. If you initialize a variable with the equal sign "=", you actually perform a copy initialization, and the compiler copies the initial value on the right side of the equal sign to the newly created object. Conversely, if no equals sign is used, direct initialization is performed.
1 string s1 = "Hiya"; Copy initialization
2 string s2 ("Hiya"); Initialize directly
3 string S3 (+, ' C '); Initialize directly
Both the 15,string type and the standard library container type provide a size_type type, which is an unsigned value and sufficient to hold any container or string object size. In c++11, we can use Auto or decltype to get the compiler to infer this type automatically without having to write very long code, such as:
1 Vector<vector<double>>::size_type i; The original method
2 Decltype (Dvv.size ()) I; C++11 with Decltype
3 Auto i = Dvv.size (); C++11 with Auto
In 16,c++11, a vector object is also provided with a method of list initialization, at which point 0 or more initialization element values enclosed in curly braces are assigned to the vector object. But it's worth noting that if you use curly braces, you can express that we want the list to initialize the vector object. Also, the initialization process treats the values in the curly braces as if they were a list of element initialization, and only considers other initialization when the list initialization cannot be performed.
1 vector<string> v1{"HI"}; V1 has an element
2 vector<string> v2 ("HI"); Error
3 vector<string> v3{10}; V3 originally wanted to initialize the vector with 10来, but found that 10 was not a Strring object, so the V3 was initialized to 10 elements.
4 vector<string> v4{10, "HI"}; V4 originally wanted to initialize the vector with 10 and "hi", but found that 10 was not a string object, so the V4 was defined as a vector of 10 elements.
17, arrays, similar to vectors, arrays are containers of objects of the same type, which have no name and need to be accessed through their location. And the array is in the definition of the time allocated size, the middle can not change, which will bring great inconvenience to use.
18, the pointer actually has the same function as the iterator, and in the case of traversing the array, the pointer operates as if using an iterator on the container. Similar to the Begin and end iterators of the container type, C++11 defines two function begin and ends to get the first and tail pointers of the sequence, which are defined in the iterator header file.
1 int ia[]={0,1,2,3,4,5,6,7,8,9};
2 int *beg = BEGIN (IA); A pointer to the first element of the IA
3 int *end = end (IA); A pointer to the next position of the IA tail element
Author: ☆ronny
Source: http://www.cnblogs.com/ronny/