"C++ Primer" Reading Notes-Chapter 3 Strings, Vectors, Arrays

Chapter 3 Strings, Vectors, Arrays

1. size_t represents an unsigned integer, which is an unsigned type.

  • The range of positive numbers it can represent is twice that of int.
  • When it is determined that the value of the variable must be greater than or equal to 0, use the size_t type instead of the int type.
  • Use size_t in a for loop

There are many benefits of using size_t, here is an article dedicated to these benefits:
Why size_t is important

2. The arithmetic problems of unsigned numbers and signed numbers

When performing operations on unsigned numbers and signed numbers, signed numbers will be converted to unsigned numbers. So we must pay attention to the following issues:

Insert picture description here

3. When cin reads a string, it ignores the blank part at the beginning

4. Problems that need to be paid attention to when adding strings

Ensure that at least one of the operands on both sides of each addition operator is a string

Insert picture description here
Insert picture description here

5. Function library for processing characters

Insert picture description here
Insert picture description here


3.4 Iterator

1. Iterator begin and end

begin points to the first element, end points to one bit after the last element

2. When traversing the container, use "<" instead of "!= end"

Insert picture description here
Insert picture description here

3. Arrow operator of iterator

Insert picture description here

4. Two operations that invalidate the iterator

  • Add elements to the vector object in the range for loop
  • Any operation that changes the capacity of a vector object

Insert picture description here


3.5 Array

1. When the exact number of elements is unclear, use vector.

2. The dimension of the array initialization should be known at compile time, so the dimension must be a constant expression (I have suffered many losses here)

Insert picture description here

3. Array does not allow copying and assignment

Insert picture description here

4. Understand the meaning of array declaration: from inside to outside, from right to left

Insert picture description here

5. To traverse the array, try to use the range for loop statement

Insert picture description here

6, the function to get the array iterator: begin function and end function

Note that you need to include the header file iterator before use

Insert picture description here

7. C-style strings

  • The string class contains an in-class member variable c_str, which saves an array of c-style characters.

Insert picture description here

  • C style string function It is
    Insert picture description here
    recommended not to use c style string as much as possible

8. Try to use vectors and iterators instead of pointers and arrays

Insert picture description here

Guess you like

Origin blog.csdn.net/youyadefeng1/article/details/112614461