C ++ Primer 5th learning notes (2), primer learning notes

Source: Internet
Author: User

C ++ Primer 5th learning notes (2), primer learning notes

C ++ Primer 5th learning notes (2)

Chapter 3 highlights and difficulties

You can click here to review the content of Chapter 1 and Chapter 2.

This article is just a learning note for C ++ beginners .... The book follows the previous article. Chapter 3 mainly describes the following five concepts:

1. using DeclarationI know that many developers need to add using namespace std directly in the first few sentences of the source file. However, the using statement is not used in all situations, we will see detailed usage later.

2. Standard library type stringIn this chapter, we will explain how to construct a string that is different from a C-flavor character array.

3. vector of the standard library type, The difference between vector and array is very big. Here we will introduce an important concept of "container.

4. iteratorThe iterator is used to access the container or some types that support the iterator instead of the subscript.

5. arrays and multi-dimensional arrays, Classic concept. It is better to use it with pointers.

 

The following are the knowledge points of this chapter:

Knowledge Point 1: P75, 3.1, header file and using statement

Although we can use the using namespace std; or using std: endl; statement in various files, the inclusion of namespace in the header file may cause various exceptions. Therefore, the header file should not contain the using declaration.

 

Knowledge Point 2: P76, 3.2.1, defining and initializing string objects

The following initialization statements are supported by strings:

String s1; // an empty string is created. The object name is s1 and the type is string.

String s2 (s1); // The s2 value is equal to the s1 value.

String s2 = s1; // In the same sentence, the copy is initialized.

String s3 ("value"); // use the string literal value to initialize a string-type object.

String s3 = "value"; // string literal value is converted to a string type variable and assigned to string.

String s4 (n, 'C'); // initialize string directly. After the operation, s4 has 10 characters, and the value of each character is 'C '.

Finally, string s5 = {"value"} and string s3 = "value" are also valid.

 

Knowledge Point 3: operations supported by P77, 3.2.2, and string

1. Get strings in the input stream The first thing to emphasize is the cin> string operation. This operation is to read the string from the input stream, it is worth noting that this process will ignore the various white spaces entered at the beginning (when we say white spaces, line breaks, and tabs ), read the input stream until the first blank space after the character is encountered, and the other getline (cin, string); operation can read a row, that is, read the data of the input stream (including spaces, tab), until a line break is encountered, the line break in the input stream itself has been read, but the line break is not saved in the string. The next time you read data from the input stream, you must at least perform operations on the input stream after the line break.

2. string: type_size: calls the size member function of each string object to make it more abstract. The returned value is of the string: type_size type, this type has some properties of the unsigned integer number. In the support of string pairs, numbers in [] will also be converted to the string: type_size type.

3. The string object and the string literal value are added. The string literal value is of the character array type. When the string literal value and the string object are calculated together, they are automatically converted to the string type.

4. Other supported operations: including subscript operators [], overloaded +, =, and ,! =, <,>, <=,> =.

 

Knowledge Point 4: P82, 3.2.3, range for (range for) Statement

The range for statement is used to traverse elements. For example: for (a variable used for accessing the basic element of the sequence a: accessed sequence object B) {statement ...... blabla ;}

During initial initialization, the value of variable a is initialized as the first element in the sequence B of the object. After iteration, the next element is accessed until the sequence is completely accessed.

You can use auto & a to declare variable a and bind the variable to a specific sequence element for modification. For example, in for (auto a: str) {}, each initialization of a is essentially to make a get a copy of each element of str (copy), and for (auto &: str) {} makes a "alias" of each element corresponding to str, so that you can modify str.

Use the range for loop to traverse multi-dimensional arrays. In order not to manually name the class, but to prevent the outer array from being auto-pointer, add & binding to the access to the external layer group.

 

Knowledge Point 5: P82, 3.2.3. process the header file cctype of each character

Each character can be processed by referencing the header file <cctype>. This header file contains many functions that facilitate character processing. List as follows:

Isalnum (c); // isalpha (c) is true when c is a letter or number; // true when c is a letter

Iscntrl (c); // true when c is a control character

Isdigit (c); // isgraph (c) is true when c is a number; // true when c is not a space but can be printed

Islower (c); // when c is lowercase letters true

Isprint (c); // isupper (c) is true when c is printable; // true when c is an uppercase character

Isxdigit (c); // true when c is a 16-digit number

Ispunct (c); // It is true when c is a punctuation character (only control characters, letters, numbers, printable spaces are punctuation characters)

Isspace (c); // true when c is blank (blank space, horizontal/vertical tab, carriage return, line feed, paper feed)

Tolower (c); // converts an uppercase character to a lowercase character, which is originally a lowercase character, and returns the converted character

Toupper (c); // converts lowercase to uppercase, which is the same as uppercase. The converted characters are returned.

 

Knowledge Point 6: P87, 3.3, class template, container, and instantiation

When talking about the container concept in C ++, we should know that containers are used to store and organize a set of specific objects. The vector of the standard library type mentioned below is a container.

A class template is generally used to generate different classes according to the rules specified by the template. We don't need to bother writing the definition of classes one by one. We only need to use the template to give a small amount of specified information, and the class template will help us automatically generate a class that we can directly use. Vector is also a class template.

The process of creating a class through a class template or creating an object through a type is called Instantiation.

 

Knowledge Point 7: P87, 3.3.1, define and initialize a vector object

