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
hold characters for languages such as Japanese.
using std::cout;
size_t
<cstddef>
)
that can hold any object's size string::size_type
string
<ios>
header defines streamsize
, which is
the type that the input-output library uses to represent sizes. <iomanip>
header defines the manipulator
setprecision
, which lets us say how many significant digits we want
our output to contain.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());
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;
T
. v.push_back(e)
e
. s.precision(n)
s
to n
for future output (or leaves it unchanged if n
is omitted). Returns
the previous precision. setprecision(n)
s
, has the effect of calling s.precision(n)
. Defined
in <iomanip>
. streamsize
setprecision
and returned by precision
. Defined in <ios>
.
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;
const
promises that we will not do anything to chw that might
change its value.const
reference" refer to a const
object or referencevector<double>& hw2 = chw; // error: requests write access to chw
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; { }
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.// this example doesn't work
try {
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< grade(midterm, final, homework) << setprecision(prec);
} ...
// 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;
}
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
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()