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::endl
The 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
        
  • Built-in type intended to hold "wide characters," which are big enough to hold characters for languages such as Japanese.
  • 
        wchar_t
        
  • Defines s as a variable of type std::string that is initially empty.
  • 
        std::string s;
        
  • Defines t as a variable of type std::string that initially contains a copy of the characters in s , where s can be either a string or a string literal.
  • 
        std::string t = s;
        
  • Defines z as a variable of type std::string that initially contains n copies of the character c . Here, c must be a char
  • 
        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 .

3.3 Details

Chapter 4 Organizing programs and data

4.1 Organizing computations 4.2 Organizing data 4.3 Putting it all together 4.4 Partitioning the grading program 4.5 The revised grading program 4.6 Details

Chapter 5 Using sequential containers and analyzing strings

5.1 Separating students into categories 5.2 Iterators 5.3 Using iterators instead of indices 5.4 Rethinking our data structure for better performance 5.5 The list type 5.6 Taking strings apart 5.7 Testing our split function 5.8 Putting strings together 5.9 Details

Chapter 6 Using library algorithms

6.1 Analyzing strings 6.2 Comparing grading schemes6.3 Classifying students, revisited 6.4 Algorithms, containers, and iterators 6.5 Details

Chapter 7 Using associative containers

7.1 Containers that support efficient look-up 7.2 Counting words 7.3 Generating a cross-reference table 7.4 Generating sentences 7.5 A note on performance 7.6 Details

Chapter 8 Writing generic functions

8.1 What is a generic function? 8.2 Data-structure independence 8.3 Input and output iterators 8.4 Using iterators for flexibility 8.5 Details

Chapter 9 Defining new types

9.1 Student_info revisited 9.2 Class types 9.3 Protection 9.4 The Student_info class 9.5 Constructors 9.6 Using the Student_info class 9.7 Details

Chapter 10 Managing memory and low-level data structures

10.1 Pointers and arrays 10.2 String literals revisited 10.3 Initializing arrays of character pointers 10.4 Arguments to main 10.5 Reading and writing files 10.6 Three kinds of memory management 10.7 Details

Chapter 11 Defining abstract data types

11.1 The Vec class 11.2 Implementing the Vec class 11.3 Copy control 11.4 Dynamic Vecs 11.5 Flexible memory management 11.6 Details

Chapter 12 Making class objects act like values

12.1 A simple string class 12.2 Automatic conversions 12.3 Str operations 12.4 Some conversions are hazardous 12.5 Conversion operators 12.6 Conversions and memory management 12.7 Details

Chapter 13 Using inheritance and dynamic binding

13.1 Inheritance 13.2 Polymorphism and virtual functions 13.3 Using inheritance to solve our problem13.4 A simple handle class 13.5 Using the handle class 13.6 Subtleties 13.7 Details

Chapter 14 Managing memory (almost) automatically

14.1 Handles that copy their objects 14.2 Reference-counted handles 14.3 Handles that let you decide when to share data 14.4 An improvement on controllable handles 14.5 Details

Chapter 15 Revisiting character pictures

15.1 Design 15.2 Implementation 15.3 Details

Chapter 16 Where do we go from here?

16.1 Use the abstractions you have 16.2 Learn more

Appendix A Language details

A.1 Declarations A.2 Types A.3 Expressions A.4 Statements

Appendix B Library summary

B.1 Input-output B.2 Containers and iterators

留言

熱門文章