SUMMARY OF CHAPTER 3-4

CHAPTER 3

SECTION 3.1-3.5

        1. the standard library defines a number of higher level abstract data types. They are abstract because when we use them we don't need to care about how the types are represented. We need to know only what operations they support.

        2. There is one case in which we should always use the fully qualified library names: inside header files.

        3. For historical reasons, and for compatibility with C, character string literals are not the same type as the standard library string type.

        4. The newline that causes getline to return is discarded; it does not get stored in the string.

        5. Any variable used to store the result from the string size operation ought to be of type string::size_type. It is particularly important not to assign the return from size to an int.

        6. When mixing strings and string literals, at least one operand to each + operator must be of string type.

        7. A vector is a class template. Templates let us write a single class or function definition that can be used on a variety of types.

        8. vector is not a type; it is a template that we can use to define any number of types. Each ofvectortype specifies an element type. Hence,vector<int> andvector<string> are types.

       9. vector initialization: the element type might be of a class type that does not define any constructors. In this case, the library still creates a value-initialized object. It does so by value-initializing each member of that object.

       10. To use size_type, we must name the type in which it is defined.

       11. C++ programmers tend to write loops using != in preference to < as a matter of habit.

       12. of-the-end iterator indicates that it refers to a nonexistent element "off the end" of the vector. If the vector is empty, the iterator returned by begin is the same as the iterator returned by end.

       13. 

//an iterator that cannot write elements
vector<int>::const_iterator
//an iterator whose value cannot change
const vector<int>::iterator

       14. Computes the difference between two iterators as a value of a signed integral type named difference_type, which, like size_type, is defined by vector.

       15. Any operation that changes the size of a vector makes existing iterators invalid. For example, after calling push_back, you should not rely on the value of an iterator into the vector.

       16. the bitset class is a class template.

       17. The bits are read from the string from right to left. The numbering conventions of strings and bitsets are inversely related: The rightmost character in the string--the one with the highest subscript--is used to initialize the low-order bit in the bitset--the bit with subscript 0. When initializing a bitset from a string, it is essential to remember this difference.

       18. In general, the library classes should be used in preference to low-level array and pointer alternatives built into the language.


CHAPTER 4

SECTION 4.1-4.4

        1. Well designed programs use arrays and pointers only in the internals of class implementations where speed is essential.

        2. There are no arrays of references.

        3. Regardless of where the array is defined, if it holds elements of a class type and it the class does not have a default constructor, then the elements must be explicitly initialized.

        4. When we create a character array from a string literal, the null is also inserted into the array.

        5. Pointers are iterators for arrays: A pointer can point to an element in an array.

        6. pointers can be used to point at single objects. Iterators are used only to access elements in a container.

        7. Uninitialized pointers are a common source of run-time errors.

        8. If you must define a pointer separately from pointing it at an object, then initialize the pointer to zero. The reason is that a zero valued pointer can be tested and the program can detect that the pointer does not point to an object.

        9. The type void* is a special pointer type that can hold an address of any object.

        10. It is legal to compute an address one past the end of  an array or object. It is not legal to dereference a pointer that holds such an address. Nor is it legal to compute an address more than one past the end of an array or an address before the beginning of an array.

        11. 

A. Pointers to const Objects       const double *cptr;

        A.1 The const qualifies the type of the object to which cptr points, not cptr itself. That is, cptr itself is not const.

        A.2 It is also a compile-time error to assign the address of a const object to a plain.

        A.3 We cannot use a pointer to const to change the underlying object. However, if the pointer addresses a nonconst object, it is possible that some other action will change the object to which the pointer points.

        A.4 It may be helpful to think of pointers to const as "pointers that think they point to const".

B. const Pointers       int errNumb = 0; int *const curErr = &errNumb; 

        B.1 As with any const, we must initialize a const pointer when we create it.

        12.

typedef string *pstring;
const pstring cstr;

When we declare a const pstring, the const modifies the type of pstring, which is a pointer. Therefore, this definition declares cstr to be a const pointer to string.

The definition is equivalent to:

string *const cstr;

        13. C-style strings are not actually a type in either C or C++. Instead, C-style strings are null-terminated arrays of characters.

        14. Never Forget About the Null-Terminator.

        15. When Using C-Style Strings, Use the strn Functions.

        16. Unlike an array variable, a dynamically allocated array continues to exist until it is explicitly freed by the program.

        17. C programs use a pair of functions named malloc and freeto allocate space from the free store. In C++ we use new and delete expressions.

        19. The elements of a dynamically allocated array can be initialized only to the default value of the element type. The elements cannot be initialized to separate values as can be done for elements of an array variable.

        20. It Is Legal to Dynamically Allocate an Empty Array. The language specifies that a call to new to create an array of size zero is legal. It is legal even though we could not create an array variable of size 0:

char arr[0];//error:cannot define zero-length array
char *cp = new char[0];//ok:but cp can't be dereferenced

        21. When defining a pointer to a multidimensioned array, it is essential to remember that what we refer to as a multidimensioned array is really an array of arrays.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值