Accelerated C++ Practical Programming by Example
Accelerated C++
Practical Programming by Example
by Andrew Koenig and Barbara E. Moo
Chapter 0 Getting started
0.1 Comments
0.2 #include
0.3 The main function
0.4 Curly braces
0.5 Using the standard library for output
std::cout << "Hello, world!" << std::endl;A name preceeded by std:: indicates that the name is part of a namespace named std.
A namespace is a collection of related names; the standard library uses std to contain all the names that it defines.
The name std::cout refers to the standard output stream, which is whatever facility the C++ implementation uses for ordinary output from programs.
0.6 The return statement
0.7 A slightly deeper look
An expression contains operators and operands.The << operator takes two operands, << is left-associative. The last output expression is equivalent to
(std::cout << "Hello, world!") << std::endlThe scope of a name is the part of a program in which that name has its meaning.
Curly braces form another kind of scope. The body of main and the body of every function is itself a scope.
0.8 Details
An expression followed by a semicolon is a statement, called an expression statement.The expression is optional; omitting it results in a null statement, which has no effect.
Chapter 1 Working with strings
1.1 Input
1.2 Framing a name
A character literal is always enclosed in single quotes; a string literal is always enclosed in double quotes.std::string stars(10, '*');then stars.size() would be 10, and stars itself would contain ********** .
1.3 Details
The string type is defined in the standard header <string>.An object of type string contains a sequence of zero or more characters.
- Built-in type that holds ordinary characters
char
wchar_t
std::string s;
std::string t = s;
std::string z(n, c);
Chapter 2 Looping and counting
2.1 The problem
2.2 Overall structure
2.3 Writing an unknown number of rows
2.4 Writing a row
std::size_t is the unsigned integer type of the result of the sizeof operator.std::size_t can store the maximum size of a theoretically possible object of any type (including array).
std::size_t is commonly used for array indexing and loop counting.
Programs that use other types, such as unsigned int, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX or if it relies on 32-bit modular arithmetic.
When indexing C++ containers, such as std::string, std::vector, etc, the appropriate type is the member typedef size_type provided by such containers. It is usually defined as a synonym for std::size_t.
std::string::size_type c = 0; while (c != cols) { // write one or more characters // adjust the value of c to maintain the invariant }
2.5 The complete framing program
C++ offers a way of saying that a particular name should always be interpreted as coming from a particular namespace.To use the name cout to mean std::cout exclusively:
using std::cout;such a declaration is called a using-declaration.
2.6 Counting
2.7 Details
Chapter 3 Working with batches of data
3.1 Computing student grades
std::ios_base::precision is a public member function which is used to get/set floating-point decimal precision.// modify precision #include <iostream> // std::cout, std::ios int main () { double f = 3.14159; std::cout.unsetf ( std::ios::floatfield ); // floatfield not set std::cout.precision(5); std::cout << f << '\n'; std::cout.precision(10); std::cout << f << '\n'; std::cout.setf( std::ios::fixed, std:: ios::floatfield ); // floatfield set to fixed std::cout << f << '\n'; return 0; }
3.2 Using medians instead of averages
A vector holds a sequence of values of a given type, grows as needed to accommodate additional values, and lets us get at each individual value efficiently.A vector is a container that holds a collection of values. All of the values in an individual vector are the same type, we must specify the type of the values that the vector will hold.
double x; vector<double> homework; // invariant: homework contains all the homework grades read so far while (cin >> x) homework.push_back(x);What push_back does is append a new element to the end of the vector .
留言