Like the definition and initialization of string, we can also define and initialize a vector object in multiple ways.

The following initialization statements are supported by strings:

Vector <Type> v1; // creates an empty vector container, which is a collection of Type objects. The Set Name Is v1 and is initialized by default.

Vector <Type> v2 (v1); // creates a vector container named v2, which has the same content as v1.

Vector <Type> v2 = v1; // same as the previous sentence, copy initialization.

Vector <Type> v3 {a, B, c}; // v3 contains the element of the number of initial values.

Vector <Type> v4 = {a, B, c}; // same as above.

Vector <Type> v5 (n, val); // initialize the container directly. After the operation, v5 has n elements, and the value of each element is val.

Vector <Type> v5 (n); // initialize the container directly. After the operation, v5 has n elements, and the value of each element is initialized by default.

When we use parentheses () to initialize an object, IDE will think that we are "building" this object through the statement; when we use curly braces {} to initialize the object, IDE will think that we are initializing the object.

When we use equal sign = to initialize an object, we execute "Copy initialization". When we do not use = to initialize an object, we execute "direct initialization ".

But when we give a value in curly brackets that does not conform to the object type, the system will think that we are building rather than initializing the object,The expression is: vector <string> s1 {10}. In this statement, 10 cannot be converted to string. Therefore, it is understood as "this string container contains 10 elements ". Of course, statements like vector <string> s1 = {10}; are incorrect, because = should be the copy initialization, however, 10 cannot be converted to a string, so it cannot be assigned a value.

 

Knowledge point 8: P90, 3.3.2, operations supported by vector

1. Add an element to the end of the container: The vector <T> v; already exists. You can add an element to the end of the v set by using v. push_back (vector <T>.

2. empty and size function members: a vector <T> v; already exists. You can use v. empty () method to determine whether v is null. You can use v. size () returns the value of v.

3. overloaded operators: The operators supported by vector include subscript operators [], overloaded +, =, and ,! =, <,>, <=,> =. This is similar to string.

 

Knowledge Point 9: P95, 3.4.1, iterator

To access the elements of the container (some containers may not support subscript operators), C ++ provides the iterator to access the specified elements in the container.

All classes that support the iterator provide function members named begin and end for us to obtain the iterator. If vector <int> i1 (10) has been defined, auto ben = i1.begin () is used. This statement obtains the iterator pointing to the first character and uses auto end = i1.end (); gets the iterator that points to the next element of the last element of the i1 container. The term "post-iteration" is used ".

When vector <int> is used to create a class, the namespace of this class is vector <int>. The iterator type in the namespace is written as vector <int >:: iterator. This is because the iterator type name "vector <int>: iterator" is too long to remember. Here we use auto to deduce this type. The member functions cbegin and cend can be used to export the underlying const iterator. This iterator is read-only and does not write the content pointed to by the iterator. Chapter 1 describes in detail.

The content of the container pointed to by the iterator can be used like the pointer to the array element pointed to by the pointer.

Knowledge Point 10: P102, 3.5.1, definition and initialization of one-dimensional arrays

One-dimensional array declaration form: type name array name [a constant]. For example, int a [15]; here the array name is a, with 15 elements, each of which is int type. For example, int * a [15]; here, the 15 elements of array a are all int * type, that is, the pointer to the int, and 15 such pointers constitute an array. Although there is a pointer array, but notAll elements are arrays of reference type..

The initialization method of one-dimensional arrays is curly braces, for example, int a [n] = {1, 2, 3}. The content in the braces is the initialization list. n is the array size, which can be the default value, the default number of hours is determined by the number of elements in the initialization list. When the number of values in the initialization list is smaller than the array length, the remaining elements of the array are initialized to the default value. For example, for an int-type array with 10 elements, if only the value of the first element is given, the last few elements are initialized to 0.

When int a [] is declared, the array is accessed by array name. We can also define a pointer to an array and a reference to an array to indirectly access this array. If int arr [10]; exists, int (* ptr) [10] = & arr; this statement can point the pointer ptr to the entire array of arr. Int (& ref) [10] = arr; then, the ref will be referenced as the entire arr array. Int * (& ref) [10] = arr; this statement indicates that ref is a reference to arr. the type of the referenced array is a pointer array.

Auto a = an array name. The type of a will be this pointer. The Pointer Points to the type of the array element. Decltype (an array name) a; in this form, a will be an array consistent with the array name attribute.

Knowledge Point 11: P111, 3.5.5. Use an array to initialize a vector object and assign a value to a string array.

As an interface with the old Code, C ++ provides a convenient way to convert arrays into vector objects. When declaring a vector object, we can use the iterator to initialize the vector with an array. If int oldarray [10]; already exists, the declared statement is like: vector <int> arr (begin (oldarray), end (oldarray )); you can initialize arr to oldarray. In the header file of <iterator>, the begin and end functions are used to return the first element/trailing pointer of the array. This initialization takes two parameters: copy the start pointer and end pointer. We can also write data such as int arr [10] = {0}; vector <int> newarr (arr + 1, arr + 4); in this way, copy the 2nd ~ Element 5th and use them to initialize the newarr.

Similarly, we can use string a ("23333333 \ n"); const char * B =. c_str (); such a statement assigns string type a to character array pointer B. The returned result is const to ensure that we do not use this pointer to change the value of the returned character array. If we change the character array through the returned pointer, the value of the string is changed. Therefore, this operation is not allowed. That's it.

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.