Tải bản đầy đủ (.pdf) (20 trang)

C++ Primer Plus (P71) pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (46.02 KB, 20 trang )

template<class ForwardIterator>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class Compare>
ForwardIterator min_element(ForwardIterator first, ForwardIterator last,
Compare comp);
The min_element() function returns the first iterator it in the range [first, last) such
that no element in the range is less than *it. The first version uses < to determine the
ordering, while the second version uses the comp comparison object.
template<class ForwardIterator>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last);
template<class ForwardIterator, class Compare>
ForwardIterator max_element(ForwardIterator first, ForwardIterator last,
Compare comp);
The max_element() function returns the first iterator it in the range [first, last) such
that there is no element that *it is less than. The first version uses < to determine the
ordering, while the second version uses the comp comparison object.
template<class InputIterator1, class InputIterator2>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2);
template<class InputIterator1, class InputIterator2, class Compare>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
Compare comp);
The lexicographical_compare() function returns true if the sequence of elements in
range [first1, last1) is lexicographically less than the sequence of elements in range
[first2, last2) and false otherwise. A lexicographical comparison compares the first
element of one sequence to the first of the second, that is, it compares *first1 to
*first2. If *first1 is less than *first2, the function returns true. If *first2 is less than
*first1, the function returns false. If the two are equivalent, comparison proceeds to
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
the next element in each sequence. This process continues until two corresponding


elements are not equivalent or until the end of a sequence is reached. If two
sequences are equivalent until the end of one is reached, the shorter sequence is
less. If the two sequences are equivalent and of the same length, neither is less, so
the function returns false. The first version of the function uses < to compare elements
and the second version uses the comp comparison object. The lexicographic
comparison is a generalization of an alphabetic comparison.
Permutations
A permutation of a sequence is a reordering of the elements. For example, a
sequence of three elements has six possible orderings, because you have a choice of
three elements for the first element. Choosing a particular element for the first position
leaves a choice of two for the second, and one for the third. For example, the six
permutations of the digits 1, 3, and 5 are as follows:
123 132 213 232 312 321
In general, a sequence of n elements has n*(n-1)*…*1, or n! possible permutations.
The permutation functions assume that the set of all possible permutations can be
arranged in lexicographical order, as in the previous example of six permutations. That
means, in general, that there is a specific permutation that precedes and follows each
permutation. For example, 213 immediately precedes 232, and 312 immediately
follows it. However, the first permutation (123 in the example) has no predecessor,
and the last permutation (321) has no follower.
template<class BidirectionalIterator>
bool next_permutation(BidirectionalIterator first,
BidirectionalIterator last);
template<class BidirectionalIterator, class Compare>
bool next_permutation(BidirectionalIterator first,
BidirectionalIterator last, Compare comp);
The next_permutation() function transforms the sequence in range [first, last) to the
next permutation in lexicographic order. If the next permutation exists, the function
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
returns true. If it doesn't exist (that is, the range contains the last permutation in

lexicographic order), the function returns false and transforms the range to the first
permutation in lexicographic order. The first version uses < to determine the ordering,
while the second version uses the comp comparison object.
template<class BidirectionalIterator>
bool prev_permutation(BidirectionalIterator first,
BidirectionalIterator last);
template<class BidirectionalIterator, class Compare>
bool prev_permutation(BidirectionalIterator first,
BidirectionalIterator last, Compare comp);
The previous_permutation() function transforms the sequence in range [first, last) to
the previous permutation in lexicographic order. If the previous permutation exists, the
function returns true. If it doesn't exist (that is, the range contains the first permutation
in lexicographic order), the function returns false and transforms the range to the last
permutation in lexicographic order. The first version uses < to determine the ordering,
while the second version uses the comp comparison object.
Numeric Operations
Table G.12 summarizes the numeric operations, which are described by the numeric
header file. Arguments are not shown, and overloaded functions are listed just once.
Each function has a version that uses < for ordering elements and a version that uses
a comparison function object for ordering elements. A fuller description including the
prototypes follows the table. Thus, you can scan the table to get an idea of what a
function does; then look up the details if you find the function appealing.
Table G.12. Sorting and Related Operations
Function Description
accumulate()
Calculates a cumulative total for values in a range.
inner_product()
Calculates the inner product of two ranges.
partial_sum()
Copies partial sums calculated from one range into a second

