`

accelerated c++ 学习笔记

 
阅读更多

Chapter 0

 

A namespace is a collection of related names。Namespaces are a mechanism for grouping related names.

  the standard library uses std to contain all the names that it defines. So, for example, the iostream standard header defines the names cout and endl , and we refer to these names as std::cout and std::endl .

 

output operator , <<。 << is left-associative , which, loosely speaking, means that when << appears twice or more in the same expression, each << will use as much of the expression as it can for its left operand, and as little of it as it can for its right operand.

 

An expression contains operators and operands。Every operand has a type。 a type denotes a data structure and the meanings of operations that make sense for that data structure.

 

std::endl , which is a manipulator。 << does whatever the manipulator says to do to the given stream, and returns the stream as its result。

 

The scope of a name is the part of a program in which that name has its meaning。

  The standard library defines all of its names in a namespace named std , so that it can avoid conflicts with names that we might define for ourselves-as long as we are not so foolish as to try to define std .

 

:: operator. This operator is also known as the scope operator。

  std::cout means "the name cout that is in the (namespace) scope std ."

 

Types define data structures and operations on those data structures.

  C++ has two kinds of types: those built into the core language, such as int , and those that are defined outside the core language, such as std::ostream .

 

The main function: A zero return from main indicates success; a nonzero return indicates failure.

  It may omit the return; explicitly including a return from main is good practice

 

 

chapter 1

 

three events that cause the system to flush the buffer.

  First, the buffer might be full.

  Second, the library might be asked to read from the standard input stream.

  The third occasion for flushing the buffer is when we explicitly say to do so.

 

+ to concatenate a string

  the + operator (and, for that matter, the >> operator) is also left-associative.

  s+t : Either s or t , but not both, may be a string literal or a value of type char .

 

When an operator has different meanings for operands of different types, we say that the operator is overloaded .

 

string construct method:

std::string spaces(greeting.size(), ' ');

 

wchar_t

  Built-in type intended to hold "wide characters," which are big enough to
hold characters for languages such as Japanese.

using -declaration
using std::cout;

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

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.

The endl manipulator is used so often that its definition appears in <iostream>

setprecision(3) , we ask the implementation to write grades with three significant digits, generally two before the decimal point and one after.


sort(homework.begin(), homework.end());
The sort function, defined in the <algorithm> header, rearranges the values in a container so that they are in nondecreasing order.

typedef type name ;    Defines name as a synonym for type.

 

vector<T> v;

Creates an empty vector that can hold elements of type T .

v.push_back(e)

Grows the vector by one element initialized to e .

s.precision(n)

Sets the precision of stream s to n for future output (or leaves it unchanged if n is omitted). Returns the previous precision.

setprecision(n)

Returns a value that, when written on an output stream s , has the effect of calling s.precision(n) . Defined in <iomanip> .

streamsize

The type of the value expected by setprecision and returned by precision . Defined in <ios> .


chapter 4

Saying that a name is a reference to an object says that the name is another name for the object
  const vector<double>& , that we specify for the third argument. This type is often called "reference to vector of const double ." 

// chw is a read-only synonym for 

homework
const vector<double>& chw = homework;
the const promises that we will not do anything to chw that might change its value.

we cannot make "a nonconst reference"  refer to a const object or reference
vector<double>& hw2 = chw;    // error: requests write access to 

chw

several functions with the same name is called overloading.

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;

try statement. It tries to execute the statements in the { } that follow the try keyword. If a domain_error exception occurs anywhere in these statements, then it stops executing them and continues with the other set of { } -enclosed statements. These statements are part of a catch clause , which begins with the word catch , and indicates the type of exception it is catching.

A good rule of thumb is to avoid more than one side effect in a single statement.
// this example doesn't work




try {
streamsize prec = cout.precision();
cout << "Your final grade is  " << setprecision(3)
     << grade(midterm, final, homework) << setprecision(prec);
} ...
another method is better:
// compute and generate the final grade, if possible 
    try  {
        double final_grade = grade(midterm, final, homework);
        streamsize prec = cout.precision();
        cout << "Your final grade is " << setprecision(3)
             << final_grade << setprecision(prec) << endl; 
    } catch (domain_error) {
        cout << endl << "You must enter your grades.  " 
                        "Please try again." << endl;
        return 1;
    }
 
This struct definition says that Student_info is a type, which has four data members.
struct Student_info {
    string name;
    double midterm, final;
    vector<double> homework;
};  // note the semicolon it's required
 


chapter 4

Exception classes: The library defines several exception classes whose names suggest the kinds of problems they might be used to report:

logic_error     domain_error      invalid_argument
length_error    out_of_range      runtime_error
range_error     overflow_error    underflow_error

e.what()

Returns a value that reports on what happened to cause the error.





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics