-------------------------------------------------------
Accelerated C++
** Chapter 0 Getting Started **
--programming language
·core language
·standard library - #include directives - standard header
--iostream
The name iostream suggests support for sequential, or stream, input-output, rather than random-access or graphical input-output.
--namespace
standard library's output operator - "<<"
The name std::cout refers to the standard output stream.
--iostream
In a typical C++ implementation under a windowing operating system, std::cout will denote the window that the implementation associates with the program while it is running. Under such a system, the output written to std::cout will appear in the associated window.
--type & operand
std::cout, "Hello, world! " and std::endl are operands.
Every operand has a type.
std::cout has type std::ostream.
left-associative 左结合
--manipulator
std::endl is a manipulator, the key property of which is that writing a manipulator on a stream manipulates the stream, by doing something other than just write characters to it.
** Chapter 1 Working with strings **
--object vs. variable
An object is a part of the computer's memory that has a type.
The distinction between objects and variables is that it is possible to have objects that do not have names.
The limited lifetime of local variables is one reason that it is important to distinguish between variables and other object.
--string
the standard library says that every string object starts out with a value.
--I/O buffer
There are three events that cause the system to flush the buffer. First, the buffer might be full, in which case the library will flush it automatically. Second, the library might be asked to read from the standard input stream. In that case, the library immediately flushes the output buffer without waiting for the buffer to become full. The third occasion for flushing the buffer is when we explicitly say to do so.
--const
If we say that a variable is const, we must initialize it then and there, and we promise that the its value will not change after we have defined it.The value that we use to initialize the const variable need not itself be a constant.
--character literal
The type of a character literal is the build-in type char.
** Chapter 2 Looping and counting **
--increment operator
Incrementing an object is so common that a special notation for doing so is useful.
?? The idea of transforming a value into its immediate successor, in contrast with computing an arbitrary value, is so fundamental to abstract data structures that it deserves a special notation for that reason alone.
--loop invariant 循环不变式
Stating the invariant in a comment can make a while much easier to understand.
?? $2.3
--class scope
Like namespaces and blocks, classes define their own scopes.
--string::size_type - type-object
Whenever we need a local variable to contain the size of a string, we should use std::string::size_type as the type of that variable to ensure that the variable is capable of containing the number of characters in a string.
Nevertheless, whenever we define a variable that contains the size of a particular data structure, it is a good habit to use the type that the library defines as being appropriate for that specific purpose.
--for-statement
Asymmetric ranges are usually easier to use than symmetric ones because of an important property: A range of the form [m, n) has n - m elements, and a range of the form [m, n] has n - m + 1 elements.
This behavioral difference between asymmetric and symmetric ranges is particularly evident in the case of empty ranges: If we use asymmetric ranges, we can express an empty range as [n, n), in contrast to [n, n-1] for symmetric ranges. The possibility that the end of a range could ever be less than the beginning can cause no end of trouble in designing programs.
--C++ Programming Language
C++ inherits a rich set of operators from C, in addition, C++ programs can extend the core language by defining what it means to apply built-in operators to objects of class type.
--Data type
·size_t
Unsigned integral type (from <cstddef>) that can hold any object's size
·string::size_type
Unsigned integral type that can hold the size of any string
** Chapter 3 Working with batches of data **
--standard library
The <ios> header defines streamsize, which is the type that the input-output library uses to represent sizes.
The <iomanip> header defines the manipulator setprecision, which lets us say how many significant digits we want our output to contain.
--end-of-file signal
Ctrl+z Microsoft Windows
Ctrl+d Unix/Linux systems
--double vs. float
Even though it might seem that float is the appropriate type, it is almost always right to use double for floating-point computations.
We could have avoided this conversion by initializing sum to 0.0 instead of 0, but it makes no practical difference in this context: Any competent implementation will do the conversion during compilation, so there is no run-time overhead, and the result will be exactly the same.
--Initialization
Local variables of built-in type that are not explicitly initialized are undefined, which means that the variable's value consists of whatever random garbage already happens to occupy the memory in which the variable is created. It is illegal to do anything to an undefined value except to overwrite it with a valid value.
--set and reset cout's precision
Nevertheless, we believe that it is wise to reset cout's precision to what it was before we changed it.
// set precision to 3, return previous value
streamsize prec = cout.precision(3);
cout << "Your final grade is "
<< 0.2 * midterm + 0.4 * final + 0.4 * sum / count << endl;
// reset precision to its original value
cout.precision(prec);
However, we prefer to use the setprecision manipulator, because by doing so, we can minimize the part of the program in which the precision is set to an unusual value.
--condition
When used in a condition, the arithmetic value is converted to a bool: Nonzero values convert to true; zero values convert to false.
--cin as condition
There are several ways in which trying to read from a stream can be unsuccessful:
·We might have reached the end of the input file.
·We might have encountered input that is incompatible with the type of the variable that we are trying to read, such as might happen if we try to read an int and find something that isn't a number.
·The system might have detected a hardware failure on the input device.
--<algorithm> header
sort(v.begin(), v.end()) function rearrange the values in a container so that they are in nondecreasing order. The sort function moves the values of the container's elements aroud rather than creating a new container to hold the results.
--vector
The references to homework[mid] and v[index] show one way to access an element of a vector.
--size_type
Moreover, whenever ordinary integers and unsigned integers combine in an expression, the ordinary integer is converted to unsigned. In consequence, expressions such as homework.size() - 100 yield unsigned results, which means that they, too, cannot be less than zero—even if homework.size() < 100.
** Chapter 4 Organizing programs and data **
--The library facilities share several qualities: Each one
1.Solves a particular kind of problem
2.Is independent of most of the others
3.Has a name
--Organizing programs and data
Like most programming languages, C++ offers two fundamental ways of organizing large programs:
+--1.functions
+--2.data structure s
+--> class
--vector<T>::clear()
--istream::clear()
// read homework grades from an input stream into a vector<double>
istream& read_hw(istream& in, vector<double>& hw)
{
if (in) {
// get rid of previous contents
hw.clear() ;
// read homework grades
double x;
while (in >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
in.clear();
}
return in;
}
Note that the clear member behaves completely differently for istream objects than it does for vector objects. For istream objects, it resets any error indications so that input can continue; for vector objects, it discards any contents that the vector might have had, leaving us with an empty vector again.
--A good rule of thumb好的经验
A good rule of thumb is to avoid more than one side effect in a single statement. Throwing an exception is a side effect, so a statement that might throw an exception should not cause any other side effects, particularly including input and output.
--函数命名
istream& read(istream& is, Student_info& s)
There is no ambiguity in naming this function read, because the type of its second parameter will tell us what we're reading.
-------------------------------------------------------
Programming Windows
--sz
string terminated with a zero
--win32 header files
windows.h
windef.h base type definition
WINAPI - __stdcall
winnt.h type for unicode support
LPSTR PSTR
winbase.h kernel functions
WinMain
winuser.h UI functions
wingdi.h GDI functions
--Hungarian Notation
-------------------------------------------------------
Visual Studio 6
--Open standard headers with <Find> in VS.
--Export a makefile in VS.
924

被折叠的 条评论
为什么被折叠?



