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:
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
5. Function library for processing characters
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"
3. Arrow operator of iterator
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
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)
3. Array does not allow copying and assignment
4. Understand the meaning of array declaration: from inside to outside, from right to left
5. To traverse the array, try to use the range for loop statement
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
7. C-style strings
- The string class contains an in-class member variable c_str, which saves an array of c-style characters.
- C style string function It is
recommended not to use c style string as much as possible