Common head files and definitions






These two definitions are machine independent.



Because these are preprocessor variables, we must not precede them with std::, nor may we mention them in using declarations.

None Preprocessor


using std::string

string::size_type (unsigned) : 体现了标准库类型与机器无关的特性

string::difference_type (signed) : 体现了标准库类型与机器无关的特性

c_str(): 成员函数,返回值为C类型的字符串,即指向一个以'\0'结束的字符数组的指针const char *。我们无法保证c_str返回的一直有效,因为如果后续操作改变了原来的string,那么c_str的返回值就会失效。如果想一直使用c_str的返回值,最后将返回的字符数组复制一份。


using std::vector Note: vector is a template, not a type.

vector<T>::size_type (wrong: vector::sizetype) : 体现了标准库类型与机器无关的特性。

vector<T>::difference_type (signed) : 体现了标准库类型与机器无关的特性。


int int_arr[] = {0, 1, 2, 3, 4, 5};

vector<int> ivec(begin(int_arr), end(int_arr)); // 注意,第二参数所表示的对象并不会称为ivec中的对象。


size_t Note: 使用数组下标的时候,通常使用size_t类型,它是一种与机器相关的无符号类型,被设计的足够大以便能够表示内存中任意对象的大小。

ptrdiff_t (signed) Note: 两个指向同一数组(不同)元素的指针相减结果的类型,使以种与机器相关的有符号类型。


Note: cctype和ctype.h的内容完全一样,不同的是,cctype中的名字从属于命名空间std,而ctype.h则不然。




bad_alloc: If new is unable to allocate the requested storage, it throws an exception of type bad_alloc.

nothrow: We can prevent new from throwing an exception by using a different form of new: int *p2 = new (nothrow) int; // if allocation fails, new returns a null pointer


runtime_error: 只有在运行时才能检测出的问题。 (catch(runtime_error err){cout << err.what();} what()成员函数返回C风格字符串(const char*)。每个标准库异常类都定义了名为what的成员函数。)

The library exception classes have only a few operations. We can create, copy, and assign objects of any of the exception classes.

exception, bad_alloc, and bad_cast objects can only be initialized by default initialization, it is not possible to provide an initializer for objects of these exception types.

The other exception types have the opposite behavior: We can initialize those objects from either a string for a C-style string, but we cannot default initialize them. When we create objects of any of these other exception types, we must supply an initializer. That initializer is used to provide additional information about the error that occured.

The exception types define only a single operation name what. That function takes no arguments and returns a const char* that points to a C-style character string. The purpose of this C-style string is to provide some sort of textual description of the exception thrown.

The contents of the C-style string that what returns depends on the type of the exception object. For the types that take a string initailizer, the what function returns that string. For the other types, the value of the string that what returns varies by compiler.

Functions are exited during the search for a handler

In complicated systems, the execution path of a program may pass through multiple try blocks before encountering code that throws an exception. For example, a try block might call a function that contains a try, which calls another function with its own try, and so on.

The search for a handler reversed the call chain. When an exception is thrown, the function that threw the exception is searched first. If no matching catch is found, that function terminates. The function that called the one that threw is searched next. If no handler is found, that function also exits. That function’s caller is searched next, and so on back up the execution path until a catch of an appropriate try is found.

If no appropriate catch is found, execution is transferred to a library function named terminate. The behavior of that function is system dependent but is guaranteed to stop further excution of the program.

Exceptions that occur in programs that do not define any try blocks are handled in the same manner: After all, if there are no try blocks, there can be no handlers. If a program has no try blocks and an exception occurs, then terminate is called and the program is exited.



The preprocessor defines five names that can be useful in debugging:

__func__ local static array of const char that holds the name of the function

__FILE__ string literal containing the name of the file

__LINE__ integer literal containing the current line number

__TIME__ string literal containing the time the file was compiled

__DATE__ string literal containing the date the file was compiled


#ifndef NDEBUG
cerr << __func__ << ": array size is " << size << endl;


电子邮件地址不会被公开。 必填项已用*标注

17 + 10 =

− 3 = 3