range.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
adjacent_difference()Copies adjacent differences calculated from elements in one
range to a second range.
template <class InputIterator, class T>
T accumulate(InputIterator first, InputIterator last, T init);
template <class InputIterator, class T, class BinaryOperation>
T accumulate(InputIterator first, InputIterator last, T init,
BinaryOperation binary_op);
The accumulate() function initializes a value acc to init; then it performs the operation
acc = acc + *i (first version) or acc = binary_op(acc, *i) (second version) for each
iterator i in the range [first, last) in order. It then returns the resulting value of acc.
template <class InputIterator1, class InputIterator2, class T>
T inner_product(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init);
template <class InputIterator1, class InputIterator2, class T,
class BinaryOperation1, class BinaryOperation2>
T inner_product(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, T init,
BinaryOperation1 binary_op1, BinaryOperation2 binary_op2);
The inner_product() function initializes a value acc to init; then it performs the
operation acc = *i * *j (first version) or acc = binary_op(*i, *j) (second version) for
each iterator i in the range [first1, last1) in order and each corresponding iterator j in
the range [first2, first2 + (last1 - first1)). That is, it calculates a value from the first
elements from each sequence, then from the second elements of each sequence, and
so on, until it reaches the end of the first sequence. (Hence the second sequence
should be at least as long as the first.) The function then returns the resulting value of
acc.
template <class InputIterator, class OutputIterator>
OutputIterator partial_sum(InputIterator first, InputIterator last,

OutputIterator result);
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
template <class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator partial_sum(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op);
The partial_sum() function assigns *first to *result, *first + *(first + 1) to *(result + 1),
(first version) or binary_op(*first, *(first + 1)) to *(result + 1) (second version, and so
on. That is, the n
th
element of the sequence beginning at result contains the sum (or
binary_op equivalent) of the first n elements of the sequence beginning at first. The
function returns the past-the-end iterator for the result. The algorithm allows result to
be first, that is, it allows the result to be copied over the original sequence, if desired.
template <class InputIterator, class OutputIterator>
OutputIterator adjacent_difference(InputIterator first, InputIterator last,
OutputIterator result);
template <class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator adjacent_difference(InputIterator first, InputIterator last,
OutputIterator result,
BinaryOperation binary_op);
The adjacent_difference() function assigns *first to the location result (*result =
*first). Subsequent locations in the target range are assigned the differences (or
binary_op equivalent) of adjacent locations in the source range. That is, the next
location in the target range (result + 1) is assigned *(first + 1) - *first (first version) or
binary_op(*(first + 1), *first) (second version, and so on. The function returns the
past-the-end iterator for the result. The algorithm allows result to be first, that is, it
allows the result to be copied over the original sequence, if desired.
CONTENTS
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.

CONTENTS
Appendix H. SELECTED READINGS
Bibliography
Booch, Grady. Object-Oriented Analysis and Design. Reading, MA:
Addison-Wesley, 1993. This book presents the concepts behind OOP, discusses OOP
methods, and presents sample applications. The examples are in C++.
Booch, Grady, Jim Rumbaugh, and Ivar Jacobson. Unified Modeling Language User
Guide. Reading, MA: Addison-Wesley, 1998. This book by the creators of the Unified
Modeling Language presents the core of UML along with many examples of its use.
Cline, Marshall, Greg Lomow, and Mike Girou. C++ FAQs, Second Edition. Reading,
MA: Addison-Wesley, 1999. This book addresses a great number of frequently asked
questions about the C++ language.
Jacobson, Ivar. Object-Oriented Software Engineering: A Use Case Driven
Approach. Reading, MA: Addison-Wesley, 1994. This book describes successful
guidelines and methods (Object-Oriented Software Engineering, or OOSE) for
developing large-scale software systems.
Josuttis, Nicolai M. The C++ Standard Library: A Tutorial and Reference. Reading,
MA: Addison-Wesley, 1999. This book describes the STL as well as other C++ library
features, such as complex number support, locales, and input/output streams.
Lee, Richard C. and William M. Tepfenhart. UML and C++, Second Edition. Upper
Saddle River, New Jersey: Prentice Hall, 2001. This book is a self-teaching guide to
the Unified Modeling Language, and it includes a review of C++ fundamentals.
Meyers, Scott. Effective C++: 50 Specific Ways to Improve Your Programs and
Designs, Second Edition. Reading, MA: Addison-Wesley, 1998. This book is aimed at
programmers who already know C++, and it provides 50 rules and guidelines. Some
are technical, such as explaining when you should define copy constructors and
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
assignment operators. Others are more general, such as discussing is-a and has-a
relationships.
Meyers, Scott. Effective STL: 50 Specific Ways to Improve Your Use of the

Standard Template Library. Reading, MA: Addison-Wesley, 2001. This book
provides guidance in choosing containers and algorithms and in other facets of using
the STL.
Meyers, Scott. More Effective C++: 35 New Ways to Improve Your Programs and
Designs. Reading, MA: Addison-Wesley, 1996. This book continues in the tradition of
Effective C++, clarifying some of the more obscure aspects of the language and
showing how to accomplish various goals, such as designing smart pointers. It reflects
the additional experience C++ programmers have gained the last few years.
Rumbaugh, James, Michael Blaha, William Premerlani, Frederick Eddy, Bill Lorensen,
William Lorenson. Object-Oriented Modeling and Design. Englewood Cliffs, Prentice
Hall, 1991. This book presents and explores the Object Modeling Technique (OMT), a
method for breaking problems into suitable objects.
Rumbaugh, James, Ivar Jacobson, and Grady Booch. Unified Modeling Reference
Manual. Reading, MA: Addison-Wesley, 1998. This book by the creators of the
Unified Modeling Language presents the complete description, in reference manual
format, of the UML.
Stroustrup, Bjarne. The C++ Programming Language. Third Edition. Reading, MA:
Addison-Wesley, 1997. Stroustrup created C++, so this is the definitive text. However,
it is most easily digested if you already have some knowledge of C++. It not only
describes the language, it also provides many examples of how to use it, as well as
discussions of OOP methodology. Successive editions of this book have grown with
the language, and this edition includes a discussion of standard library elements such
as the STL and strings.
Stroustrup, Bjarne. The Design and Evolution of C++. Reading, MA:
Addison-Wesley, 1994.
If you're interested in learning how C++ evolved and why it is the way it is, read this
book.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
The ISO/ANSI standard (ISO/IEC 14882:1998):
A printed version of the standard is available for $231 from

Global Engineering Documents, Inc.
15 Inverness Way East
Englewood, CO 80122-5704
You can order from its Web site:

A downloadable electronic version (pdf format) is available for $18 from the American
National Standards Institute Web site:

Internet Resources:
The C++ FAQ Lite site for frequently asked questions (in English, Chinese, French,
Russian, and Portuguese) is a slimmed down version of the book by Cline, et al.
Currently it has the following URL:
/>You can find a moderated discussion of C++ questions in the following newsgroup:
comp.lang.c++.moderated
CONTENTS
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
CONTENTS
Appendix I. CONVERTING TO ANSI/ISO
STANDARD C++
Preprocessor Directives
Use Function Prototypes
Type Casts
Become Familiar with C++ Features
Use the New Header Organization
Use Namespaces
Use the autoptr Template
Use the string Class
Use the STL
You might have programs (or programming habits) developed in C or in older versions
of C++ that you want to convert to Standard C++. This appendix provides some

guidelines. Some pertain to moving from C to C++, others from older C++ to Standard
C++.
Preprocessor Directives
The C/C++ preprocessor provides an array of directives. In general, C++ practice is to
use those directives designed to manage the compilation process and to avoid using
directives as a substitute for code. For example, the #include directive is an essential
component for managing program files. Other directives, such as #ifndef and #endif,
let you control whether particular blocks of code get compiled. The #pragma directive
lets you control compiler-specific compilation options. These all are useful, sometimes
necessary, tools. You should exert caution, however, when it comes to the #define
directive.
Use const Instead of #define to Define Constants
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
Symbolic constants make code more readable and maintainable. The constant's name
indicates its meaning, and if you need to change the value, you just have to change
the value once, in the definition, then recompile. C used the preprocessor for this
purpose:
#define MAX_LENGTH 100
The preprocessor then does a text substitution in your source code, replacing
occurrences of MAX_LENGTH with 100 prior to compilation.
The C++ approach is to apply the const modifier to a variable declaration:
const int MAX_LENGTH = 100;
This treats MAX_LENGTH as a read-only int.
There are several advantages to the const approach. First, the declaration explicitly
names the type. For #define, you must use various suffixes to a number to indicate
types other than char, int, or double; for example, using 100L to indicate a long type
or 3.14F to indicate a float type. More importantly, the const approach can just as
easily be used with derived types:
const int base_vals[5] = {1000, 2000, 3500, 6000, 10000};
const string ans[3] = {"yes", "no", "maybe"};

Finally, const identifiers obey the same scope rules as variables. Thus, you can create
constants with global scope, named namespace scope, and block scope. If, say, you
define a constant in a particular function, you don't have to worry about the definition
conflicting with a global constant used elsewhere in a program. For example, consider
the following:
#define n 5
const int dz = 12;

void fizzle()
{
int n;
int dz;
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.

}
The preprocessor will replace
int n;
with
int 5;
and induce a compilation error. The dz defined in fizzle(), however, will be a local
variable. Also, fizzle(), if necessary, can use the scope resolution operator and access
the constant as ::dz.
C has borrowed the const keyword from C++, but the C++ version is more useful. For
example, the C++ version has internal linkage for external const values rather than
the default external linkage used by variables and by the C const. This means that
each file in a program using a const needs that const defined in that particular file.
This might sound like extra work, but, in fact, it makes life easier. With internal linkage,
you can place const definitions in a header file used by various files in a project. That
is a compiler error for external linkage but not for internal linkage. Also, because a
const must be defined in the file using it (being in a header file used by that file

satisfies the requirement), you can use const values as array size arguments:
const int MAX_LENGTH = 100;

double loads[MAX_LENGTH];
for (int i = 0; i < MAX_LENGTH; i++)
loads[i] = 50;
This won't work in C because the defining declaration for MAX_LENGTH could be in
a separate file and not be available when this particular file is compiled. In fairness, it
should be added that, in C, you could use the static modifier to create constants with
internal linkage. It's just that C++, by making static the default, requires one less thing
for you to remember.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
Incidentally, the revised C standard (C99) does allow you to use a const as an array
size, but the array is treated as a new form of array, called a variable array, that is not
part of the C++ standard.
The #define directive, however, still is useful as part of the standard idiom for
controlling when a header file is compiled:
// blooper.h
#ifndef _BLOOPER_H_
#define _BLOOPER_H_
// code goes here
#endif
For typical symbolic constants, however, get into the habit of using const instead of
#define. Another good alternative, particularly when you have a set of related integer
constants, is to use enum:
enum {LEVEL1 = 1, LEVEL2 = 2, LEVEL3 = 4, LEVEL4 = 8};
Use inline Instead of #define to Define Short Functions
The traditional C way to create the near-equivalent of an inline function was to use a
#define macro definition:
#define Cube(X) X*X*X

This lead the preprocessor to do text substitution, with X being replaced by the
corresponding argument to Cube():
y = Cube(x); // replaced with y = x*x*x;
y = Cube(x + z++); // replaced with x + z++*x + z++*x + z++;
Because the preprocessor uses text substitution instead of true passing of arguments,
using such macros can lead to unexpected and incorrect results. Such error can be
reduced by using lots of parentheses in the macro to ensure the correct order of
operations:
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
#define Cube(X) ((X)*(X)*(X))
Even this, however, doesn't deal with cases such as using values like z++.
The C++ approach of using the keyword inline to identify inline functions is much
more dependable because it uses true argument passing. Furthermore, C++ inline
functions can be regular functions or class methods.
One positive feature of the #define macro is that it is typeless so it can be used with
any type for which the operation makes sense. In C++ you can create inline templates
to achieve type-independent functions while retaining argument passing.
In short, use C++ inlining instead of C #define macros.
Use Function Prototypes
Actually, you don't have a choice. Although prototyping is optional in C, it is mandatory
in C++. Note that a function that is defined before its first use, such as an inline
function, serves as its own prototype.
Do use const in function prototypes and headers when appropriate. In particular, use
const with pointer parameters and reference parameters representing data that is not
to be altered. Not only does this allow the compiler to catch errors that change data, it
also makes a function more general. That is, a function with a const pointer or
reference can process both const and non-const data, while a function that fails to use
const with a pointer or reference only can process non-const data.
Type Casts
One of Stroustrup's pet peeves about C is its undisciplined type cast operator. True,

type casts often are necessary, but the standard type cast is too unrestrictive. For
example, consider the following code:
struct Doof
{
double feeb;
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
double steeb;
char sgif[10];
};
Doof leam;
short * ps = (short *) & leam; // old syntax
int * pi = int * (&leam); // new syntax
Nothing in the language prevents you from casting a pointer of one type to a pointer to
a totally unrelated type.
In a way, the situation is similar to that of the goto statement. The problem with the
goto statement was that it was too flexible, leading to twisted code. The solution was
to provide more limited, structured versions of goto to handle the most common tasks
for which goto was needed. This was the genesis of language elements such as for
and while loops and if else statements. Standard C++ provides a similar solution for
the problem of the undisciplined type cast, namely, restricted type casts to handle the
most common situations requiring type casts. These are the type cast operators
discussed in Chapter 15, "Friends, Exceptions, and More":
dynamic_cast
static_cast
const_cast
reinterpret_cast
So, if you are doing a type cast involving pointers, use one of these operators if
possible. Doing so both documents the intent of the cast and provides checking that
the cast is being used as intended.
Become Familiar with C++ Features

If you've been using malloc() and free(), switch to using new and delete instead. If
you've been using setjmp() and longjmp() for error handling, use try, throw, and
catch instead. Try using the bool type for values representing true and false.
Use the New Header Organization
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
The Standard specifies new names for the header files, as described in Chapter 2,
"Setting Out to C++." If you've been using the old-style header files, you should
change over to using the new-style names. This is not just a cosmetic change
because the new versions might add new features. For example, the ostream header
file provides support for wide-character input and output. It also provides new
manipulators such as boolalpha and fixed (as described in Chapter 17, "Input, Output,
and Files"). These offer a simpler interface than using setf() or the iomanip functions
for setting many formatting options. If you do use setf(), use ios_base instead of ios
when specifying constants; that is, use ios_base::fixed instead of ios::fixed. Also, the
new header files incorporate namespaces.
Use Namespaces
Namespaces help organize identifiers used in a program in order to avoid name
conflicts. Because the standard library, as implemented with the new header file
organization, places names in the std namespace, using these header files requires
that you deal with namespaces.
The examples in this book, for simplicity, utilize a using directive to make all the
names from the std namespace available:
#include <iostream>
#include <string>
#include <vector>
using namespace std; // a using-directive
However, the wholesale exporting of all the names in a namespace, whether needed
or not, runs counter to the goals of namespaces.
Instead, the recommended approach is to use either using declarations or the scope
resolution operator (::) to make available just those names a program needs. For

example,
#include <iostream>
using std::cin; // a using-declaration
using std::cout;
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
using std::endl;
makes cin, cout, and endl available for the rest of the file. Using the scope resolution
operator, however, makes a name available just in the expression using the operator:
cout << std::fixed << x << endl; //using the scope resolution operator
This could get wearisome, but you can collect your common using declarations in a
header file:
// mynames a header file
#include <iostream>
using std::cin; // a using-declaration
using std::cout;
using std::endl;
Going a step further, you could collect using declarations in namespaces:
// mynames a header file
#include <iostream>
namespace io
{
using std::cin;
using std::cout;
using std::endl;
}
namespace formats
{
using std::fixed;
using std::scientific;
using std:boolalpha;

}
Then a program could include this file and use the namespaces it needs:
#include "mynames"
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
using namespace io;
Use the autoptr Template
Each use of new should be paired with a use of delete. This can lead to problems if a
function in which new is used terminates early via an exception being thrown. As
discussed in Chapter 15, using an autoptr object to keep track of an object created by
new automates the activation of delete.
Use the string Class
The traditional C-style string suffers from not being a real type. You can store a string
in a character array, you can initialize a character array to a string. But you can't use
the assignment operator to assign a string to a character array; instead, you must
remember to use strcpy() or strncpy(). You can't use the relational operators to
compare C-style strings; instead, you must remember to use strcmp(). (And if you
forget and use, say, the > operator, you don't get a syntax error; instead, the program
compares string addresses instead of string contents.)
The string class (Chapter 16, "The string Class and the Standard Template Library,"
and Appendix F, "The string Template Class"), on the other hand, lets you use objects
to represent strings. Assignment, relational operators, and the addition operator (for
concatenation) all are defined. Furthermore, the string class provides automatic
memory management so that you normally don't have to worry about someone
entering a string that either overruns an array or gets truncated before being stored.
The string class provides many convenience methods. For example, you can append
one string object to another, but you also can append a C-style string or even a char
value to a string object. For functions that require a C-style string argument, you can
use the c_str() method to return a suitable pointer-to-char.
Not only does the string class provide a well-designed set of methods for handling
string-related tasks, such as finding substrings, but it also features a design that is

compatible with the STL so that you can use STL algorithms with string objects.
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
Use the STL
The Standard Template Library (Chapter 16 and Appendix G, "The STL Methods and
Functions") provides ready-made solutions to many programming needs, so use it.
For example, instead of declaring an array of double or of string objects, you can
create a vector<double> object or a vector<string> object. The advantages are
similar to those of using string objects instead of C-style strings. Assignment is
defined, so you can use the assignment operator to assign one vector object to
another. You can pass a vector object by reference, and a function receiving such an
object can use the size() method to determine the number of elements in the vector
object. Built-in memory management allows for automatic resizing when you use the
pushback() method to add elements to a vector object. And, of course, several useful
class methods and general algorithms are at your service.
If you need a list, a double-ended queue (or deque), a stack, a regular queue, a set, or
a map, the STL provides useful container templates. The algorithm library is designed
so that you easily copy the contents of a vector to a list or compare the contents of a
set to a vector. This design makes the STL into a toolkit providing basic units that you
can assemble as needed.
The extensive algorithm library was designed with efficiency as one of the main design
goals, so you can get top-flight results with relatively little programming effort on your
part. And the iterator concept used to implement the algorithms means that they aren't
limited to being used with STL containers. In particular, they can be applied to
traditional arrays, too.
CONTENTS
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
CONTENTS
Appendix J. ANSWERS TO REVIEW QUESTIONS
Chapter 2
Chapter 3

Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 2
.1:What are the modules of C++ programs called?
A:They are called functions.
.2:What does the following preprocessor directive do?
#include <iostream>
A:It causes the contents of the iostream file to be substituted for this
directive before final compilation.
.3:What does the following statement do?
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.
using namespace std;
A:It makes definitions made in the std namespace available to a program.
.4:What statement would you use to print the phrase "Hello, world" and then
start a new line?
A:
cout << "Hello, world\n";
or

cout << "Hello, world" << endl;
.5:What statement would you use to create an integer variable with the
name cheeses?
A:
int cheeses;
.6:What statement would you use to assign the value 32 to the variable
cheeses?
A:
cheeses = 32;
.7:What statement would you use to read a value from keyboard input into
the variable cheeses?
A:
cin >> cheeses;
.8:What statement would you use to print "We have X varieties of cheese,"
where the current value of the cheeses variable replaces X?
This document was created by an unregistered ChmMagic, please go to to register it. Thanks.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×