Lippman C++ 5th Ed Source Code Initial Commit (GCC_4_7_0.zip) master
authorMichael Cambria <mcc@halfball.org>
Wed, 4 May 2016 23:04:50 +0000 (19:04 -0400)
committerMichael Cambria <mcc@halfball.org>
Wed, 4 May 2016 23:04:50 +0000 (19:04 -0400)
440 files changed:
1/README [new file with mode: 0644]
1/Sales_item.h [new file with mode: 0644]
1/add.cc [new file with mode: 0644]
1/add_item.cc [new file with mode: 0644]
1/add_item2.cc [new file with mode: 0644]
1/avg_price.cc [new file with mode: 0644]
1/data/add [new file with mode: 0644]
1/data/add_item [new file with mode: 0644]
1/data/book_sales [new file with mode: 0644]
1/data/mysum [new file with mode: 0644]
1/data/occurs [new file with mode: 0644]
1/for_ex.cc [new file with mode: 0644]
1/forcount.cc [new file with mode: 0644]
1/item_io.cc [new file with mode: 0644]
1/main_only.cc [new file with mode: 0644]
1/makefile [new file with mode: 0644]
1/mysum.cc [new file with mode: 0644]
1/occurs.cc [new file with mode: 0644]
1/runpgms [new file with mode: 0644]
1/whilecount.cc [new file with mode: 0644]
10/README [new file with mode: 0644]
10/absInt.cc [new file with mode: 0644]
10/accum.cc [new file with mode: 0644]
10/accum4.cc [new file with mode: 0644]
10/avg_price.cc [new file with mode: 0644]
10/bind2.cc [new file with mode: 0644]
10/data/absInt [new file with mode: 0644]
10/data/accum [new file with mode: 0644]
10/data/book_sales [new file with mode: 0644]
10/data/count-size [new file with mode: 0644]
10/data/iostream_iter [new file with mode: 0644]
10/data/outFile1 [new file with mode: 0644]
10/data/outFile2 [new file with mode: 0644]
10/data/rcomma [new file with mode: 0644]
10/data/readme [new file with mode: 0644]
10/data/reverse-iter [new file with mode: 0644]
10/data/use_find [new file with mode: 0644]
10/elimDups.cc [new file with mode: 0644]
10/equal.cc [new file with mode: 0644]
10/equiv-istream-iter.cc [new file with mode: 0644]
10/inserters.cc [new file with mode: 0644]
10/iostream_iter.cc [new file with mode: 0644]
10/lambda.cc [new file with mode: 0644]
10/makefile [new file with mode: 0644]
10/newcount-size.cc [new file with mode: 0644]
10/ostreamIter2.cc [new file with mode: 0644]
10/outrunpgms [new file with mode: 0644]
10/rcomma.cc [new file with mode: 0644]
10/rev-iters.cc [new file with mode: 0644]
10/reverse-iter.cc [new file with mode: 0644]
10/reverse.cc [new file with mode: 0644]
10/runpgms [new file with mode: 0644]
10/sortSI.cc [new file with mode: 0644]
10/use-istream-iter.cc [new file with mode: 0644]
10/use_find.cc [new file with mode: 0644]
11/README [new file with mode: 0644]
11/data/Alice [new file with mode: 0644]
11/data/rules [new file with mode: 0644]
11/data/simple_query [new file with mode: 0644]
11/data/storyDataFile [new file with mode: 0644]
11/data/text [new file with mode: 0644]
11/data/word_count [new file with mode: 0644]
11/erasemap.cc [new file with mode: 0644]
11/makefile [new file with mode: 0644]
11/map1.cc [new file with mode: 0644]
11/multimap.cc [new file with mode: 0644]
11/pair.cc [new file with mode: 0644]
11/restricted_count.cc [new file with mode: 0644]
11/runpgms [new file with mode: 0644]
11/setOps.cc [new file with mode: 0644]
11/unorderedWordCount.cc [new file with mode: 0644]
11/wcEx.cc [new file with mode: 0644]
11/word_count.cc [new file with mode: 0644]
11/word_transform.cc [new file with mode: 0644]
12/Foo.h [new file with mode: 0644]
12/QueryResult.h [new file with mode: 0644]
12/README [new file with mode: 0644]
12/StrBlob.h [new file with mode: 0644]
12/TextQuery.cc [new file with mode: 0644]
12/TextQuery.h [new file with mode: 0644]
12/UP.cc [new file with mode: 0644]
12/allocPtr.cc [new file with mode: 0644]
12/allocSP.cc [new file with mode: 0644]
12/data/Alice [new file with mode: 0644]
12/data/alloc [new file with mode: 0644]
12/data/storyDataFile [new file with mode: 0644]
12/makefile [new file with mode: 0644]
12/oknew.cc [new file with mode: 0644]
12/querymain.cc [new file with mode: 0644]
12/release.cc [new file with mode: 0644]
12/runpgms [new file with mode: 0644]
12/unique.cc [new file with mode: 0644]
12/useBlob.cc [new file with mode: 0644]
12/usealloc.cc [new file with mode: 0644]
12/usealloc2.cc [new file with mode: 0644]
12/weak.cc [new file with mode: 0644]
13/Folder.cc [new file with mode: 0644]
13/Folder.h [new file with mode: 0644]
13/FolderMain.cc [new file with mode: 0644]
13/HasPtrUseCount.cc [new file with mode: 0644]
13/HasPtrValueLike.cc [new file with mode: 0644]
13/README [new file with mode: 0644]
13/StrFolder.cc [new file with mode: 0644]
13/StrFolder.h [new file with mode: 0644]
13/StrFolderMain.cc [new file with mode: 0644]
13/StrVec.cc [new file with mode: 0644]
13/StrVec.h [new file with mode: 0644]
13/String.cc [new file with mode: 0644]
13/String.h [new file with mode: 0644]
13/copyNoMove.cc [new file with mode: 0644]
13/data/storyDataFile [new file with mode: 0644]
13/makefile [new file with mode: 0644]
13/moveHP.cc [new file with mode: 0644]
13/runpgms [new file with mode: 0644]
13/serial.cc [new file with mode: 0644]
13/small_SI.h [new file with mode: 0644]
13/substr.cc [new file with mode: 0644]
13/useStrVec.cc [new file with mode: 0644]
14/README [new file with mode: 0644]
14/Sales_data.cc [new file with mode: 0644]
14/Sales_data.h [new file with mode: 0644]
14/absInt.cc [new file with mode: 0644]
14/add.cc [new file with mode: 0644]
14/calc.cc [new file with mode: 0644]
14/data/absInt [new file with mode: 0644]
14/data/add_item [new file with mode: 0644]
14/data/count-size [new file with mode: 0644]
14/data/word_echo [new file with mode: 0644]
14/directcall.cc [new file with mode: 0644]
14/fcnobj.cc [new file with mode: 0644]
14/makefile [new file with mode: 0644]
14/newcount-size.cc [new file with mode: 0644]
14/readStr.cc [new file with mode: 0644]
14/runpgms [new file with mode: 0644]
15/Basket.cc [new file with mode: 0644]
15/Basket.h [new file with mode: 0644]
15/Basket_main.cc [new file with mode: 0644]
15/Query.cc [new file with mode: 0644]
15/Query.h [new file with mode: 0644]
15/Quote.cc [new file with mode: 0644]
15/Quote.h [new file with mode: 0644]
15/README [new file with mode: 0644]
15/andQueryTest.cc [new file with mode: 0644]
15/and_orQueryTest.cc [new file with mode: 0644]
15/data/querymain [new file with mode: 0644]
15/data/storyDataFile [new file with mode: 0644]
15/data/three [new file with mode: 0644]
15/data/two [new file with mode: 0644]
15/get_print.cc [new file with mode: 0644]
15/makefile [new file with mode: 0644]
15/runpgms [new file with mode: 0644]
15/sample.cc [new file with mode: 0644]
15/scope.cc [new file with mode: 0644]
15/useQuote.cc [new file with mode: 0644]
15/vecBasket.cc [new file with mode: 0644]
15/virtualsNscope.cc [new file with mode: 0644]
15/wordQueryTest.cc [new file with mode: 0644]
16/Array.cc [new file with mode: 0644]
16/Blob.h [new file with mode: 0644]
16/DebugDelete.h [new file with mode: 0644]
16/README [new file with mode: 0644]
16/SP.cc [new file with mode: 0644]
16/Sales_data.cc [new file with mode: 0644]
16/Sales_data.h [new file with mode: 0644]
16/Vec.h [new file with mode: 0644]
16/Vecmain.cc [new file with mode: 0644]
16/array-parms4.cc [new file with mode: 0644]
16/build.cc [new file with mode: 0644]
16/compare.cc [new file with mode: 0644]
16/compare.h [new file with mode: 0644]
16/compareDef.cc [new file with mode: 0644]
16/compareSpec.cc [new file with mode: 0644]
16/data/Vecmain [new file with mode: 0644]
16/data/book_sales [new file with mode: 0644]
16/data/book_trans [new file with mode: 0644]
16/debug.cc [new file with mode: 0644]
16/debug_rep.h [new file with mode: 0644]
16/expansion.cc [new file with mode: 0644]
16/flip.cc [new file with mode: 0644]
16/makefile [new file with mode: 0644]
16/multiset.cc [new file with mode: 0644]
16/overSpec.cc [new file with mode: 0644]
16/qm.cc [new file with mode: 0644]
16/runpgms [new file with mode: 0644]
16/special.cc [new file with mode: 0644]
16/trail-ret.cc [new file with mode: 0644]
16/typealias.cc [new file with mode: 0644]
16/useBlob.cc [new file with mode: 0644]
16/useChcking.cc [new file with mode: 0644]
16/variadic.cc [new file with mode: 0644]
16/variadic3.cc [new file with mode: 0644]
17/README [new file with mode: 0644]
17/REerrs.cc [new file with mode: 0644]
17/badgetc.cc [new file with mode: 0644]
17/bits.cc [new file with mode: 0644]
17/ccpgms.cc [new file with mode: 0644]
17/data/badgetc [new file with mode: 0644]
17/data/bits [new file with mode: 0644]
17/data/copyOut [new file with mode: 0644]
17/data/files [new file with mode: 0644]
17/data/findDups [new file with mode: 0644]
17/data/findDupsV2 [new file with mode: 0644]
17/data/findgerunds [new file with mode: 0644]
17/data/game [new file with mode: 0644]
17/data/no-skipws [new file with mode: 0644]
17/data/phones [new file with mode: 0644]
17/data/phones2 [new file with mode: 0644]
17/data/phonesFinal [new file with mode: 0644]
17/data/skipws [new file with mode: 0644]
17/data/store1 [new file with mode: 0644]
17/data/store2 [new file with mode: 0644]
17/data/store3 [new file with mode: 0644]
17/data/store4 [new file with mode: 0644]
17/data/validatePhones [new file with mode: 0644]
17/findbook.cc [new file with mode: 0644]
17/game.cc [new file with mode: 0644]
17/getc.cc [new file with mode: 0644]
17/getput.cc [new file with mode: 0644]
17/makefile [new file with mode: 0644]
17/manips.cc [new file with mode: 0644]
17/manips11.cc [new file with mode: 0644]
17/no-skipws.cc [new file with mode: 0644]
17/normalInts.cc [new file with mode: 0644]
17/phones.cc [new file with mode: 0644]
17/phonesFinal.cc [new file with mode: 0644]
17/point.cc [new file with mode: 0644]
17/prec.cc [new file with mode: 0644]
17/rand1.cc [new file with mode: 0644]
17/rand2.cc [new file with mode: 0644]
17/rand4.cc [new file with mode: 0644]
17/rand6.cc [new file with mode: 0644]
17/regex1.cc [new file with mode: 0644]
17/runpgms [new file with mode: 0644]
17/sci_dec.cc [new file with mode: 0644]
17/seed.cc [new file with mode: 0644]
17/seek.cc [new file with mode: 0644]
17/setw.cc [new file with mode: 0644]
17/skipws.cc [new file with mode: 0644]
17/subexpr.cc [new file with mode: 0644]
17/tuple.cc [new file with mode: 0644]
17/validatePhones.cc [new file with mode: 0644]
17/zips.cc [new file with mode: 0644]
18/Animal.cc [new file with mode: 0644]
18/Animal.h [new file with mode: 0644]
18/Animal_virtual_baseVers.h [new file with mode: 0644]
18/README [new file with mode: 0644]
18/Sales_data.cc [new file with mode: 0644]
18/book.cc [new file with mode: 0644]
18/bookexcept.h [new file with mode: 0644]
18/class.cc [new file with mode: 0644]
18/data/book_sales [new file with mode: 0644]
18/ex-over.cc [new file with mode: 0644]
18/inherit.cc [new file with mode: 0644]
18/makefile [new file with mode: 0644]
18/mi-scope.cc [new file with mode: 0644]
18/over.cc [new file with mode: 0644]
18/over2.cc [new file with mode: 0644]
18/runpgms [new file with mode: 0644]
18/spec.cc [new file with mode: 0644]
18/usingDirective.cc [new file with mode: 0644]
18/virt-inherit.cc [new file with mode: 0644]
19/.useScreen.cc.swp [new file with mode: 0644]
19/README [new file with mode: 0644]
19/Screen.cc [new file with mode: 0644]
19/Screen.h [new file with mode: 0644]
19/TextQuery.h [new file with mode: 0644]
19/Token.h [new file with mode: 0644]
19/bitfields.cc [new file with mode: 0644]
19/calc.cc [new file with mode: 0644]
19/dyn_cast.cc [new file with mode: 0644]
19/enum-match.cc [new file with mode: 0644]
19/enum.cc [new file with mode: 0644]
19/makefile [new file with mode: 0644]
19/memFN-bind.cc [new file with mode: 0644]
19/out2 [new file with mode: 0644]
19/runpgms [new file with mode: 0644]
19/tok2.cc [new file with mode: 0644]
19/typeid.cc [new file with mode: 0644]
19/useScreen.cc [new file with mode: 0644]
2/README [new file with mode: 0644]
2/Sales_data.cc [new file with mode: 0644]
2/Sales_data.h [new file with mode: 0644]
2/compounddecls.cc [new file with mode: 0644]
2/convs.cc [new file with mode: 0644]
2/data/add_item [new file with mode: 0644]
2/data/data/add_item [new file with mode: 0644]
2/dbl_ptr.cc [new file with mode: 0644]
2/decl-ex.cc [new file with mode: 0644]
2/escape.cc [new file with mode: 0644]
2/inits.cc [new file with mode: 0644]
2/literals.cc [new file with mode: 0644]
2/makefile [new file with mode: 0644]
2/ref-ex.cc [new file with mode: 0644]
2/runpgms [new file with mode: 0644]
2/runpgms.bat [new file with mode: 0644]
2/scope_levels.cc [new file with mode: 0644]
2/unsigned.cc [new file with mode: 0644]
3/README [new file with mode: 0644]
3/add_using.cc [new file with mode: 0644]
3/arrayScores.cc [new file with mode: 0644]
3/cctype.cc [new file with mode: 0644]
3/char_repl.cc [new file with mode: 0644]
3/cstring_hdr.cc [new file with mode: 0644]
3/data/add [new file with mode: 0644]
3/data/grades [new file with mode: 0644]
3/data/hexify [new file with mode: 0644]
3/data/iterarith [new file with mode: 0644]
3/data/ptr_traversal2 [new file with mode: 0644]
3/data/string_io [new file with mode: 0644]
3/data/vecStrings2 [new file with mode: 0644]
3/data/word_echo [new file with mode: 0644]
3/getline.cc [new file with mode: 0644]
3/hexify.cc [new file with mode: 0644]
3/makefile [new file with mode: 0644]
3/multidim.cc [new file with mode: 0644]
3/ptr_traversal2.cc [new file with mode: 0644]
3/runpgms [new file with mode: 0644]
3/square.cc [new file with mode: 0644]
3/strcompare.cc [new file with mode: 0644]
3/string_add.cc [new file with mode: 0644]
3/string_io.cc [new file with mode: 0644]
3/string_io2.cc [new file with mode: 0644]
3/string_size.cc [new file with mode: 0644]
3/string_size2.cc [new file with mode: 0644]
3/string_size3.cc [new file with mode: 0644]
3/vecScores.cc [new file with mode: 0644]
3/vecStrings2.cc [new file with mode: 0644]
3/vecSubs.cc [new file with mode: 0644]
3/vec_decls.cc [new file with mode: 0644]
3/word_echo.cc [new file with mode: 0644]
4/README [new file with mode: 0644]
4/arith-ex.cc [new file with mode: 0644]
4/arraysizeof.cc [new file with mode: 0644]
4/cond.cc [new file with mode: 0644]
4/data/oofe [new file with mode: 0644]
4/data/vecSubs [new file with mode: 0644]
4/incr.cc [new file with mode: 0644]
4/int-div.cc [new file with mode: 0644]
4/makefile [new file with mode: 0644]
4/oofe.cc [new file with mode: 0644]
4/overflow.cc [new file with mode: 0644]
4/prec.cc [new file with mode: 0644]
4/runpgms [new file with mode: 0644]
4/shift-prec.cc [new file with mode: 0644]
4/sizeof_pgm.cc [new file with mode: 0644]
4/unaryOps.cc [new file with mode: 0644]
4/vec_init.cc [new file with mode: 0644]
4/warn_dbltoint.cc [new file with mode: 0644]
5/README [new file with mode: 0644]
5/add_item2.cc [new file with mode: 0644]
5/data/add_item_data [new file with mode: 0644]
5/data/doWhile [new file with mode: 0644]
5/data/grades [new file with mode: 0644]
5/data/guesses [new file with mode: 0644]
5/data/readme [new file with mode: 0644]
5/doWhile.cc [new file with mode: 0644]
5/guess.cc [new file with mode: 0644]
5/ifgrades.cc [new file with mode: 0644]
5/makefile [new file with mode: 0644]
5/othercnt.cc [new file with mode: 0644]
5/rangefor.cc [new file with mode: 0644]
5/runpgms [new file with mode: 0644]
5/vowels.cc [new file with mode: 0644]
6/LocalMath.cc [new file with mode: 0644]
6/LocalMath.h [new file with mode: 0644]
6/README [new file with mode: 0644]
6/arrRet.cc [new file with mode: 0644]
6/count-calls.cc [new file with mode: 0644]
6/data/mainmath [new file with mode: 0644]
6/data/refparms [new file with mode: 0644]
6/errMsg_initList.cc [new file with mode: 0644]
6/fact.cc [new file with mode: 0644]
6/fcnptrRet.cc [new file with mode: 0644]
6/good_printmain.cc [new file with mode: 0644]
6/inline_shorter.cc [new file with mode: 0644]
6/mainmath.cc [new file with mode: 0644]
6/mainret.cc [new file with mode: 0644]
6/make_plural.h [new file with mode: 0644]
6/makefile [new file with mode: 0644]
6/mk_plural.cc [new file with mode: 0644]
6/printFcns.h [new file with mode: 0644]
6/ref-fcn.cc [new file with mode: 0644]
6/refparms.cc [new file with mode: 0644]
6/refret-ex.cc [new file with mode: 0644]
6/reset.cc [new file with mode: 0644]
6/runpgms [new file with mode: 0644]
6/stl-arr-fcns.cc [new file with mode: 0644]
6/use_mk_plural.cc [new file with mode: 0644]
6/usefcnptr.cc [new file with mode: 0644]
6/wdebug.cc [new file with mode: 0644]
7/Account.cc [new file with mode: 0644]
7/Account.h [new file with mode: 0644]
7/Debug.cc [new file with mode: 0644]
7/Debug.h [new file with mode: 0644]
7/README [new file with mode: 0644]
7/Sales_data.cc [new file with mode: 0644]
7/Sales_data.h [new file with mode: 0644]
7/Screen.h [new file with mode: 0644]
7/WindowMgr.h [new file with mode: 0644]
7/add_item.cc [new file with mode: 0644]
7/avg_price.cc [new file with mode: 0644]
7/data/add_item [new file with mode: 0644]
7/data/book_sales [new file with mode: 0644]
7/makefile [new file with mode: 0644]
7/runpgms [new file with mode: 0644]
7/useAccount.cc [new file with mode: 0644]
7/useDebug.cc [new file with mode: 0644]
7/useScreen.cc [new file with mode: 0644]
8/README [new file with mode: 0644]
8/add_item.cc [new file with mode: 0644]
8/add_itemV2.cc [new file with mode: 0644]
8/buf.cc [new file with mode: 0644]
8/clearIO.cc [new file with mode: 0644]
8/data/add_item [new file with mode: 0644]
8/data/sstream [new file with mode: 0644]
8/fileIO.cc [new file with mode: 0644]
8/makefile [new file with mode: 0644]
8/runpgms [new file with mode: 0644]
8/sstream.cc [new file with mode: 0644]
9/README [new file with mode: 0644]
9/capacity.cc [new file with mode: 0644]
9/data/erase [new file with mode: 0644]
9/erase1.cc [new file with mode: 0644]
9/erase2.cc [new file with mode: 0644]
9/find-str.cc [new file with mode: 0644]
9/find_ops.cc [new file with mode: 0644]
9/makefile [new file with mode: 0644]
9/refreshIter.cc [new file with mode: 0644]
9/runpgms [new file with mode: 0644]
9/stack.cc [new file with mode: 0644]
9/str_assignOps.cc [new file with mode: 0644]
9/substr.cc [new file with mode: 0644]
9/useConvs.cc [new file with mode: 0644]
CompilerNotes.pdf [new file with mode: 0644]
GCC 4.7 README.pdf [new file with mode: 0644]
GNU_makefile_template [new file with mode: 0644]
Version_test.h [new file with mode: 0644]
makefile [new file with mode: 0644]
noinput [new file with mode: 0644]
runpgms [new file with mode: 0644]

diff --git a/1/README b/1/README
new file mode 100644 (file)
index 0000000..c4aaafd
--- /dev/null
+++ b/1/README
@@ -0,0 +1,14 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   book_sales       avg_price, item_io
+   add_item         add_item, add_item2
+   add              add
+   my_sum           my_sum
+   occurs           occurs
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/1/Sales_item.h b/1/Sales_item.h
new file mode 100644 (file)
index 0000000..ff86669
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+/* This file defines the Sales_item class used in chapter 1.
+ * The code used in this file will be explained in
+ * Chapter 7 (Classes) and Chapter 14 (Overloaded Operators)
+ * Readers shouldn't try to understand the code in this file
+ * until they have read those chapters.
+*/
+
+#ifndef SALESITEM_H
+// we're here only if SALESITEM_H has not yet been defined 
+#define SALESITEM_H
+
+// Definition of Sales_item class and related functions goes here
+#include <iostream>
+#include <string>
+
+class Sales_item {
+// these declarations are explained section 7.2.1, p. 270 
+// and in chapter 14, pages 557, 558, 561
+friend std::istream& operator>>(std::istream&, Sales_item&);
+friend std::ostream& operator<<(std::ostream&, const Sales_item&);
+friend bool operator<(const Sales_item&, const Sales_item&);
+friend bool 
+operator==(const Sales_item&, const Sales_item&);
+public:
+    // constructors are explained in section 7.1.4, pages 262 - 265
+    // default constructor needed to initialize members of built-in type
+    Sales_item() = default;
+    Sales_item(const std::string &book): bookNo(book) { }
+    Sales_item(std::istream &is) { is >> *this; }
+public:
+    // operations on Sales_item objects
+    // member binary operator: left-hand operand bound to implicit this pointer
+    Sales_item& operator+=(const Sales_item&);
+    
+    // operations on Sales_item objects
+    std::string isbn() const { return bookNo; }
+    double avg_price() const;
+// private members as before
+private:
+    std::string bookNo;      // implicitly initialized to the empty string
+    unsigned units_sold = 0; // explicitly initialized
+    double revenue = 0.0;
+};
+
+// used in chapter 10
+inline
+bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) 
+{ return lhs.isbn() == rhs.isbn(); }
+
+// nonmember binary operator: must declare a parameter for each operand
+Sales_item operator+(const Sales_item&, const Sales_item&);
+
+inline bool 
+operator==(const Sales_item &lhs, const Sales_item &rhs)
+{
+    // must be made a friend of Sales_item
+    return lhs.units_sold == rhs.units_sold &&
+           lhs.revenue == rhs.revenue &&
+           lhs.isbn() == rhs.isbn();
+}
+
+inline bool 
+operator!=(const Sales_item &lhs, const Sales_item &rhs)
+{
+    return !(lhs == rhs); // != defined in terms of operator==
+}
+
+// assumes that both objects refer to the same ISBN
+Sales_item& Sales_item::operator+=(const Sales_item& rhs) 
+{
+    units_sold += rhs.units_sold; 
+    revenue += rhs.revenue; 
+    return *this;
+}
+
+// assumes that both objects refer to the same ISBN
+Sales_item 
+operator+(const Sales_item& lhs, const Sales_item& rhs) 
+{
+    Sales_item ret(lhs);  // copy (|lhs|) into a local object that we'll return
+    ret += rhs;           // add in the contents of (|rhs|) 
+    return ret;           // return (|ret|) by value
+}
+
+std::istream& 
+operator>>(std::istream& in, Sales_item& s)
+{
+    double price;
+    in >> s.bookNo >> s.units_sold >> price;
+    // check that the inputs succeeded
+    if (in)
+        s.revenue = s.units_sold * price;
+    else 
+        s = Sales_item();  // input failed: reset object to default state
+    return in;
+}
+
+std::ostream& 
+operator<<(std::ostream& out, const Sales_item& s)
+{
+    out << s.isbn() << " " << s.units_sold << " "
+        << s.revenue << " " << s.avg_price();
+    return out;
+}
+
+double Sales_item::avg_price() const
+{
+    if (units_sold) 
+        return revenue/units_sold; 
+    else 
+        return 0;
+}
+#endif
diff --git a/1/add.cc b/1/add.cc
new file mode 100644 (file)
index 0000000..5bd0d25
--- /dev/null
+++ b/1/add.cc
@@ -0,0 +1,42 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+
+int main()
+{
+       // prompt user to enter two numbers
+       std::cout << "Enter two numbers:" << std::endl; 
+       int v1 = 0, v2 = 0;
+       std::cin >> v1 >> v2;   
+       std::cout << "The sum of " << v1 << " and " << v2
+                 << " is " << v1 + v2 << std::endl;
+       return 0;
+}
+
diff --git a/1/add_item.cc b/1/add_item.cc
new file mode 100644 (file)
index 0000000..8a378ac
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+#include "Sales_item.h"
+
+int main() 
+{
+    Sales_item item1, item2;
+
+    std::cin >> item1 >> item2;   //read a pair of transactions
+    std::cout << item1 + item2 << std::endl; //print their sum
+
+    return 0;
+}
diff --git a/1/add_item2.cc b/1/add_item2.cc
new file mode 100644 (file)
index 0000000..d43748c
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+#include "Sales_item.h"
+
+int main() 
+{
+    Sales_item item1, item2;
+
+    std::cin >> item1 >> item2;
+       // first check that item1 and item2 represent the same book
+       if (item1.isbn() == item2.isbn()) {
+       std::cout << item1 + item2 << std::endl;
+       return 0;   // indicate success
+       } else {
+       std::cerr << "Data must refer to same ISBN" 
+                         << std::endl;
+       return -1;  // indicate failure
+       }
+}
diff --git a/1/avg_price.cc b/1/avg_price.cc
new file mode 100644 (file)
index 0000000..05d6d3e
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+#include "Sales_item.h"
+
+int main() 
+{
+    Sales_item total; // variable to hold data for the next transaction
+
+    // read the first transaction and ensure that there are data to process
+    if (std::cin >> total) {
+               Sales_item trans; // variable to hold the running sum
+        // read and process the remaining transactions
+        while (std::cin >> trans) {
+                       // if we're still processing the same book
+            if (total.isbn() == trans.isbn()) 
+                total += trans; // update the running total 
+            else {              
+                       // print results for the previous book 
+                std::cout << total << std::endl;  
+                total = trans;  // total now refers to the next book
+            }
+               }
+        std::cout << total << std::endl; // print the last transaction
+    } else {
+        // no input! warn the user
+        std::cerr << "No data?!" << std::endl;
+        return -1;  // indicate failure
+    }
+
+    return 0;
+}
diff --git a/1/data/add b/1/data/add
new file mode 100644 (file)
index 0000000..a07a471
--- /dev/null
@@ -0,0 +1 @@
+3 7 
diff --git a/1/data/add_item b/1/data/add_item
new file mode 100644 (file)
index 0000000..92fd61c
--- /dev/null
@@ -0,0 +1,2 @@
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/1/data/book_sales b/1/data/book_sales
new file mode 100644 (file)
index 0000000..0d14afe
--- /dev/null
@@ -0,0 +1,10 @@
+0-201-70353-X 4 24.99
+0-201-82470-1 4 45.39
+0-201-88954-4 2 15.00 
+0-201-88954-4 5 12.00 
+0-201-88954-4 7 12.00 
+0-201-88954-4 2 12.00 
+0-399-82477-1 2 45.39
+0-399-82477-1 3 45.39
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/1/data/mysum b/1/data/mysum
new file mode 100644 (file)
index 0000000..3c2bb02
--- /dev/null
@@ -0,0 +1 @@
+3 4 5 6
diff --git a/1/data/occurs b/1/data/occurs
new file mode 100644 (file)
index 0000000..dde16e2
--- /dev/null
@@ -0,0 +1 @@
+42 42 42 42 42 55 55 62 100 100 100
diff --git a/1/for_ex.cc b/1/for_ex.cc
new file mode 100644 (file)
index 0000000..3e3da2f
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+int main()
+{
+       int sum = 0;
+       for (int i = -100; i <= 100; ++i)
+       sum += i;
+       std::cout << sum << std::endl;
+       return 0;
+}
diff --git a/1/forcount.cc b/1/forcount.cc
new file mode 100644 (file)
index 0000000..8ced398
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+
+int main()
+{
+    int sum = 0;
+
+    // sum values from 1 through 10 inclusive
+    for (int val = 1; val <= 10; ++val) 
+        sum += val;  // equivalent to sum = sum + val
+    std::cout << "Sum of 1 to 10 inclusive is " 
+              << sum << std::endl;
+    return 0;
+}
+
diff --git a/1/item_io.cc b/1/item_io.cc
new file mode 100644 (file)
index 0000000..8e46602
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+#include "Sales_item.h"
+
+int main() 
+{
+    Sales_item book;
+
+    // read ISBN, number of copies sold, and sales price
+    std::cin >> book;
+    // write ISBN, number of copies sold, total revenue, and average price
+    std::cout << book << std::endl;
+
+    return 0;
+}
diff --git a/1/main_only.cc b/1/main_only.cc
new file mode 100644 (file)
index 0000000..b3a362e
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+int main() 
+{
+       return 0;
+}
diff --git a/1/makefile b/1/makefile
new file mode 100644 (file)
index 0000000..30cb6d9
--- /dev/null
@@ -0,0 +1,10 @@
+# executable files for this directory
+OBJECTS = add.exe add_item.exe add_item2.exe avg_price.exe \
+                 for_ex.exe forcount.exe item_io.exe \
+                 main_only.exe mysum.exe occurs.exe whilecount.exe 
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+add_item.exe add_item2.exe avg_price.exe item_io.exe: Sales_item.h
diff --git a/1/mysum.cc b/1/mysum.cc
new file mode 100644 (file)
index 0000000..d8f1e27
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream> 
+
+int main() 
+{
+       int sum = 0, value = 0;
+
+       // read until end-of-file, calculating a running total of all values read
+       while (std::cin >> value) 
+               sum += value; // equivalent to sum = sum + value
+
+       std::cout << "Sum is: " << sum << std::endl;
+       return 0;
+}
diff --git a/1/occurs.cc b/1/occurs.cc
new file mode 100644 (file)
index 0000000..66830fa
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+
+int main()
+{
+       // currVal is the number we're counting; we'll read new values into val
+       int currVal = 0, val = 0;
+
+       // read first number and ensure that we have data to process
+       if (std::cin >> currVal) {        
+               int cnt = 1;  // store the count for the current value we're processing
+               while (std::cin >> val) { // read the remaining numbers 
+                       if (val == currVal)   // if the values are the same
+                               ++cnt;            // add 1 to cnt 
+                       else { // otherwise, print the count for the previous value
+                               std::cout << currVal << " occurs " 
+                                         << cnt << " times" << std::endl;
+                               currVal = val;    // remember the new value
+                               cnt = 1;          // reset the counter
+                       }
+               }  // while loop ends here
+               // remember to print the count for the last value in the file
+               std::cout << currVal << " occurs " 
+                         << cnt << " times" << std::endl;
+       } // outermost if statement ends here
+       return 0;
+}
diff --git a/1/runpgms b/1/runpgms
new file mode 100644 (file)
index 0000000..1370a6b
--- /dev/null
+++ b/1/runpgms
@@ -0,0 +1,9 @@
+echo "add:" && ./add < data/add 
+echo "add_item:" && ./add_item < data/add_item
+echo "add_item2:" && ./add_item2 < data/add_item
+echo "avg_price:" && ./avg_price < data/book_sales
+echo "item_io:" && ./item_io < data/book_sales 
+echo "occurs:" && ./occurs < data/occurs
+echo "mysum:" && ./mysum < data/mysum
+
+../noinput for_ex forcount main_only whilecount
diff --git a/1/whilecount.cc b/1/whilecount.cc
new file mode 100644 (file)
index 0000000..76cf572
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+
+int main()
+{
+    int sum = 0, val = 1;
+    // keep executing the while as long as val is less than or equal to 10
+    while (val <= 10) {
+        sum += val;  // assigns sum + val to sum
+        ++val;       // add 1 to val
+    }
+    std::cout << "Sum of 1 to 10 inclusive is " 
+              << sum << std::endl;
+
+       return 0;
+}
+
diff --git a/10/README b/10/README
new file mode 100644 (file)
index 0000000..377713c
--- /dev/null
+++ b/10/README
@@ -0,0 +1,24 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   absInt           absInt
+   accum            accum
+   count-size       elimDups
+   count-size       bind2
+   count-size       newcount-size
+   book_sales       avg_price
+   book_sales       sortSI
+   iostream_iter    accum4
+   iostream_iter    equiv-istream
+   iostream_iter    iostream_iter
+   iostream_iter    ostreamIter2
+   iostream_iter    use-istream-iter
+   rcomma           rcomma
+   reverse-iter     reverse-iter
+   use_find         use_find
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/10/absInt.cc b/10/absInt.cc
new file mode 100644 (file)
index 0000000..9a21f9e
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include <iterator>
+using std::inserter;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <algorithm>
+using std::for_each; using std::transform;
+
+int main() {
+       vector<int> vi;
+       int i;
+       while (cin >> i)
+               vi.push_back(i);
+       // pass a lambda to for_each to print each element in vi
+       for_each(vi.begin(), vi.end(), [](int i) { cout << i << " "; });
+       cout << endl;
+
+       vector<int> orig = vi; // save original data in vi
+
+       // replace negative values by their absolute value
+       transform(vi.begin(), vi.end(), vi.begin(),
+                 [](int i) { return i < 0 ? -i : i; });
+       // print the elements now in vi
+       for_each(vi.begin(), vi.end(), [](int i) { cout << i << " "; });
+       cout << endl;
+
+       vi = orig;  // start afresh
+       // use a lambda with a specified return type to transform vi
+       transform(vi.begin(), vi.end(), vi.begin(),
+                 [](int i) -> int 
+                 { if (i < 0) return -i; else return i; });
+       // print the elements now in vi
+       for_each(vi.begin(), vi.end(), [](int i) { cout << i << " "; });
+       cout << endl;
+
+    return 0;
+}
diff --git a/10/accum.cc b/10/accum.cc
new file mode 100644 (file)
index 0000000..aed0301
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+#include <algorithm>
+using std::fill; using std::fill_n;
+
+#include <numeric>
+using std::accumulate;
+
+#include <iterator>
+using std::back_inserter;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       vector<int> vec(10);              // default initialized to 0
+       fill(vec.begin(), vec.end(), 1);  // reset each element to 1
+
+       // sum the elements in vec starting the summation with the value 0
+       int sum = accumulate(vec.cbegin(), vec.cend(), 0);
+       cout << sum << endl;
+       
+       // set a subsequence of the container to 10
+       fill(vec.begin(), vec.begin() + vec.size()/2, 10);
+       cout << accumulate(vec.begin(), vec.end(), 0) << endl;
+
+       // reset the same subsequence to 0
+       fill_n(vec.begin(), vec.size()/2, 0);
+       cout << accumulate(vec.begin(), vec.end(), 0) << endl;
+
+       // create 10 elements on the end of vec each with the value 42
+       fill_n(back_inserter(vec), 10, 42);
+       cout << accumulate(vec.begin(), vec.end(), 0) << endl;
+       
+       // concatenate elements in a vector of strings and store in sum 
+       vector<string> v;
+       string s;
+       while (cin >> s)
+               v.push_back(s);
+       string concat = accumulate(v.cbegin(), v.cend(), string(""));
+       cout << concat << endl;
+       
+       return 0;
+}
+
diff --git a/10/accum4.cc b/10/accum4.cc
new file mode 100644 (file)
index 0000000..9b0426a
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <numeric>
+using std::accumulate;
+
+#include <iterator>
+using std::istream_iterator;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       istream_iterator<int> in(cin), eof;
+       cout << accumulate(in, eof, 0) << endl;
+       return 0;
+}
diff --git a/10/avg_price.cc b/10/avg_price.cc
new file mode 100644 (file)
index 0000000..9f94861
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iterator>
+using std::istream_iterator; using std::ostream_iterator;
+
+#include <iostream>
+using std::cin; using std::cout;
+
+#include "Sales_item.h"
+
+int main() 
+{
+       // iterators that can read and write Sales_items
+       istream_iterator<Sales_item> item_iter(cin), eof;
+       ostream_iterator<Sales_item> out_iter(cout, "\n");
+
+       // store the first transaction in sum and read the next record
+       Sales_item sum = *item_iter++; 
+
+       while (item_iter != eof) {
+               // if the current transaction (which is in item_iter) 
+               // has the same ISBN
+           if (item_iter->isbn() == sum.isbn())
+               sum += *item_iter++; // add it to sum 
+                                        // and read the next transaction
+           else {
+               out_iter = sum;      // write the current sum
+               sum = *item_iter++;  // read the next transaction
+           }
+       }
+       out_iter = sum;  // remember to print the last set of records
+
+       return 0;
+}
diff --git a/10/bind2.cc b/10/bind2.cc
new file mode 100644 (file)
index 0000000..ce1cda0
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::for_each;
+
+#include <functional>
+using std::bind; using namespace std::placeholders;
+using std::ref;
+
+#include <iterator>
+using std::istream_iterator;
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string; 
+
+#include <iostream>
+using std::cout; using std::cin; using std::endl;
+using std::ostream; 
+
+#include <fstream>
+using std::ifstream; using std::ofstream; 
+
+ostream &print(ostream &os, const string &s, char c)
+{
+       return os << s << c;
+}
+
+int main()
+{
+       string s;
+       vector<string> words;
+       while (cin >> s)
+               words.push_back(s);
+
+       for_each(words.begin(), words.end(), 
+                bind(print, ref(cout), _1, ' '));
+       cout << endl;
+
+       ofstream os("data/outFile1");
+       for_each(words.begin(), words.end(), 
+                bind(print, ref(os), _1, ' '));
+       os << endl;
+
+       ifstream is("data/outFile1");
+       istream_iterator<string> in(is), eof;
+       for_each(in, eof, bind(print, ref(cout), _1, '\n'));
+       cout << endl;
+
+       return 0;
+}
diff --git a/10/data/absInt b/10/data/absInt
new file mode 100644 (file)
index 0000000..9726e80
--- /dev/null
@@ -0,0 +1,15 @@
+-42
+65
+-95
+100
+39
+-67
+95
+76
+-88
+76
+83
+92
+76
+-93
+0
diff --git a/10/data/accum b/10/data/accum
new file mode 100644 (file)
index 0000000..5bfcb72
--- /dev/null
@@ -0,0 +1 @@
+now is the time for all good fellows to come to the aid of the party
diff --git a/10/data/book_sales b/10/data/book_sales
new file mode 100644 (file)
index 0000000..0d14afe
--- /dev/null
@@ -0,0 +1,10 @@
+0-201-70353-X 4 24.99
+0-201-82470-1 4 45.39
+0-201-88954-4 2 15.00 
+0-201-88954-4 5 12.00 
+0-201-88954-4 7 12.00 
+0-201-88954-4 2 12.00 
+0-399-82477-1 2 45.39
+0-399-82477-1 3 45.39
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/10/data/count-size b/10/data/count-size
new file mode 100644 (file)
index 0000000..da448a3
--- /dev/null
@@ -0,0 +1 @@
+the quick red fox jumps over the slow red turtle
diff --git a/10/data/iostream_iter b/10/data/iostream_iter
new file mode 100644 (file)
index 0000000..2451c69
--- /dev/null
@@ -0,0 +1 @@
+23 109 45 89 6 34 12 90 34 23 56 23 8 89 23
diff --git a/10/data/outFile1 b/10/data/outFile1
new file mode 100644 (file)
index 0000000..e2b4b0d
--- /dev/null
@@ -0,0 +1 @@
+the quick red fox jumps over the slow red turtle \r
diff --git a/10/data/outFile2 b/10/data/outFile2
new file mode 100644 (file)
index 0000000..5095c75
--- /dev/null
@@ -0,0 +1 @@
+6 8 12 23 23 23 23 34 34 45 56 89 89 90 109 \r
diff --git a/10/data/rcomma b/10/data/rcomma
new file mode 100644 (file)
index 0000000..bc224c0
--- /dev/null
@@ -0,0 +1 @@
+FIRST,MIDDLE,LAST
diff --git a/10/data/readme b/10/data/readme
new file mode 100644 (file)
index 0000000..b2faf07
--- /dev/null
@@ -0,0 +1,24 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   SI_data          arrow
+   absInt           absInt
+   count-size       bind
+   count-size       bind2
+   count-size       newcount-size
+   book_sales       avg_price
+   iostream_iter    accum4
+   iostream_iter    equiv-istream
+   iostream_iter    iostream_iter
+   iostream_iter    ostreamIter2
+   iostream_iter    use-istream-iter
+   parms            parms
+   readme           readStr
+   reverse-iter     reverse-iter
+   use_find         use_find
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/10/data/reverse-iter b/10/data/reverse-iter
new file mode 100644 (file)
index 0000000..23ee009
--- /dev/null
@@ -0,0 +1 @@
+1 7 3 5 9
diff --git a/10/data/use_find b/10/data/use_find
new file mode 100644 (file)
index 0000000..d544c40
--- /dev/null
@@ -0,0 +1,3 @@
+42
+47
+83
diff --git a/10/elimDups.cc b/10/elimDups.cc
new file mode 100644 (file)
index 0000000..0e17e5b
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::sort; using std::for_each;
+
+#include <functional>
+using std::bind; 
+using namespace std::placeholders;
+
+#include <string>
+using std::string;
+
+#include <vector>
+using std::vector; 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl; 
+
+// comparison function to be used to sort by word length
+bool isShorter(const string &s1, const string &s2)
+{
+    return s1.size() < s2.size();
+}
+
+bool LT(const string &s1, const string &s2)
+{
+       return s1 < s2;
+}
+
+void print(const vector<string> &words)
+{
+       for_each(words.begin(), words.end(),
+               [](const string &s) { cout << s << " "; });
+       cout << endl;
+}
+
+int main()
+{
+    vector<string> words;
+
+    // copy contents of each book into a single vector
+    string next_word;
+    while (cin >> next_word) {
+        // insert next book's contents at end of words
+        words.push_back(next_word);
+    }
+       print(words);
+
+       vector<string> cpy = words; // save the original data
+
+       // uses string < to compare elements
+       // sort and print the vector
+       sort(words.begin(), words.end());
+
+       words = cpy;  // return to the original data
+       // uses the LT function to compare elements
+       // should have the same output as the previous sort
+       sort(words.begin(), words.end(), LT);
+    print(words);
+
+       words = cpy;  // return to the original data
+
+       // eliminate duplicates
+       sort(words.begin(), words.end());
+       auto it = unique(words.begin(), words.end());
+       words.erase(it, words.end());
+
+       // sort by length using a function
+       stable_sort(words.begin(), words.end(), isShorter);  
+    print(words);
+
+       words = cpy; // return to the original data
+       // sort the original input on word length, shortest to longest
+       sort(words.begin(), words.end(), isShorter);  
+    print(words);
+
+       // use bind to invert isShorter to sort longest to shortest
+       sort(words.begin(), words.end(), bind(isShorter, _2, _1));  
+    print(words);
+
+       return 0;
+}
diff --git a/10/equal.cc b/10/equal.cc
new file mode 100644 (file)
index 0000000..f90199b
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::equal;
+
+#include <list>
+using std::list;
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       list<const char *> roster1;
+       vector<string> roster2;
+       roster2 = {"hello", "so long", "tata"};
+       roster1 = {"hello", "so long"};
+
+       auto b = 
+       // roster2 should have at least as many elements as roster1
+       equal(roster1.cbegin(), roster1.cend(), roster2.cbegin());
+
+       (b) ? cout << "true" : cout << "false";
+       cout << endl;
+
+       return 0;
+}
diff --git a/10/equiv-istream-iter.cc b/10/equiv-istream-iter.cc
new file mode 100644 (file)
index 0000000..1454ff1
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iterator>
+using std::istream_iterator;
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main() {
+       // use istream_iterator to initialize a vector
+       istream_iterator<int> in_iter(cin), eof;  
+       vector<int> vec(in_iter, eof);  
+
+       for (auto it : vec)
+               cout << it << " ";
+       cout << endl;
+
+       return 0;
+}
+
diff --git a/10/inserters.cc b/10/inserters.cc
new file mode 100644 (file)
index 0000000..07d82da
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::copy;
+
+#include <list>
+using std::list;
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <iterator>
+using std::inserter; using std::front_inserter;
+
+void print(const string &label, const list<int> &lst)
+{
+       cout << label << endl;
+       for (auto iter : lst)
+        cout << iter << " ";
+    cout << endl;
+}
+
+int main() 
+{
+
+    list<int> lst = {1,2,3,4};
+       print("lst", lst);
+
+    // after copy completes, lst2 contains 4 3 2 1
+       list<int> lst2, lst3;     // empty lists
+    copy(lst.cbegin(), lst.cend(), front_inserter(lst2));
+
+    // after copy completes, lst3 contains 1 2 3 4
+    copy(lst.cbegin(), lst.cend(), inserter(lst3, lst3.begin()));
+
+       print("lst2", lst2);
+       print("lst3", lst3);
+       
+       vector<int> v = {1,2,3,4,5};
+       list<int> new_lst = {6,7,8,9};
+       auto it = new_lst.begin();
+       copy(v.begin(), v.end(), inserter(new_lst, it));
+       print("new_lst", new_lst);
+
+       return 0;
+}
diff --git a/10/iostream_iter.cc b/10/iostream_iter.cc
new file mode 100644 (file)
index 0000000..8730474
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::copy; using std::sort; using std::unique_copy;
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+#include <iterator>
+using std::istream_iterator; using std::ostream_iterator;
+
+#include <fstream>
+using std::ofstream;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+    istream_iterator<int> int_it(cin); // reads ints from cin
+    istream_iterator<int> int_eof;     // end iterator value
+       vector<int> v(int_it, int_eof);    // initialize v by reading cin
+
+    sort(v.begin(), v.end());
+    ostream_iterator<int> out(cout, " "); // write ints to cout
+    unique_copy(v.begin(), v.end(), out); // write unique elements to cout
+    cout << endl;                         // write a newline after the output
+       ofstream out_file("data/outFile2");   // writes int to named file
+       ostream_iterator<int> out_iter(out_file, " ");
+       copy(v.begin(), v.end(), out_iter); 
+       out_file << endl;  // write a newline at end of the file
+    return 0;
+}
diff --git a/10/lambda.cc b/10/lambda.cc
new file mode 100644 (file)
index 0000000..229618d
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+#include <string>
+#include <iostream>
+using std::cout; using std::endl; using std::vector; using std::string;
+
+// five functions illustrating different aspects of lambda expressions
+void fcn1()
+{
+       size_t v1 = 42;  // local variable
+       // copies v1 into the callable object named f
+       auto f = [v1] { return v1; };
+       v1 = 0;
+       auto j = f(); // j is 42; f stored a copy of v1 when we created it
+       cout << j << endl;
+}
+
+void fcn2()
+{
+       size_t v1 = 42;  // local variable
+       // the object f2 contains a reference to v1 
+       auto f2 = [&v1] { return v1; };
+       v1 = 0;
+       auto j = f2(); // j is 0; f2 refers to v1; it doesn't store it
+       cout << j << endl;
+}
+
+void fcn3()
+{
+       size_t v1 = 42;  // local variable
+       // f can change the value of the variables it captures
+       auto f = [v1] () mutable { return ++v1; };
+       v1 = 0;
+       auto j = f(); // j is 43
+       cout << j << endl;
+}
+
+void fcn4()
+{
+       size_t v1 = 42;  // local variable
+       // v1 is a reference to a nonconst variable
+       // we can change that variable through the reference inside f2
+       auto f2 = [&v1] { return ++v1; };
+       v1 = 0;
+       auto j = f2(); // j is 1
+       cout << j << endl;
+}
+
+void fcn5()
+{
+    size_t v1 = 42;
+       // p is a const pointer to v1
+    size_t* const p = &v1;
+       // increments v1, the objet to which p points
+    auto f = [p]() { return ++*p; };
+    auto j = f();  // returns incremented value of *p
+    cout << v1 << " " << j << endl; // prints 43 43
+    v1 = 0;
+    j = f();       // returns incremented value of *p
+    cout << v1 << " " << j << endl; // prints 1 1
+}
+
+
+int main()
+{
+       fcn1();
+       fcn2();
+       fcn3();
+       fcn4();
+       fcn5();
+       return 0;
+}
diff --git a/10/makefile b/10/makefile
new file mode 100644 (file)
index 0000000..3e00b1a
--- /dev/null
@@ -0,0 +1,15 @@
+# executable files for this directory
+OBJECTS = absInt.exe accum.exe accum4.exe \
+                 avg_price.exe bind2.exe elimDups.exe equal.exe \
+                 equiv-istream-iter.exe inserters.exe \
+                 iostream_iter.exe lambda.exe newcount-size.exe \
+                 ostreamIter2.exe rcomma.exe \
+                 reverse.exe reverse-iter.exe rev-iters.exe \
+                 sortSI.exe use_find.exe use-istream-iter.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../1 -I../6
+
diff --git a/10/newcount-size.cc b/10/newcount-size.cc
new file mode 100644 (file)
index 0000000..9c7043e
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::find_if; using std::for_each;
+using std::sort; using std::stable_sort; 
+using std::unique; 
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+#include <fstream>
+using std::ifstream;
+
+#include <cstddef>
+using std::size_t;
+
+#include <functional>
+using std::bind; 
+using std::placeholders::_1;
+using namespace std::placeholders;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include "make_plural.h"
+
+// comparison function to be used to sort by word length
+bool isShorter(const string &s1, const string &s2)
+{
+    return s1.size() < s2.size();
+}
+
+// determine whether a length of a given word is 6 or more
+bool GT(const string &s, string::size_type m) 
+{
+    return s.size() >= m;
+}
+
+void elimDups(vector<string> &words)
+{
+    // sort words alphabetically so we can find the duplicates
+    sort(words.begin(), words.end());
+       for_each(words.begin(), words.end(), 
+                [](const string &s) { cout << s << " "; });
+       cout << endl;
+
+
+    // unique reorders the input so that each word appears once in the
+    // front part of the range 
+       // returns an iterator one past the unique range
+    auto end_unique = unique(words.begin(), words.end());
+       for_each(words.begin(), words.end(), 
+                [](const string &s) { cout << s << " "; });
+       cout << endl;
+
+    // erase uses a vector operation to remove the nonunique elements
+    words.erase(end_unique, words.end());
+       for_each(words.begin(), words.end(), 
+                [](const string &s) { cout << s << " "; });
+       cout << endl;
+}
+
+void 
+biggies(vector<string> &words, vector<string>::size_type sz)
+{
+       elimDups(words); // put words in alphabetical order and remove duplicates
+    // sort words by size, but maintain alphabetical order for words of the same size
+    stable_sort(words.begin(), words.end(), 
+                   [](const string &a, const string &b) 
+                     { return a.size() < b.size();});
+
+       // get an iterator to the first element whose size() is >= sz
+    auto wc = find_if(words.begin(), words.end(), 
+                [sz](const string &a) 
+                       { return a.size() >= sz; });
+
+       // compute the number of elements with size >= sz 
+       auto count = words.end() - wc;
+    cout << count << " " << make_plural(count, "word", "s")
+         << " of length " << sz << " or longer" << endl;
+
+       // print words of the given size or longer, each one followed by a space
+       for_each(wc, words.end(), 
+                [](const string &s){cout << s << " ";});
+       cout << endl;
+}
+
+bool check_size(const string &s, string::size_type sz)
+{
+       return s.size() >= sz;
+}
+
+int main()
+{
+    vector<string> words;
+
+    // copy contents of each book into a single vector
+    string next_word;
+    while (cin >> next_word) {
+        // insert next book's contents at end of words
+        words.push_back(next_word);
+    }
+
+       biggies(words, 5); // biggies changes its first argument
+
+       // alternative solution using bind and check_size function
+       // NB: words was changed inside biggies, 
+       //     at this point in the program words has only unique
+       //     words and is in order by size
+       size_t sz = 5;
+       auto
+       wc = find_if(words.begin(), words.end(), 
+                    bind(check_size, std::placeholders::_1, sz));
+       auto count = words.end() - wc;
+    cout << count << " " << make_plural(count, "word", "s")
+         << " of length " << sz << " or longer" << endl;
+
+       return 0;
+}
diff --git a/10/ostreamIter2.cc b/10/ostreamIter2.cc
new file mode 100644 (file)
index 0000000..4e3bf6a
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iterator>
+using std::istream_iterator; using std::ostream_iterator;
+
+#include<vector>
+using std::vector;
+
+#include<iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main() {
+       vector<int> vec;
+       istream_iterator<int> in_iter(cin);  // read ints from cin
+       istream_iterator<int> eof;           // istream ``end'' iterator
+       
+       while (in_iter != eof)
+               vec.push_back(*in_iter++);
+       ostream_iterator<int> out_iter(cout, " ");
+       
+       for (auto e : vec)
+               out_iter = e;  // the assignment writes this element to cout
+       cout << endl;
+
+       return 0;
+}
+
diff --git a/10/outrunpgms b/10/outrunpgms
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/10/rcomma.cc b/10/rcomma.cc
new file mode 100644 (file)
index 0000000..d126cab
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <algorithm>
+using std::find;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       string line;
+       getline(cin, line);
+
+       // find the first element in a comma-separated list
+       auto comma = find(line.cbegin(), line.cend(), ',');
+       cout << string(line.cbegin(), comma) << endl;
+       
+       // find the last element in a comma-separated list
+       auto rcomma = find(line.crbegin(), line.crend(), ',');
+       
+       // WRONG: will generate the word in reverse order
+       cout << string(line.crbegin(), rcomma) << endl;
+       
+       // ok: get a forward iterator and read to the end of line
+       cout << string(rcomma.base(), line.cend()) << endl;
+
+       return 0;
+}
diff --git a/10/rev-iters.cc b/10/rev-iters.cc
new file mode 100644 (file)
index 0000000..1a4da4b
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include<iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+    vector<int> vec = {0,1,2,3,4,5,6,7,8,9};
+    // reverse iterator of vector from back to front
+    for (auto r_iter = vec.crbegin(); // binds r_iter to the last element
+              r_iter != vec.crend();  // one before first element
+              ++r_iter)          // decrements the iterator one element
+        cout << *r_iter << endl; // prints 9, 8, 7, . . . 0
+    return 0;
+}
diff --git a/10/reverse-iter.cc b/10/reverse-iter.cc
new file mode 100644 (file)
index 0000000..989d6db
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::copy; using std::sort;
+
+#include <iterator>
+using std::istream_iterator; using std::ostream_iterator;
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       vector<int> vec;
+       istream_iterator<int> in(cin), eof;
+       copy (in, eof, back_inserter(vec));
+
+       sort(vec.begin(), vec.end()); // sorts vec in ``normal'' order
+       vector<int> vec2(vec);        // vec2 is a copy of vec
+
+       // sorts in reverse: puts the smallest element at the end of vec
+       sort(vec.rbegin(), vec.rend());
+
+       // prints vec
+       ostream_iterator<int> out(cout, " ");
+       copy(vec.begin(), vec.end(), out);    // reverse sort
+       cout << endl;
+
+       // prints vec2
+       copy(vec2.begin(), vec2.end(), out);  // forward sort
+       cout << endl;
+
+       // it refers to the first element in vec
+       auto it = vec.begin();
+
+       // rev_it refers one before the first element in vec
+       vector<int>::reverse_iterator rev_it(it);
+       cout << *(rev_it.base()) << endl; // prints first element in vec
+       
+       // ways to print value at beginning of vec through it and rev_it
+       cout << *it << endl;
+       cout << *(rev_it.base()) << endl;  
+       cout << *(rev_it - 1) << endl;
+
+       return 0;
+}
+               
diff --git a/10/reverse.cc b/10/reverse.cc
new file mode 100644 (file)
index 0000000..883701a
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::find; using std::find_if; using std::for_each;
+using std::remove_copy_if; using std::reverse_copy;
+using std::reverse; 
+
+#include <iterator>
+using std::back_inserter;
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       vector<int> v1 = {0,1,2,3,4,5,6,7,8,9};
+       vector<int> v2;
+       vector<int> v3 = v1;
+
+       find(v1.begin(), v1.end(), 42);  // find first element equal to 42 
+       find_if(v1.begin(), v1.end(),    // find first odd element
+               [](int i) { return i % 2; });
+
+       // puts elements in v1 into reverse order
+       reverse(v1.begin(), v1.end());  
+       v1 = v3;  // restore original data
+
+       // copies elements from v1 into v2 in reverse order; v1 is unchanged
+       reverse_copy(v1.begin(), v1.end(), back_inserter(v2));
+
+       for (auto i : v1) 
+               cout << i << " ";   // prints 0 1 2 . . . 8 9
+       cout << endl;
+       for (auto i : v2) 
+               cout << i << " ";   // prints 9 8 7 . . . 1 0
+       cout << endl;
+
+       // removes the odd elements from v1
+       auto it = remove_if(v1.begin(), v1.end(), 
+                           [](int i) { return i % 2; });
+       // prints 0 2 4 6 8
+       for_each(v1.begin(), it, [](int i) { cout << i << " "; });
+       cout << endl;
+
+       v1 = v3;    // restore original data
+       v2.clear(); // make v2 empty again
+
+       // copies only the even elements from v1 into v2; v1 is unchanged
+       remove_copy_if(v1.begin(), v1.end(), back_inserter(v2),
+                      [](int i) { return i % 2; });
+       for (auto i : v2) 
+               cout << i << " ";  // prints 0 2 4 6 8
+       cout << endl;
+       for (auto i : v1)      // prints 0 1 2 . . . 8 9
+               cout << i << " ";
+       cout << endl;
+
+       return 0;
+}
diff --git a/10/runpgms b/10/runpgms
new file mode 100644 (file)
index 0000000..0ac0b98
--- /dev/null
@@ -0,0 +1,18 @@
+echo "absInt:" && ./absInt.exe < data/absInt
+echo "accum:" && ./accum.exe < data/accum
+echo "accum4:" && ./accum4.exe < data/iostream_iter
+echo "avg_price:" && ./avg_price.exe < data/book_sales
+echo "bind2:" && ./bind2.exe < data/count-size
+echo "elimDups: " && ./elimDups < data/count-size
+echo "equiv-istream-iter:" && ./equiv-istream-iter.exe < data/iostream_iter
+echo "iostream_iter:" && ./iostream_iter.exe < data/iostream_iter
+echo "newcount-size:" && ./newcount-size.exe < data/count-size
+echo "ostreamIter2:" && ./ostreamIter2.exe < data/iostream_iter
+echo "rcomma:" && ./rcomma.exe < data/rcomma
+echo "reverse-iter:" && ./reverse-iter.exe < data/reverse-iter
+echo "sortSI:" && ./sortSI.exe < data/book_sales
+echo "use_find:" && ./use_find.exe < data/use_find
+echo "use-istream-iter:" && ./use-istream-iter.exe < data/iostream_iter
+
+../noinput equal.exe inserters.exe lambda.exe reverse.exe \
+                  rev-iters.exe 
diff --git a/10/sortSI.cc b/10/sortSI.cc
new file mode 100644 (file)
index 0000000..1e323f9
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::sort;
+
+#include <vector>
+using std::vector; 
+
+#include <iostream>
+using std::cout; using std::cin; using std::endl;
+
+#include "Sales_item.h"
+
+int main() 
+{
+    Sales_item trans;
+    vector<Sales_item> file;
+
+    while (std::cin >> trans)  // read the transations
+        file.push_back(trans);
+
+       for (auto i : file)        // print what was read
+               cout << i << endl;
+       cout << "\n\n";
+
+       sort(file.begin(), file.end(), compareIsbn); // sort into ISBN order
+       for (auto i : file)        // print in ISBN order
+               cout << i << endl;
+
+       return 0;
+}
+
diff --git a/10/use-istream-iter.cc b/10/use-istream-iter.cc
new file mode 100644 (file)
index 0000000..88f8f83
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::copy;
+
+#include <iterator>
+using std::istream_iterator; using std::ostream_iterator;
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main() {
+       vector<int> vec;
+       istream_iterator<int> in_iter(cin);  // read ints from cin
+       istream_iterator<int> eof;           // istream ``end'' iterator
+
+       // use in_iter to read cin storing what we read in vec
+       while (in_iter != eof)  // while there's valid input to read
+               // postfix increment reads the stream and 
+               // returns the old value of the iterator
+               // we dereference that iterator to get 
+               // the previous value read from the stream 
+               vec.push_back(*in_iter++);  
+
+       // use an ostream_iterator to print the contents of vec
+       ostream_iterator<int> out_iter(cout, " ");
+       copy(vec.begin(), vec.end(), out_iter);
+       cout << endl;
+       
+       // alternative way to print contents of vec
+       for (auto e : vec)
+               *out_iter++ = e;  // the assignment writes this element to cout
+       cout << endl;
+       
+       return 0;
+}
+       
diff --git a/10/use_find.cc b/10/use_find.cc
new file mode 100644 (file)
index 0000000..f0f0316
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::find;
+
+#include <iterator>
+using std::begin; using std::end;
+
+#include <vector>
+using std::vector;
+
+#include <list>
+using std::list;
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       int ia[] = {27, 210, 12, 47, 109, 83};
+       int val = 83;
+       int* result = find(begin(ia), end(ia), val);
+       cout << "The value " << val
+            << (result == end(ia)
+                  ? " is not present" : " is present") << endl;
+       
+       // search starting from ia[1] up to but not including ia[4]
+       result = find(ia + 1, ia + 4, val);
+       
+       // initialize the vector with some values
+       vector<int> vec = {27, 210, 12, 47, 109, 83};
+       val = 42; // value we'll look for
+
+       // result2 will denote the element we want if it's in vec, 
+       // or vec.cend() if not
+       auto result2 = find(vec.cbegin(), vec.cend(), val);
+
+       // report the result
+       cout << "The value " << val
+            << (result2 == vec.cend()
+                  ? " is not present" : " is present") << endl;
+       
+       // now use find to look in a list of strings
+       list<string> lst = {"val1", "val2", "val3"};
+
+       string sval = "a value";  // value we'll look for
+       // this call to find looks through string elements in a list
+       auto result3 = find(lst.cbegin(), lst.cend(), sval);
+       cout << "The value " << sval
+            << (result3 == lst.cend()
+                  ? " is not present" : " is present") << endl;
+
+       return 0;
+}
+       
diff --git a/11/README b/11/README
new file mode 100644 (file)
index 0000000..ee650fa
--- /dev/null
+++ b/11/README
@@ -0,0 +1,15 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   storyDatafile    wcEx
+   storyDatafile    erasemap
+   storyDatafile    pair
+   storyDatafile    word_count
+   word_count       restricted_count
+   word_count       unorderedWord_count
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/11/data/Alice b/11/data/Alice
new file mode 100644 (file)
index 0000000..08b6485
--- /dev/null
@@ -0,0 +1 @@
+Alice
diff --git a/11/data/rules b/11/data/rules
new file mode 100644 (file)
index 0000000..c63e9e3
--- /dev/null
@@ -0,0 +1,8 @@
+brb be right back
+k okay?
+y why
+r are
+u you
+pic picture
+thk thanks!
+l8r later
diff --git a/11/data/simple_query b/11/data/simple_query
new file mode 100644 (file)
index 0000000..21e6e0a
--- /dev/null
@@ -0,0 +1,2 @@
+data
+compiler
diff --git a/11/data/storyDataFile b/11/data/storyDataFile
new file mode 100644 (file)
index 0000000..1f06b36
--- /dev/null
@@ -0,0 +1,10 @@
+Alice Emma has long flowing red hair. 
+Her Daddy says when the wind blows 
+through her hair, it looks almost alive, 
+like a fiery bird in flight. 
+A beautiful fiery bird, he tells her, 
+magical but untamed. 
+"Daddy, shush, there is no such thing," 
+she tells him, at the same time wanting 
+him to tell her more.
+Shyly, she asks, "I mean, Daddy, is there?"
diff --git a/11/data/text b/11/data/text
new file mode 100644 (file)
index 0000000..c6fe124
--- /dev/null
@@ -0,0 +1,3 @@
+where r u
+y dont u send me a pic 
+k thk l8r
diff --git a/11/data/word_count b/11/data/word_count
new file mode 100644 (file)
index 0000000..bc13fed
--- /dev/null
@@ -0,0 +1,7 @@
+Although most programmers are familiar with data structures
+such as vectors and lists, many have never used an
+associative data structure.
+Before we look at the details of how the library supports
+these types, it will be helpful to start with
+examples of how we can use
+these containers.
diff --git a/11/erasemap.cc b/11/erasemap.cc
new file mode 100644 (file)
index 0000000..30f1d00
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <map>
+using std::map;
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main() 
+{
+    map<string, size_t> word_count;  // empty map from string to size_t
+    string word;
+    while (cin >> word)
+      ++word_count[word];
+
+       string removal_word = "the";
+       
+       // two ways to remove an entry from a map
+       // 1. by key
+       // erase on a key returns the number of elements removed
+       if (word_count.erase(removal_word))
+            cout << "ok: " << removal_word << " removed\n";
+       else cout << "oops: " << removal_word << " not found!\n";
+       
+       // 2. by removing an iterator to the element we want removed
+       removal_word = "The";  // strings are case sensitive
+       map<string,size_t>::iterator where;
+       where = word_count.find(removal_word);  // should be gone
+       
+       if (where == word_count.end())
+            cout << "oops: " << removal_word << " not found!\n";
+       else {
+            word_count.erase(where);   // erase iterator returns void
+            cout << "ok: " << removal_word << " removed!\n";
+       }
+           return 0;
+}
diff --git a/11/makefile b/11/makefile
new file mode 100644 (file)
index 0000000..530f735
--- /dev/null
@@ -0,0 +1,17 @@
+# executable files for this directory
+OBJECTS = erasemap.exe map1.exe \
+                 multimap.exe pair.exe \
+                 restricted_count.exe setOps.exe \
+                 unorderedWordCount.exe wcEx.exe \
+                 word_count.exe word_transform.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+# directory 7 has Sales_data code, 6 has make_plural function
+LOCFLAGS = -I../7 -I../6
+
+map1.exe: map1.o ../7/Sales_data.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) map1.o ../7/Sales_data.o -o map1.exe
+
diff --git a/11/map1.cc b/11/map1.cc
new file mode 100644 (file)
index 0000000..06ab7c3
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <set>
+using std::multiset;
+
+#include <map>
+using std::map;
+
+#include <string>
+using std::string;
+
+#include <utility>
+using std::pair;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include "Sales_data.h"      
+#include "make_plural.h"
+
+// SDComp points to a function that compares two Sales_data objects
+typedef bool(*SDComp)(const Sales_data&, const Sales_data&);
+
+// bookstore can have several transactions with the same ISBN
+// elements in bookstore will be in ISBN order
+multiset<Sales_data, decltype(compareIsbn)*> 
+       bookstore(compareIsbn);
+
+// alternative way to declare bookstore using a lambda
+multiset<Sales_data, SDComp> 
+       bookstore2([](const Sales_data &l, const Sales_data &r)
+                 { return l.isbn() < r.isbn(); });
+
+int main() 
+{
+       map <string, size_t> word_count; // empty map
+       
+       // insert a value-initialized element with key Anna; 
+       // assign 1 to the value of that element
+       word_count["Anna"] = 1;  
+
+       // fetch the element indexed by Anna; prints 1
+       cout << word_count["Anna"] << endl;  
+       
+       ++word_count["Anna"];        // fetch the element and add 1 to it
+       cout << word_count["Anna"] << endl; // prints 2
+       
+       // various ways to add word to word_count 
+       string word; 
+       word_count.insert({word, 1});
+       word_count.insert(make_pair(word, 1));
+       word_count.insert(pair<string, size_t>(word, 1));
+       word_count.insert(map<string, size_t>::value_type(word, 1));
+       
+       typedef map<string,size_t>::value_type valType;
+       word_count.insert(valType(word, 1));
+       
+       // use value returned by insert
+       pair<map<string, size_t>::iterator, bool> insert_ret;
+
+       // if Anna not already in word_count, insert it with value 1
+       insert_ret = word_count.insert({"Anna", 1});
+       
+       if (insert_ret.second == false)    // Anna was already in the map
+           insert_ret.first->second++;    // increment current value
+       cout << word_count["Anna"] << endl;
+
+       // get an iterator to an element in word_count
+       auto map_it = word_count.begin();
+       
+       // *map_it is a reference to a pair<const string, size_t> object
+       cout << map_it->first;         // prints the key for this element
+       cout << " " << map_it->second; // prints the value of the element
+       ++map_it->second;     // ok: we can change the value through an iterator
+       return 0;
+}
diff --git a/11/multimap.cc b/11/multimap.cc
new file mode 100644 (file)
index 0000000..689d618
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <map>
+using std::multimap;
+
+#include <string>
+using std::string;
+
+#include <utility>
+using std::pair;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+    // map from author to title; there can be multiple titles per author
+    multimap<string, string> authors;
+    // add data to authors
+    authors.insert({"Alain de Botton", "On Love"});
+    authors.insert({"Alain de Botton", "Status Anxiety"});
+    authors.insert({"Alain de Botton", "Art of Travel"});
+    authors.insert({"Alain de Botton", 
+                                     "Architecture of Happiness"});
+
+    string search_item("Alain de Botton"); // author we'll look for
+    auto entries = authors.count(search_item); // number of elements
+    auto iter = authors.find(search_item); // first entry for this author
+
+    // loop through the number of entries there are for this author
+    while(entries) {
+               cout << iter->second << endl; // print each title
+               ++iter;     // advance to the next title
+               --entries;  // keep track of how many we've printed
+       }
+
+    // definitions of authors and search_item as above
+    // beg and end denote the range of elements for this author
+    for (auto beg = authors.lower_bound(search_item),
+              end = authors.upper_bound(search_item);
+                beg != end; ++beg)
+        cout << beg->second << endl; // print each title
+
+    // definitions of authors and search_item as above
+    // pos holds iterators that denote the range of elements for this key
+    for (auto pos = authors.equal_range(search_item);
+         pos.first != pos.second; ++pos.first)
+        cout << pos.first->second << endl; // print each title
+
+       return 0;
+}
+
diff --git a/11/pair.cc b/11/pair.cc
new file mode 100644 (file)
index 0000000..b32b321
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <list>
+using std::list;
+
+#include <vector>
+using std::vector;
+
+#include <algorithm>
+using std::sort;
+
+#include <iterator>
+using std::istream_iterator; using std::ostream_iterator;
+
+#include <utility>
+using std::pair; using std::make_pair;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include "Sales_data.h"
+
+pair<string, string> anon;       // holds two strings
+pair<string, size_t> word_count; // holds a string and an size_t 
+pair<string, vector<int>> line;  // holds string and vector<int>
+
+// list initialize the members in a pair
+pair<string, string> author{"James", "Joyce"};
+
+// ways to initialize a pair
+typedef pair<string, string> Author;
+Author proust("Marcel", "Proust");  // construct a pair
+Author joyce{"James", "Joyce"};     // list initialize a pair
+Author austen = make_pair("Jane", "Austen"); // use make_pair
+
+void preliminaries(vector<string> &v)
+{
+       // process v
+       istream_iterator<string> input(cin), eof;
+       copy(input, eof, back_inserter(v));
+       sort(v.begin(), v.end(), 
+            [](const string &a, const string &b) 
+            { return a.size() < b.size(); });
+}
+
+// three different ways to return a pair
+// 1. list initialize the return value
+pair<string, int>
+process(vector<string> &v)
+{
+       if (!v.empty())
+               return {v.back(), v.back().size()}; // list initialize
+       else
+               return pair<string, int>(); // explicitly constructed return value
+}
+
+// 2. use make_pair to generate the return value
+pair<string, int>
+process2(vector<string> &v)
+{
+       // process v
+       if (!v.empty())
+               return make_pair(v.back(), v.back().size());
+       else
+               return pair<string, int>();
+}
+
+// 3. explicitly construct the return value
+pair<string, int>
+process3(vector<string> &v)
+{
+       // process v
+       if (!v.empty())
+               return pair<string, int>(v.back(), v.back().size());
+       else
+               return pair<string, int>();
+}
+
+int main()
+{
+       vector<string> v;
+       string s;
+       while (cin >> s)
+               v.push_back(s);
+
+       preliminaries(v);  // sort v into size order
+
+       // all three output statements should be the same 
+       auto biggest = process(v);
+       cout << biggest.first << " " << biggest.second << endl;
+       biggest = process2(v);
+       cout << biggest.first << " " << biggest.second << endl;
+       biggest = process3(v);
+       cout << biggest.first << " " << biggest.second << endl;
+}
diff --git a/11/restricted_count.cc b/11/restricted_count.cc
new file mode 100644 (file)
index 0000000..959218e
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <map>
+using std::map;
+
+#include <set>
+using std::set;
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main() 
+{
+    // count the number of times each word occurs in the input
+    map<string, size_t> word_count; // empty map from string to size_t
+       set<string> exclude = {"The", "But", "And", "Or", "An", "A", 
+                              "the", "but", "and", "or", "an", "a"};
+    string word;
+    while (cin >> word)
+               // count only words that are not in exclude
+               if (exclude.find(word) == exclude.end())
+                       ++word_count[word];   // fetch and increment the counter for word
+
+       for (const auto &w : word_count) // for each element in the map
+               // print the results
+               cout <<  w.first << " occurs " << w.second 
+                    << ((w.second > 1) ? " times" : " time") << endl;
+
+return 0;
+}
diff --git a/11/runpgms b/11/runpgms
new file mode 100644 (file)
index 0000000..ed101d0
--- /dev/null
@@ -0,0 +1,9 @@
+echo "wcEx:" && ./wcEx.exe < data/storyDataFile
+echo "erasemap:" && ./erasemap.exe < data/storyDataFile
+echo "restricted_count:" && ./restricted_count.exe < data/word_count
+echo "unorderedWordCount:" && ./unorderedWordCount.exe < data/word_count
+echo "word_count:" && ./word_count.exe < data/storyDataFile
+echo "pair:" && ./pair.exe < data/storyDataFile
+echo "word_transform:" && ./word_transform.exe data/rules data/text
+
+../noinput map1.exe multimap.exe setOps.exe 
diff --git a/11/setOps.cc b/11/setOps.cc
new file mode 100644 (file)
index 0000000..67a86ec
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <set>
+using std::set; using std::multiset;
+
+#include <string>
+using std::string;
+
+#include <vector>
+using std::vector; 
+
+#include <iostream>
+using std::cout; using std::endl; 
+
+int main() 
+{
+       // define a vector with 20 elements, 
+       // holding two copies of each number from 0 to 9
+       vector<int> ivec;
+       for (vector<int>::size_type i = 0; i != 10; ++i) {
+           ivec.push_back(i);
+           ivec.push_back(i);  // duplicate copies of each number
+       }
+       
+       // iset holds unique elements from ivec; miset holds all 20 elements
+       set<int> iset(ivec.cbegin(), ivec.cend());
+       multiset<int> miset(ivec.cbegin(), ivec.cend());
+       
+       cout << ivec.size() << endl;    // prints 20
+       cout << iset.size() << endl;    // prints 10
+       cout << miset.size() << endl;   // prints 20
+       
+       // returns an iterator that refers to the element with key == 1
+       iset.find(1);   
+       iset.find(11);  // returns the iterator == iset.end()
+       iset.count(1);  // returns 1
+       iset.count(11); // returns 0
+
+       // returns an iterator to the first element with key == 1
+       iset.find(1);   
+       iset.find(11);   // returns the iterator == iset.end()
+       miset.count(1);  // returns 2
+       miset.count(11); // returns 0
+       
+       set<string> set1;    // empty set
+       set1.insert("the");  // set1 now has one element
+       set1.insert("and");  // set1 now has two elements
+       
+       ivec = {2,4,6,8,2,4,6,8}; // ivec now has eight elements
+       set<int> set2;            // empty set
+       set2.insert(ivec.cbegin(), ivec.cend()); // set2 has four elements
+       set2.insert({1,3,5,7,1,3,5,7});      // set2 now has eight elements
+
+       return 0;
+}
diff --git a/11/unorderedWordCount.cc b/11/unorderedWordCount.cc
new file mode 100644 (file)
index 0000000..a37043d
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <unordered_map>
+using std::unordered_map;
+
+#include <unordered_set>
+using std::unordered_set; using std::unordered_multiset;
+
+#include <string>
+using std::string; 
+
+using std::hash;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <cstddef>
+using std::size_t;
+
+#include "Sales_data.h"
+
+// unordered_map version of the word count program
+int main() 
+{
+       // count occurrences, but the words won't be in alphabetical order
+    unordered_map<string, size_t> word_count;  
+    string word;
+    while (cin >> word)
+               ++word_count[word]; // fetch and increment the counter for word
+
+       for (const auto &w : word_count) // for each element in the map
+               // print the results
+               cout <<  w.first << " occurs " << w.second 
+                    << ((w.second > 1) ? " times" : " time") << endl;
+
+       return 0;
+}
+
+// how to override default hash and equality operator on key_type
+size_t hasher(const Sales_data &sd) 
+{
+       return hash<string>()(sd.isbn());
+}
+bool eqOp(const Sales_data &lhs, const Sales_data &rhs)
+{
+       return lhs.isbn() == rhs.isbn();
+}
+
+// type alias using our functions in place of hash<key_type> and ==
+using SD_multiset = unordered_multiset<Sales_data, 
+                    decltype(hasher)*, decltype(eqOp)*>;
+
+// bookstore can hold multiple Sales_data with the same ISBN
+// arguments are the bucket size 
+// and pointers to the hash function and equality operator
+SD_multiset bookstore(42, hasher, eqOp);
+
+// how to override just the hash function;
+// Foo must have ==
+struct Foo { string s; };
+
+// we'll see how to define our own operators in chapter 14
+bool operator==(const Foo& l, const Foo&r) { return l.s == r.s; }
+
+size_t FooHash(const Foo& f) { return hash<string>()(f.s); }
+
+// use FooHash to generate the hash code; Foo must have an == operator
+unordered_set<Foo, decltype(FooHash)*> fooSet(10, FooHash);
+
diff --git a/11/wcEx.cc b/11/wcEx.cc
new file mode 100644 (file)
index 0000000..f68f137
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <map>
+using std::map;
+
+#include <string>
+using std::string;
+
+#include <cstddef>
+using std::size_t;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main() 
+{
+    // count number of times each word occurs in the input
+    map<string, size_t> word_count; // empty map from string to size_t
+       string word;
+       while (cin >> word)
+      ++word_count.insert({word, 0}).first->second;
+
+       for(auto it = word_count.cbegin(); it != word_count.cend(); ++it) {
+               auto w = *it;
+               cout <<  w.first << " occurs " << w.second << " times" << endl;
+       }
+
+    // get iterator positioned on the first element
+    auto map_it = word_count.cbegin();
+    // for each element in the map
+    while (map_it != word_count.cend()) {
+        // print the element key, value pairs
+        cout << map_it->first << " occurs " 
+             << map_it->second << " times" << endl;
+        ++map_it;  // increment iterator to denote the next element
+    }
+
+       return 0;
+}
diff --git a/11/word_count.cc b/11/word_count.cc
new file mode 100644 (file)
index 0000000..94f697c
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <map>
+using std::map;
+
+#include <string>
+using std::string;
+
+#include <utility>
+using std::pair;
+
+#include <cstddef>
+using std::size_t;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main() 
+{
+    // count the number of times each word occurs in the input
+    map<string, size_t> word_count; // empty map from string to size_t
+    string word;
+    while (cin >> word)
+      ++word_count[word];
+
+       for (const auto &w : word_count)
+               cout <<  w.first << " occurs " << w.second << " times" << endl;
+
+    // get an iterator positioned on the first element
+    auto map_it = word_count.cbegin();
+    // compare the current iterator to the off-the-end iterator
+    while (map_it != word_count.cend()) {
+        // dereference the iterator to print the element key--value pairs
+        cout << map_it->first << " occurs " 
+             << map_it->second << " times" << endl;
+        ++map_it;  // increment the iterator to denote the next element
+    }
+
+       return 0;
+}
+
diff --git a/11/word_transform.cc b/11/word_transform.cc
new file mode 100644 (file)
index 0000000..a758191
--- /dev/null
@@ -0,0 +1,117 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <map>
+#include <vector>
+#include <iostream>
+#include <fstream>
+#include <string>
+#include <stdexcept>
+#include <sstream>
+
+using std::map; using std::string; using std::vector;
+using std::ifstream; using std::cout; using std::endl;
+using std::getline; 
+using std::runtime_error; using std::istringstream;
+
+map<string, string> buildMap(ifstream &map_file)
+{
+    map<string, string> trans_map;   // holds the transformations
+    string key;    // a word to transform
+       string value;  // phrase to use instead
+       // read the first word into key and the rest of the line into value
+       while (map_file >> key && getline(map_file, value))
+               if (value.size() > 1) // check that there is a transformation
+               trans_map[key] = value.substr(1); // skip leading space 
+               else
+                       throw runtime_error("no rule for " + key);
+       return trans_map;
+}
+
+const string &
+transform(const string &s, const map<string, string> &m)
+{
+       // the actual map work; this part is the heart of the program
+       auto map_it = m.find(s);
+       // if this word is in the transformation map
+       if (map_it != m.cend()) 
+               return map_it->second; // use the replacement word
+       else
+               return s;              // otherwise return the original unchanged
+}
+
+// first argument is the transformations file; 
+// second is file to transform
+void word_transform(ifstream &map_file, ifstream &input)
+{
+       auto trans_map = buildMap(map_file); // store the transformations
+
+       // for debugging purposes print the map after its built
+    cout << "Here is our transformation map: \n\n";
+       for (auto entry : trans_map)
+        cout << "key: "   << entry.first
+             << "\tvalue: " << entry.second << endl;
+    cout << "\n\n";
+
+       // do the transformation of the given text 
+    string text;                    // hold each line from the input
+    while (getline(input, text)) {  // read a line of input
+        istringstream stream(text); // read each word 
+        string word;
+        bool firstword = true;      // controls whether a space is printed 
+        while (stream >> word) {
+           if (firstword)
+               firstword = false;
+           else
+               cout << " ";  // print a space between words
+           // transform returns its first argument or its transformation 
+           cout << transform(word, trans_map); // print the output 
+        }
+        cout << endl;        // done with this line of input
+    }
+}
+
+int main(int argc, char **argv)
+{
+       // open and check both files
+    if (argc != 3)
+        throw runtime_error("wrong number of arguments");
+
+    ifstream map_file(argv[1]); // open transformation file 
+    if (!map_file)              // check that open succeeded
+        throw runtime_error("no transformation file");
+    
+    ifstream input(argv[2]);    // open file of text to transform
+    if (!input)                 // check that open succeeded
+        throw runtime_error("no input file");
+
+       word_transform(map_file, input);
+
+    return 0;  // exiting main will automatically close the files
+}
diff --git a/12/Foo.h b/12/Foo.h
new file mode 100644 (file)
index 0000000..16c5e7e
--- /dev/null
+++ b/12/Foo.h
@@ -0,0 +1,48 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef FOO_H
+#define FOO_H
+
+#include <iostream>
+
+typedef int T;
+struct Foo {  // members are public by default
+       Foo(T t): val(t) { }
+       T val;
+};
+
+std::ostream&
+print(std::ostream &os, const Foo &f) 
+{
+       os << f.val;
+       return os;
+}
+
+#endif
diff --git a/12/QueryResult.h b/12/QueryResult.h
new file mode 100644 (file)
index 0000000..b8b98f2
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef QUERYRESULT_H
+#define QUERYRESULT_H
+#include <memory>
+#include <string>
+#include <vector>
+#include <set>
+#include <iostream>
+
+class QueryResult {
+friend std::ostream& print(std::ostream&, const QueryResult&);
+public:
+       typedef std::vector<std::string>::size_type line_no;
+       typedef std::set<line_no>::const_iterator line_it;
+       QueryResult(std::string s, 
+                   std::shared_ptr<std::set<line_no>> p, 
+                   std::shared_ptr<std::vector<std::string>> f):
+               sought(s), lines(p), file(f) { }
+       std::set<line_no>::size_type size() const  { return lines->size(); }
+       line_it begin() const { return lines->cbegin(); }
+       line_it end() const   { return lines->cend(); }
+       std::shared_ptr<std::vector<std::string>> get_file() { return file; }
+private:
+       std::string sought;  // word this query represents
+       std::shared_ptr<std::set<line_no>> lines; // lines it's on
+       std::shared_ptr<std::vector<std::string>> file;  //input file
+};
+
+std::ostream &print(std::ostream&, const QueryResult&);
+#endif
diff --git a/12/README b/12/README
new file mode 100644 (file)
index 0000000..581ae8b
--- /dev/null
+++ b/12/README
@@ -0,0 +1,14 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   storyDatafile    usealloc
+   storyDatafile    querymain
+   Alice            querymain
+   alloc            allocPtr  
+   alloc            allocSP
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/12/StrBlob.h b/12/StrBlob.h
new file mode 100644 (file)
index 0000000..414c0fe
--- /dev/null
@@ -0,0 +1,170 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced. Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/
+
+#ifndef STRBLOB_H
+#define STRBLOB_H
+#include <vector>
+#include <string>
+#include <initializer_list>
+#include <memory>
+#include <stdexcept>
+
+// forward declaration needed for friend declaration in StrBlob
+class StrBlobPtr;
+
+class StrBlob {
+       friend class StrBlobPtr;
+public:
+    typedef std::vector<std::string>::size_type size_type;
+
+       // constructors
+    StrBlob() : data(std::make_shared<std::vector<std::string>>()) { }
+    StrBlob(std::initializer_list<std::string> il);
+
+       // size operations
+    size_type size() const { return data->size(); }
+    bool empty() const { return data->empty(); }
+
+    // add and remove elements
+    void push_back(const std::string &t) { data->push_back(t); }
+    void pop_back();
+
+    // element access
+    std::string& front();
+    std::string& back();
+
+       // interface to StrBlobPtr
+       StrBlobPtr begin();  // can't be defined until StrBlobPtr is
+    StrBlobPtr end();
+private:
+    std::shared_ptr<std::vector<std::string>> data; 
+    // throws msg if data[i] isn't valid
+    void check(size_type i, const std::string &msg) const;
+};
+
+// constructor
+inline
+StrBlob::StrBlob(std::initializer_list<std::string> il): 
+              data(std::make_shared<std::vector<std::string>>(il)) { }
+
+// StrBlobPtr throws an exception on attempts to access a nonexistent element 
+class StrBlobPtr {
+       friend bool eq(const StrBlobPtr&, const StrBlobPtr&);
+public:
+    StrBlobPtr(): curr(0) { }
+    StrBlobPtr(StrBlob &a, size_t sz = 0): wptr(a.data), curr(sz) { }
+
+    std::string& deref() const;
+    StrBlobPtr& incr();       // prefix version
+    StrBlobPtr& decr();       // prefix version
+private:
+    // check returns a shared_ptr to the vector if the check succeeds
+    std::shared_ptr<std::vector<std::string>> 
+        check(std::size_t, const std::string&) const;
+
+    // store a weak_ptr, which means the underlying vector might be destroyed
+    std::weak_ptr<std::vector<std::string>> wptr;  
+    std::size_t curr;      // current position within the array
+};
+
+inline
+std::string& StrBlobPtr::deref() const
+{
+    auto p = check(curr, "dereference past end"); 
+    return (*p)[curr];  // (*p) is the vector to which this object points
+}
+
+inline
+std::shared_ptr<std::vector<std::string>> 
+StrBlobPtr::check(std::size_t i, const std::string &msg) const
+{
+    auto ret = wptr.lock();   // is the vector still around?
+    if (!ret)
+        throw std::runtime_error("unbound StrBlobPtr");
+
+    if (i >= ret->size()) 
+        throw std::out_of_range(msg);
+    return ret; // otherwise, return a shared_ptr to the vector
+}
+
+// prefix: return a reference to the incremented object
+inline
+StrBlobPtr& StrBlobPtr::incr()
+{
+    // if curr already points past the end of the container, can't increment it
+    check(curr, "increment past end of StrBlobPtr");
+    ++curr;       // advance the current state
+    return *this;
+}
+
+inline
+StrBlobPtr& StrBlobPtr::decr()
+{
+    // if curr is zero, decrementing it will yield an invalid subscript
+    --curr;       // move the current state back one element}
+    check(-1, "decrement past begin of StrBlobPtr");
+    return *this;
+}
+
+// begin and end members for StrBlob
+inline
+StrBlobPtr
+StrBlob::begin() 
+{
+       return StrBlobPtr(*this);
+}
+
+inline
+StrBlobPtr
+StrBlob::end() 
+{
+       auto ret = StrBlobPtr(*this, data->size());
+    return ret; 
+}
+
+// named equality operators for StrBlobPtr
+inline
+bool eq(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
+{
+       auto l = lhs.wptr.lock(), r = rhs.wptr.lock();
+       // if the underlying vector is the same 
+       if (l == r) 
+               // then they're equal if they're both null or 
+               // if they point to the same element
+               return (!r || lhs.curr == rhs.curr);
+       else
+               return false; // if they point to difference vectors, they're not equal
+}
+
+inline
+bool neq(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
+{
+       return !eq(lhs, rhs); 
+}
+#endif
diff --git a/12/TextQuery.cc b/12/TextQuery.cc
new file mode 100644 (file)
index 0000000..70feeb8
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "TextQuery.h"
+#include "make_plural.h"
+
+#include <cstddef>
+#include <memory>
+#include <sstream>
+#include <string>
+#include <vector>
+#include <map>
+#include <set>
+#include <iostream>
+#include <fstream>
+#include <cctype>
+#include <cstring>
+#include <utility>
+
+using std::size_t;
+using std::shared_ptr;
+using std::istringstream;
+using std::string;
+using std::getline;
+using std::vector;
+using std::map;
+using std::set;
+using std::cerr;
+using std::cout;
+using std::cin;
+using std::ostream;
+using std::endl;
+using std::ifstream;
+using std::ispunct;
+using std::tolower;
+using std::strlen;
+using std::pair;
+
+// read the input file and build the map of lines to line numbers
+TextQuery::TextQuery(ifstream &is): file(new vector<string>)
+{
+    string text;
+    while (getline(is, text)) {       // for each line in the file
+               file->push_back(text);        // remember this line of text
+               int n = file->size() - 1;     // the current line number
+               istringstream line(text);     // separate the line into words
+               string word;               
+               while (line >> word) {        // for each word in that line
+            word = cleanup_str(word);
+            // if word isn't already in wm, subscripting adds a new entry
+            auto &lines = wm[word]; // lines is a shared_ptr 
+            if (!lines) // that pointer is null the first time we see word
+                lines.reset(new set<line_no>); // allocate a new set
+            lines->insert(n);      // insert this line number
+               }
+       }
+}
+
+// not covered in the book -- cleanup_str removes
+// punctuation and converts all text to lowercase so that
+// the queries operate in a case insensitive manner
+string TextQuery::cleanup_str(const string &word)
+{
+    string ret;
+    for (auto it = word.begin(); it != word.end(); ++it) {
+        if (!ispunct(*it))
+            ret += tolower(*it);
+    }
+    return ret;
+}
+
+QueryResult
+TextQuery::query(const string &sought) const
+{
+       // we'll return a pointer to this set if we don't find sought
+       static shared_ptr<set<line_no>> nodata(new set<line_no>); 
+
+    // use find and not a subscript to avoid adding words to wm!
+    auto loc = wm.find(cleanup_str(sought));
+
+       if (loc == wm.end()) 
+               return QueryResult(sought, nodata, file);  // not found
+       else 
+               return QueryResult(sought, loc->second, file);
+}
+
+ostream &print(ostream & os, const QueryResult &qr)
+{
+    // if the word was found, print the count and all occurrences
+    os << qr.sought << " occurs " << qr.lines->size() << " "
+       << make_plural(qr.lines->size(), "time", "s") << endl;
+
+    // print each line in which the word appeared
+       for (auto num : *qr.lines) // for every element in the set 
+               // don't confound the user with text lines starting at 0
+        os << "\t(line " << num + 1 << ") " 
+                  << *(qr.file->begin() + num) << endl;
+
+       return os;
+}
+
+// debugging routine, not covered in the book
+void TextQuery::display_map()
+{
+    auto iter = wm.cbegin(), iter_end = wm.cend();
+
+    // for each word in the map
+    for ( ; iter != iter_end; ++iter) {
+        cout << "word: " << iter->first << " {";
+
+        // fetch location vector as a const reference to avoid copying it
+        auto text_locs = iter->second;
+        auto loc_iter = text_locs->cbegin(),
+                        loc_iter_end = text_locs->cend();
+
+        // print all line numbers for this word
+        while (loc_iter != loc_iter_end)
+        {
+            cout << *loc_iter;
+
+            if (++loc_iter != loc_iter_end)
+                 cout << ", ";
+
+         }
+
+         cout << "}\n";  // end list of output this word
+    }
+    cout << endl;  // finished printing entire map
+}
+
diff --git a/12/TextQuery.h b/12/TextQuery.h
new file mode 100644 (file)
index 0000000..f1957e2
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef TEXTQUERY_H
+#define TEXTQUERY_H
+#include <memory>
+#include <string>
+#include <vector>
+#include <map>
+#include <set>
+#include <fstream>
+#include "QueryResult.h"
+
+/* this version of the query classes includes two
+ * members not covered in the book:  
+ *   cleanup_str: which removes punctuation and 
+ *                converst all text to lowercase
+ *   display_map: a debugging routine that will print the contents
+ *                of the lookup mape
+*/
+
+class QueryResult; // declaration needed for return type in the query function
+class TextQuery {
+public:
+       using line_no = std::vector<std::string>::size_type;
+       TextQuery(std::ifstream&);
+    QueryResult query(const std::string&) const; 
+    void display_map();        // debugging aid: print the map
+private:
+    std::shared_ptr<std::vector<std::string>> file; // input file
+    // maps each word to the set of the lines in which that word appears
+    std::map<std::string, 
+                std::shared_ptr<std::set<line_no>>> wm;  
+
+       // canonicalizes text: removes punctuation and makes everything lower case
+    static std::string cleanup_str(const std::string&);
+};
+#endif
diff --git a/12/UP.cc b/12/UP.cc
new file mode 100644 (file)
index 0000000..ccd879a
--- /dev/null
+++ b/12/UP.cc
@@ -0,0 +1,50 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <memory>
+using std::unique_ptr; using std::shared_ptr;
+
+int main()
+{
+       // up points to an array of ten uninitialized ints
+       unique_ptr<int[]> up(new int[10]);
+       for (size_t i = 0; i != 10; ++i)
+               up[i] = i;  // assign a new value to each of the elements
+       up.release();   // automatically uses delete[] to destroy its pointer
+       
+       // to use a shared_ptr we must supply a deleter
+       shared_ptr<int> sp(new int[10], [](int *p) { delete[] p; });
+       // shared_ptrs don't have subscript operator 
+       // and don't support pointer arithmetic
+       for (size_t i = 0; i != 10; ++i)
+               *(sp.get() + i) = i;  // use get to get a built-in pointer
+       sp.reset(); // uses the lambda we supplied 
+                   // that uses delete[] to free the array
+}
+       
diff --git a/12/allocPtr.cc b/12/allocPtr.cc
new file mode 100644 (file)
index 0000000..62f2b1d
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::istream; using std::ostream; 
+using std::cin; using std::cout; using std::endl;
+
+#include "Foo.h"
+
+// factory returns a pointer to a dynamically allocated object 
+Foo* factory(T arg)
+{
+       // process arg as appropriate
+       return new Foo(arg); // caller is responsible for deleting this memory
+}
+
+Foo* use_factory(T arg)
+{
+       Foo *p = factory(arg);
+       print(cout, *p);
+       cout << endl;
+       // use p
+       return p;  // caller must delete the memory
+} 
+
+int main()
+{
+       T arg;
+       while (cin >> arg) {
+               auto p = use_factory(arg);
+               delete p; // remember to delete the memory from use_factory
+       }
+       
+       return 0;
+}
diff --git a/12/allocSP.cc b/12/allocSP.cc
new file mode 100644 (file)
index 0000000..3b267c7
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+#include <memory>
+using std::make_shared; using std::shared_ptr;
+
+#include <iostream>
+using std::istream; using std::ostream; 
+using std::cin; using std::cout; using std::endl;
+
+#include "Foo.h"
+
+// factory returns a shared_ptr to a dynamically allocated object 
+shared_ptr<Foo> factory(T arg)
+{
+       // process arg as appropriate
+       // shared_ptr will take care of deleting this memory
+       return make_shared<Foo>(arg); 
+}
+
+shared_ptr<Foo> use_factory(T arg)
+{
+       shared_ptr<Foo> p = factory(arg);
+       print(cout, *p);
+       cout << endl;
+       // use p
+       return p;  // reference count is incremented when we return p
+} 
+
+int main()
+{
+       T arg;
+       while (cin >> arg)
+               use_factory(arg);
+}
+
diff --git a/12/data/Alice b/12/data/Alice
new file mode 100644 (file)
index 0000000..08b6485
--- /dev/null
@@ -0,0 +1 @@
+Alice
diff --git a/12/data/alloc b/12/data/alloc
new file mode 100644 (file)
index 0000000..95a4bfe
--- /dev/null
@@ -0,0 +1 @@
+1 2 3 4 5 6 7 8 9
diff --git a/12/data/storyDataFile b/12/data/storyDataFile
new file mode 100644 (file)
index 0000000..1f06b36
--- /dev/null
@@ -0,0 +1,10 @@
+Alice Emma has long flowing red hair. 
+Her Daddy says when the wind blows 
+through her hair, it looks almost alive, 
+like a fiery bird in flight. 
+A beautiful fiery bird, he tells her, 
+magical but untamed. 
+"Daddy, shush, there is no such thing," 
+she tells him, at the same time wanting 
+him to tell her more.
+Shyly, she asks, "I mean, Daddy, is there?"
diff --git a/12/makefile b/12/makefile
new file mode 100644 (file)
index 0000000..71db9c1
--- /dev/null
@@ -0,0 +1,17 @@
+# executable files for this directory
+OBJECTS = allocPtr.exe allocSP.exe \
+                 oknew.exe querymain.exe release.exe \
+                 unique.exe UP.exe usealloc.exe usealloc2.exe \
+                 useBlob.exe weak.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+# 7 has Sales_data files, 6 has make_plural
+LOCFLAGS = -I../7 -I../6
+
+querymain.exe: querymain.o TextQuery.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) querymain.o TextQuery.o \
+                       -o querymain.exe
+
diff --git a/12/oknew.cc b/12/oknew.cc
new file mode 100644 (file)
index 0000000..8ef4f8d
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <cstddef>
+using std::size_t;
+
+#include <new>
+using std::nothrow;
+
+size_t get_size() { return 42; }
+
+int main() 
+{
+       // if allocation fails, new returns a null pointer
+       int *p1 = new int; // if allocation fails, new throws std::bad_alloc
+       int *p2 = new (nothrow) int; // if allocation fails, new returns a null pointer
+       
+       int i0;                  // named, uninitialized int variable
+
+       int *p0 = new int;       // pi points to a dynamically allocated, 
+                                // unnamed, uninitialized int 
+       delete p0;  // frees the memory to which pi points
+
+       // named initialized variables
+       int i(1024);             // value of i is 1024
+       string s(10, '9');       // value of s is "9999999999"
+       vector<int> v = {0,1,2,3,4,5,6,7,8,9};
+
+       // unnamed, initialized dynamically allocated objects
+       int *pi = new int(1024); // object to which pi points has value 1024
+       string *ps = new string(10, '9');   // *ps is "9999999999"
+       // vector with ten elements with values from 0 to 9
+       vector<int> *pv = new vector<int>{0,1,2,3,4,5,6,7,8,9};
+       
+       cout << "*pi: " << *pi
+            << "\ti: " << i << endl
+            << "*ps: " << *ps
+            << "\ts: " << s << endl;
+       
+       for (auto b = pv->begin(); b != pv->end(); ++b)
+               cout << *b << " ";
+       cout << endl;
+
+       // when we're done using the memory must delete the pointers
+       delete pi;  // frees the memory to which pi points
+       delete ps;  // frees the string to which ps points
+                   // the string destructor frees the space used by its data
+       delete pv;  // frees the memory for the vector
+                   // which also destroys the elements in that vector
+       
+       // call get_size to determine how many ints to allocate
+       int *pia = new int[get_size()]; // pia points to the first of these ints
+       delete [] pia; // brackets used to delete pointer to element in an array
+       typedef int arrT[42];  // arrT names the type array of 42 ints
+       int *p = new arrT;     // allocates an array of 42 ints; p points to the first one
+       delete [] p;           // brackets are necessary because we allocated an array 
+
+       return 0;
+}
diff --git a/12/querymain.cc b/12/querymain.cc
new file mode 100644 (file)
index 0000000..fd4a6d7
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <fstream>
+using std::ifstream;
+
+#include <iostream>
+using std::cin; using std::cout; using std::cerr;
+using std::endl;
+
+#include <cstdlib>  // for EXIT_FAILURE
+
+#include "TextQuery.h"
+#include "make_plural.h"
+
+void runQueries(ifstream &infile)
+{
+       // infile is an ifstream that is the file we want to query
+    TextQuery tq(infile);  // store the file and build the query map
+    // iterate with the user: prompt for a word to find and print results
+    while (true) {
+        cout << "enter word to look for, or q to quit: ";
+        string s;
+        // stop if we hit end-of-file on the input or if a 'q' is entered
+        if (!(cin >> s) || s == "q") break;
+               // run the query and print the results
+        print(cout, tq.query(s)) << endl;
+    }
+}
+
+// program takes single argument specifying the file to query
+int main(int argc, char **argv)
+{
+    // open the file from which user will query words
+    ifstream infile;
+       // open returns void, so we use the comma operator XREF(commaOp) 
+       // to check the state of infile after the open
+    if (argc < 2 || !(infile.open(argv[1]), infile)) {
+        cerr << "No input file!" << endl;
+        return EXIT_FAILURE;
+    }
+       runQueries(infile);
+    return 0;
+}
diff --git a/12/release.cc b/12/release.cc
new file mode 100644 (file)
index 0000000..636f03e
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <memory>
+using std::unique_ptr;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       unique_ptr<string> p1(new string("Stegosaurus"));
+       
+       // transfers ownership from p1 
+       // (which points to the string Stegosaurus) to p2
+       unique_ptr<string> p2(p1.release()); // release makes p1 null
+       cout << *p2 << endl; // prints Stegosaurus
+       
+       unique_ptr<string> p3(new string("Trex"));
+
+       // reset deletes the memory to which p2 had pointed
+       // and transfers ownership from p3 to p2 
+       p2.reset(p3.release()); 
+       cout << *p2 << endl; // prints Trex
+       
+       // p1 is null, p2 is steg, and p3 is trex
+       if (p1)
+               cout << "p1 not null?" << endl;
+       if (p3)
+               cout << "p2 not null?" << endl;
+       cout << *p2 << endl;
+}
diff --git a/12/runpgms b/12/runpgms
new file mode 100644 (file)
index 0000000..2612892
--- /dev/null
@@ -0,0 +1,7 @@
+echo "querymain:" && ./querymain.exe data/storyDataFile < data/Alice
+echo "allocPtr:" && ./allocPtr.exe < data/alloc
+echo "allocSP:" && ./allocSP.exe < data/alloc
+echo "usealloc:" && ./usealloc.exe < data/storyDataFile
+
+../noinput oknew.exe release.exe \
+                  unique.exe UP.exe usealloc2.exe useBlob.exe weak.exe
diff --git a/12/unique.cc b/12/unique.cc
new file mode 100644 (file)
index 0000000..fb7d0f4
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::ostream; using std::cin; using std::cout; using std::endl;
+
+#include <memory>
+using std::shared_ptr;
+
+int main() {
+       shared_ptr<string> p(new string("Hello!"));
+       shared_ptr<string> p2(p);    // two users of the allocated string
+       string newVal;
+       if (!p.unique())
+               p.reset(new string(*p)); // we aren't alone; allocate a new copy
+       *p += newVal; // now that we know we're the only pointer, okay to change this object 
+       cout << *p << " " << *p2 << endl;
+}
diff --git a/12/useBlob.cc b/12/useBlob.cc
new file mode 100644 (file)
index 0000000..246800d
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced. Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/
+#include <iostream>
+using std::cout; using std::endl;
+
+#include "StrBlob.h"
+
+int main()
+{
+       StrBlob b1; 
+       {
+           StrBlob b2 = { "a", "an", "the" };
+           b1 = b2;  
+           b2.push_back("about");
+               cout << b2.size() << endl;
+       }
+       cout << b1.size() << endl;
+
+       for (auto it = b1.begin(); neq(it, b1.end()); it.incr())
+               cout << it.deref() << endl;
+
+       return 0;
+}
diff --git a/12/usealloc.cc b/12/usealloc.cc
new file mode 100644 (file)
index 0000000..7bf9d3f
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string; 
+
+#include <memory>
+using std::allocator;
+
+#include <cstddef>
+using std::size_t; 
+
+#include <iostream>
+using std::cout; using std::endl; 
+
+#include <fstream>
+using std::ifstream;
+
+int main() 
+{
+       const size_t n = 100;
+       allocator<string> alloc;      // object that can allocate strings
+       auto p = alloc.allocate(n);   // allocate n unconstructed strings
+       
+       auto q = p; // q will point to one past the last constructed element
+       alloc.construct(q++);         // *q is the empty string
+       cout << *(q-1) << endl;
+       
+       alloc.construct(q++, 10, 'c'); // *q is cccccccccc
+       cout << *(q - 1) << endl;
+       
+       alloc.construct(q++, "hi");    // *q is hi!
+       cout << *(q - 1) << endl;
+       
+       cout << *p << endl;  // ok: uses the string output operator
+       while (q != p)
+               alloc.destroy(--q);  // free the strings we actually allocated
+       
+       alloc.deallocate(p, n);  // return the memory we allocated
+       
+       p = alloc.allocate(n);   // allocate n unconstructed strings
+       string s;
+       q = p;                   // q points to the memory for first string
+       ifstream in("data/storyDataFile");
+       while (in >> s && q != p + n)
+               alloc.construct(q++, s); // construct only as many strings as we need
+       size_t size = q - p;         // remember how many strings we read
+
+       // use the array
+
+       cout << "read " << size << " strings" << endl;
+       
+       for (q = p + size - 1; q != p; --q)
+               alloc.destroy(q);         // free the strings we allocated
+       alloc.deallocate(p, n);       // return the memory we allocated
+
+       in.close();
+       in.open("data/storyDataFile");
+       p = new string[n];            // construct n empty strings
+       q = p;                        // q points to the first string
+       while (in >> s && q != p + n)
+               *q++ = s;                 // assign a new value to *q
+       size = q - p;                 // remember how many strings we read
+
+       cout << "read " << size << " strings" << endl;
+
+       // use the array
+
+       delete[] p;  // p points to an array; must remember to use delete[]
+}
diff --git a/12/usealloc2.cc b/12/usealloc2.cc
new file mode 100644 (file)
index 0000000..592b3d5
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <string>
+using std::string; 
+
+#include <vector>
+using std::vector; 
+
+#include <memory>
+using std::uninitialized_copy;
+using std::allocator; using std::uninitialized_fill_n;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       vector<int> vi{1,2,3,4,5,6,7,8,9};
+
+       allocator<int> alloc;
+       // allocate twice as many elements as vi holds
+       auto p = alloc.allocate(vi.size() * 2);
+
+       // construct elements starting at p as copies of elements in vi
+       auto q = uninitialized_copy(vi.begin(), vi.end(), p); 
+
+       // initialize the remaining elements to 42
+       uninitialized_fill_n(q, vi.size(), 42);
+
+       for (size_t i = 0; i != vi.size(); ++i)
+               cout << *(p + i) << " ";
+       cout << endl;
+
+       for (size_t i = 0; i != vi.size(); ++i)
+               cout << *(q + i) << " ";
+       cout << endl;
+
+       alloc.deallocate(p, vi.size());
+}
diff --git a/12/weak.cc b/12/weak.cc
new file mode 100644 (file)
index 0000000..c9aed14
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <memory>
+using std::make_shared; using std::weak_ptr; using std::shared_ptr;
+
+int main()
+{
+       auto p = make_shared<int>(42);
+
+       weak_ptr<int> wp(p);  // wp weakly shares with p; use count in p is unchanged
+
+       p.reset(); // assuming p.unique() was true, the int is deleted
+
+       if (shared_ptr<int> np = wp.lock()) { // true if np is not null
+               // inside the if, np shares its object with p
+       }
+}
diff --git a/13/Folder.cc b/13/Folder.cc
new file mode 100644 (file)
index 0000000..e383e09
--- /dev/null
@@ -0,0 +1,221 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <utility>   
+// for move, we don't supply a using declaration for move
+
+#include <iostream>
+using std::cerr; using std::endl;
+
+#include <set>
+using std::set; 
+
+#include <string>
+using std::string; 
+
+#include "Folder.h"
+
+void swap(Message &lhs, Message &rhs)
+{
+       using std::swap;  // not strictly needed in this case, but good habit
+
+       // remove pointers to each Message from their (original) respective Folders
+       for (auto f: lhs.folders) 
+               f->remMsg(&lhs);
+       for (auto f: rhs.folders) 
+               f->remMsg(&rhs);
+
+       // swap the contents and Folder pointer sets
+       swap(lhs.folders, rhs.folders);   // uses swap(set&, set&)
+       swap(lhs.contents, rhs.contents); // swap(string&, string&)
+
+       // add pointers to each Message to their (new) respective Folders
+       for (auto f: lhs.folders) 
+               f->addMsg(&lhs);
+       for (auto f: rhs.folders) 
+               f->addMsg(&rhs);
+}
+
+Folder::Folder(Folder &&f)
+{
+       move_Messages(&f);   // make each Message point to this Folder
+}
+
+Folder& Folder::operator=(Folder &&f) 
+{
+       if (this != &f) {
+               remove_from_Msgs();  // remove this Folder from the current msgs
+               move_Messages(&f);   // make each Message point to this Folder
+       }
+       return *this;
+}
+
+void Folder::move_Messages(Folder *f)
+{
+       msgs = std::move(f->msgs); // move the set from f to this Folder
+       f->msgs.clear(); // ensure that destroying f is harmless
+       for (auto m : msgs) {  // for each Message in this Folder
+               m->remFldr(f);     // remove the pointer to the old Folder
+               m->addFldr(this);  // insert pointer to this Folder
+       }
+}
+
+Message::Message(Message &&m): contents(std::move(m.contents))
+{
+       move_Folders(&m); // moves folders and updates the Folder pointers
+}
+
+Message::Message(const Message &m): 
+    contents(m.contents), folders(m.folders) 
+{
+    add_to_Folders(m); // add this Message to the Folders that point to m
+}
+
+Message& Message::operator=(Message &&rhs) 
+{
+       if (this != &rhs) {       // direct check for self-assignment
+               remove_from_Folders();
+               contents = std::move(rhs.contents); // move assignment
+               move_Folders(&rhs); // reset the Folders to point to this Message
+       }
+    return *this;
+}
+
+Message& Message::operator=(const Message &rhs)
+{
+       // handle self-assignment by removing pointers before inserting them
+    remove_from_Folders();    // update existing Folders
+    contents = rhs.contents;  // copy message contents from rhs
+    folders = rhs.folders;    // copy Folder pointers from rhs
+    add_to_Folders(rhs);      // add this Message to those Folders
+    return *this;
+}
+
+Message::~Message()
+{
+    remove_from_Folders();
+}
+
+// move the Folder pointers from m to this Message
+void Message::move_Folders(Message *m)
+{
+       folders = std::move(m->folders); // uses set move assignment
+       for (auto f : folders) {  // for each Folder 
+               f->remMsg(m);    // remove the old Message from the Folder
+               f->addMsg(this); // add this Message to that Folder
+       }
+       m->folders.clear();  // ensure that destroying m is harmless
+}
+
+// add this Message to Folders that point to m
+void Message::add_to_Folders(const Message &m)
+{
+       for (auto f : m.folders) // for each Folder that holds m
+        f->addMsg(this); // add a pointer to this Message to that Folder
+}
+
+// remove this Message from the corresponding Folders 
+void Message::remove_from_Folders()
+{
+       for (auto f : folders)  // for each pointer in folders
+               f->remMsg(this);    // remove this Message from that Folder
+       folders.clear();        // no Folder points to this Message
+
+}
+
+void Folder::add_to_Messages(const Folder &f)
+{
+       for (auto msg : f.msgs)
+               msg->addFldr(this);   // add this Folder to each Message
+}
+
+Folder::Folder(const Folder &f) : msgs(f.msgs)
+{
+    add_to_Messages(f);  // add this Folder to each Message in f.msgs
+}
+
+Folder& Folder::operator=(const Folder &f)
+{
+    remove_from_Msgs();  // remove this folder from each Message in msgs
+       msgs = f.msgs;       // copy the set of Messages from f
+    add_to_Messages(f);  // add this folder to each Message in msgs
+    return *this;
+}
+
+Folder::~Folder()
+{
+    remove_from_Msgs();
+}
+
+
+void Folder::remove_from_Msgs()
+{
+    while (!msgs.empty()) 
+        (*msgs.begin())->remove(*this);
+}
+void Message::save(Folder &f)
+{
+    folders.insert(&f); // add the given Folder to our list of Folders
+    f.addMsg(this);     // add this Message to f's set of Messages
+}
+
+void Message::remove(Folder &f)
+{
+    folders.erase(&f); // take the given Folder out of our list of Folders
+    f.remMsg(this);    // remove this Message to f's set of Messages
+}
+
+void Folder::save(Message &m)
+{
+    // add m and add this folder to m's set of Folders
+    msgs.insert(&m);
+    m.addFldr(this);
+}
+
+void Folder::remove(Message &m)
+{
+    // erase m from msgs and remove this folder from m
+    msgs.erase(&m);
+    m.remFldr(this);
+}
+
+void Folder::debug_print()
+{
+    cerr << "Folder contains " << msgs.size() << " messages" << endl;
+    int ctr = 1;
+    for (auto m : msgs) {
+        cerr << "Message " << ctr++ << ":\n\t" << m->contents << endl;
+       }
+}
+
+void Message::debug_print()
+{
+    cerr << "Message:\n\t" << contents << endl;
+    cerr << "Appears in " << folders.size() << " Folders" << endl;
+}
diff --git a/13/Folder.h b/13/Folder.h
new file mode 100644 (file)
index 0000000..f27a37e
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef FOLDER_H
+#define FOLDER_H
+
+#include <string>
+#include <set>
+
+class Folder;
+
+class Message {
+       friend void swap(Message&, Message&);
+       friend class Folder;
+public:
+    // folders is implicitly initialized to the empty set 
+    explicit Message(const std::string &str = ""): 
+               contents(str) { }  
+
+    // copy control to manage pointers to this Message
+    Message(const Message&);             // copy constructor
+    Message& operator=(const Message&);  // copy assignment
+    ~Message();                          // destructor
+    Message(Message&&);            // move constructor
+    Message& operator=(Message&&); // move assignment
+
+    // add/remove this Message from the specified Folder's set of messages
+    void save(Folder&);   
+    void remove(Folder&); 
+    void debug_print(); // print contents and it's list of Folders, 
+                        // printing each Folder as well
+private:
+    std::string contents;      // actual message text
+    std::set<Folder*> folders; // Folders that have this Message
+
+    // utility functions used by copy constructor, assignment, and destructor
+    // add this Message to the Folders that point to the parameter
+    void add_to_Folders(const Message&);
+       void move_Folders(Message*);
+    // remove this Message from every Folder in folders
+    void remove_from_Folders(); 
+
+    // used by Folder class to add self to this Message's set of Folder's
+    void addFldr(Folder *f) { folders.insert(f); }
+    void remFldr(Folder *f) { folders.erase(f); }
+};
+// declaration for swap should be in the same header as Message itself
+void swap(Message&, Message&);
+
+class Folder {
+       friend void swap(Message&, Message&);
+       friend class Message;
+public:
+    ~Folder(); // remove self from Messages in msgs
+    Folder(const Folder&); // add new folder to each Message in msgs
+    Folder& operator=(const Folder&); // delete Folder from lhs messages
+                                      // add Folder to rhs messages
+    Folder(Folder&&);   // move Messages to this Folder 
+    Folder& operator=(Folder&&); // delete Folder from lhs messages
+                                 // add Folder to rhs messages
+
+    Folder() = default; // defaults ok
+
+    void save(Message&);   // add this message to folder
+    void remove(Message&); // remove this message from this folder
+    
+    void debug_print(); // print contents and it's list of Folders, 
+private:
+    std::set<Message*> msgs;  // messages in this folder
+
+    void add_to_Messages(const Folder&);// add this Folder to each Message
+    void remove_from_Msgs();     // remove this Folder from each Message
+    void addMsg(Message *m) { msgs.insert(m); }
+    void remMsg(Message *m) { msgs.erase(m); }
+       void move_Messages(Folder*); // move Message pointers to point to this Folder
+};
+
+#endif
diff --git a/13/FolderMain.cc b/13/FolderMain.cc
new file mode 100644 (file)
index 0000000..a621acd
--- /dev/null
@@ -0,0 +1,133 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string; 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include "Folder.h"
+
+int main()
+{
+       string s1("contents1");
+       string s2("contents2");
+       string s3("contents3");
+       string s4("contents4");
+       string s5("contents5");
+       string s6("contents6");
+       
+       // all new messages, no copies yet
+       Message m1(s1);
+       Message m2(s2);
+       Message m3(s3);
+       Message m4(s4);
+       Message m5(s5);
+       Message m6(s6);
+
+       Folder f1;
+       Folder f2;
+
+       m1.save(f1); m3.save(f1); m5.save(f1);
+       m1.save(f2);
+       m2.save(f2); m4.save(f2); m6.save(f2);
+       
+       m1.debug_print();
+       f2.debug_print();
+
+       // create some copies
+       Message c1(m1);
+       Message c2(m2), c4(m4), c6(m6);
+       
+       m1.debug_print();
+       f2.debug_print();
+
+       // now some assignments
+       m2 = m3;
+       m4 = m5;
+       m6 = m3;
+       m1 = m5;
+
+       m1.debug_print();
+       f2.debug_print();
+
+       // finally, self-assignment
+       m2 = m2;
+       m1 = m1;
+
+       m1.debug_print();
+       f2.debug_print();
+
+       vector<Message> vm;
+       cout << "capacity: " << vm.capacity() << endl;
+       vm.push_back(m1);
+
+       cout << "capacity: " << vm.capacity() << endl;
+       vm.push_back(m2);
+
+       cout << "capacity: " << vm.capacity() << endl;
+       vm.push_back(m3);
+
+       cout << "capacity: " << vm.capacity() << endl;
+       vm.push_back(m4);
+
+       cout << "capacity: " << vm.capacity() << endl;
+       vm.push_back(m5);
+
+       cout << "capacity: " << vm.capacity() << endl;
+       vm.push_back(m6);
+
+       vector<Folder> vf;
+       cout << "capacity: " << vf.capacity() << endl;
+       vf.push_back(f1);
+
+       cout << "capacity: " << vf.capacity() << endl;
+       vf.push_back(f2);
+
+       cout << "capacity: " << vf.capacity() << endl;
+       vf.push_back(Folder(f1));
+
+       cout << "capacity: " << vf.capacity() << endl;
+       vf.push_back(Folder(f2));
+
+       cout << "capacity: " << vf.capacity() << endl;
+       vf.push_back(Folder());
+
+       Folder f3;
+       f3.save(m6);
+       cout << "capacity: " << vf.capacity() << endl;
+       vf.push_back(f3);
+
+
+       return 0;
+}
diff --git a/13/HasPtrUseCount.cc b/13/HasPtrUseCount.cc
new file mode 100644 (file)
index 0000000..494c186
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+// reference counted version of HasPtr
+#include <string>
+
+#include <cstddef>
+
+class HasPtr {
+public:
+       // constructor allocates a new string and a new counter, 
+       // which it sets to 1
+    HasPtr(const std::string &s = std::string()): 
+         ps(new std::string(s)), i(0), use(new std::size_t(1)) {}
+
+       // copy constructor copies all three data members 
+       // and increments the counter
+    HasPtr(const HasPtr &p): 
+               ps(p.ps), i(p.i), use(p.use) { ++*use; }
+
+       HasPtr& operator=(const HasPtr&);
+
+       ~HasPtr();
+
+       // move constructor takes over the pointers from its argument
+       // and makes the argument safe to delete
+       HasPtr(HasPtr &&p): ps(p.ps), i(p.i), use(p.use) 
+               { p.ps = 0; p.use = 0; }
+
+       HasPtr &operator=(HasPtr&&);
+
+private:
+    std::string *ps;
+    int    i;
+       std::size_t *use;  // member to track how many objects share *ps
+};
+
+HasPtr::~HasPtr()
+{
+       if (--*use == 0) {   // if the reference count goes to 0
+               delete ps;       // delete the string
+               delete use;      // and the counter
+       }
+}
+
+HasPtr &
+HasPtr::operator=(HasPtr &&rhs)
+{
+       if (this != &rhs) {
+               if (--*use == 0) {   // do the work of the destructor
+                       delete ps;
+                       delete use;
+               }
+               ps = rhs.ps;         // do the work of the move constructor
+               i = rhs.i;
+               use = rhs.use;
+               ps = 0; use = 0;
+       }
+       return *this;
+}
+
+HasPtr& HasPtr::operator=(const HasPtr &rhs) 
+{
+       ++*rhs.use;  // increment the use count of the right-hand operand
+       if (--*use == 0) {   // then decrement this object's counter
+               delete ps;       // if no other users 
+               delete use;      // free this object's allocated members
+       }
+       ps = rhs.ps;         // copy data from rhs into this object
+       i = rhs.i;
+       use = rhs.use; 
+       return *this;        // return this object
+}
+
+HasPtr f(HasPtr hp) // HasPtr passed by value, so it is copied
+{
+       HasPtr ret;
+       ret = hp;        // assignment copies the given HasPtr
+       // proces ret
+       return ret;      // ret and hp are destroyed
+}
+
+int main()
+{ 
+       HasPtr h("hi mom!");
+       HasPtr h2 = h;  // no new memory is allocated, 
+                       // h and h2 share the same underlying string
+} 
+
diff --git a/13/HasPtrValueLike.cc b/13/HasPtrValueLike.cc
new file mode 100644 (file)
index 0000000..824a9a8
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+
+// value-like implementation of HasPtr
+class HasPtr {
+       friend void swap(HasPtr&, HasPtr&);
+public:
+    HasPtr(const std::string &s = std::string()): 
+               ps(new std::string(s)), i(0) { }
+
+       // each HasPtr  has its own copy of the string to which ps points
+    HasPtr(const HasPtr &p): 
+               ps(new std::string(*p.ps)), i(p.i) { }
+
+       HasPtr& operator=(const HasPtr &);
+
+       ~HasPtr() { delete ps; }
+private:
+    std::string *ps;
+    int    i;
+};
+
+inline
+void swap(HasPtr &lhs, HasPtr &rhs)
+{
+       using std::swap;
+       swap(lhs.ps, rhs.ps); // swap the pointers, not the string data
+       swap(lhs.i, rhs.i);   // swap the int members
+}
+
+using std::string;
+HasPtr& HasPtr::operator=(const HasPtr &rhs) 
+{
+       auto newp = new string(*rhs.ps);  // copy the underlying string
+       delete ps;       // free the old memory
+       ps = newp;       // copy data from rhs into this object
+       i = rhs.i;
+       return *this;    // return this object
+}
+
+HasPtr f(HasPtr hp)  // HasPtr passed by value, so it is copied
+{
+       HasPtr ret = hp; // copies the given HasPtr
+       // process ret
+       return ret;      // ret and hp are destroyed
+}
+
+int main()
+{ 
+       HasPtr h("hi mom!");  // allocates a new copy of "hi mom!"
+       f(h);  // copy constructor copies h in the call to f
+                  // that copy is destroyed when f exits
+}  // h is destroyed on exit, which destroys its allocated memory
diff --git a/13/README b/13/README
new file mode 100644 (file)
index 0000000..d759066
--- /dev/null
+++ b/13/README
@@ -0,0 +1,9 @@
+No programs in this chapter read input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/13/StrFolder.cc b/13/StrFolder.cc
new file mode 100644 (file)
index 0000000..27c58fb
--- /dev/null
@@ -0,0 +1,220 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <utility>
+// for std::move, but we don't supply a using declaration for move
+
+#include <iostream>
+using std::cerr; using std::endl;
+
+#include <set>
+using std::set;  
+
+#include "StrFolder.h"
+
+void swap(Message &lhs, Message &rhs)
+{
+       using std::swap;  // not strictly needed in this case, but good habit
+
+       // remove pointers to each Message from their (original) respective Folders
+       for (auto f: lhs.folders) 
+               f->remMsg(&lhs);
+       for (auto f: rhs.folders) 
+               f->remMsg(&rhs);
+
+       // swap the contents and Folder pointer sets
+       swap(lhs.folders, rhs.folders);   // uses swap(set&, set&)
+       swap(lhs.contents, rhs.contents); // swap(String&, String&)
+
+       // add pointers to each Message to their (new) respective Folders
+       for (auto f: lhs.folders) 
+               f->addMsg(&lhs);
+       for (auto f: rhs.folders) 
+               f->addMsg(&rhs);
+}
+
+Folder::Folder(Folder &&f)
+{
+       move_Messages(&f);   // make each Message point to this Folder
+}
+
+Folder& Folder::operator=(Folder &&f) 
+{
+       if (this != &f) {        // direct check for self-assignment
+               remove_from_Msgs();  // remove this Folder from the current msgs
+               move_Messages(&f);   // make each Message point to this Folder
+       }
+       return *this;
+}
+
+void Folder::move_Messages(Folder *f)
+{
+       msgs = std::move(f->msgs); // move the set from f to this Folder
+       f->msgs.clear();      // ensure that destroying f is harmless
+       for (auto m : msgs) { // for each Message in this Folder
+               m->remFldr(f);    // remove the pointer to the old Folder
+               m->addFldr(this); // insert pointer to this Folder
+       }
+}
+
+Message::Message(Message &&m): contents(std::move(m.contents))
+{
+       move_Folders(&m); // moves folders and updates the Folder pointers
+}
+
+Message::Message(const Message &m): 
+    contents(m.contents), folders(m.folders) 
+{
+    add_to_Folders(m); // add this Message to the Folders that point to m
+}
+
+
+Message& Message::operator=(Message &&rhs) 
+{
+       if (this != &rhs) {       // direct check for self-assignment
+               remove_from_Folders();
+               contents = std::move(rhs.contents); // move assignment
+               move_Folders(&rhs); // reset the Folders to point to this Message
+       }
+    return *this;
+}
+
+Message& Message::operator=(const Message &rhs)
+{
+       // handle self-assignment by removing pointers before inserting them
+    remove_from_Folders();    // update existing Folders
+    contents = rhs.contents;  // copy message contents from rhs
+    folders = rhs.folders;    // copy Folder pointers from rhs
+    add_to_Folders(rhs);      // add this Message to those Folders
+    return *this;
+}
+
+Message::~Message()
+{
+    remove_from_Folders();
+}
+
+// move the Folder pointers from m to this Message
+void Message::move_Folders(Message *m)
+{
+       folders = std::move(m->folders); // uses set move assignment
+       for (auto f : folders) {  // for each Folder 
+               f->remMsg(m);    // remove the old Message from the Folder
+               f->addMsg(this); // add this Message to that Folder
+       }
+       m->folders.clear();  // ensure that destroying m is harmless
+}
+
+// add this Message to Folders that point to m
+void Message::add_to_Folders(const Message &m)
+{
+       for (auto f : m.folders) // for each Folder that holds m
+        f->addMsg(this); // add a pointer to this Message to that Folder
+}
+
+// remove this Message from the corresponding Folders 
+void Message::remove_from_Folders()
+{
+       for (auto f : folders)  // for each pointer in folders
+               f->remMsg(this);    // remove this Message from that Folder
+       folders.clear();        // no Folder points to this Message
+
+}
+
+void Folder::add_to_Messages(const Folder &f)
+{
+       for (auto msg : f.msgs)
+               msg->addFldr(this);   // add this Folder to each Message
+}
+
+Folder::Folder(const Folder &f) : msgs(f.msgs)
+{
+    add_to_Messages(f);  // add this Folder to each Message in f.msgs
+}
+
+Folder& Folder::operator=(const Folder &f)
+{
+    remove_from_Msgs();  // remove this folder from each Message in msgs
+       msgs = f.msgs;       // copy the set of Messages from f
+    add_to_Messages(f);  // add this folder to each Message in msgs
+    return *this;
+}
+
+Folder::~Folder()
+{
+    remove_from_Msgs();
+}
+
+
+void Folder::remove_from_Msgs()
+{
+    while (!msgs.empty())
+        (*msgs.begin())->remove(*this);
+}
+
+void Message::save(Folder &f)
+{
+    folders.insert(&f); // add the given Folder to our list of Folders
+    f.addMsg(this);     // add this Message to f's set of Messages
+}
+
+void Message::remove(Folder &f)
+{
+    folders.erase(&f); // take the given Folder out of our list of Folders
+    f.remMsg(this);    // remove this Message to f's set of Messages
+}
+
+void Folder::save(Message &m)
+{
+    // add m and add this folder to m's set of Folders
+    msgs.insert(&m);
+    m.addFldr(this);
+}
+
+void Folder::remove(Message &m)
+{
+    // erase m from msgs and remove this folder from m
+    msgs.erase(&m);
+    m.remFldr(this);
+}
+
+void Folder::debug_print()
+{
+    cerr << "Folder contains " << msgs.size() << " messages" << endl;
+    int ctr = 1;
+    for (auto m : msgs) {
+        cerr << "Message " << ctr++ << ":\n\t" << m->contents << endl;
+       }
+}
+
+void Message::debug_print()
+{
+    cerr << "Message:\n\t" << contents << endl;
+    cerr << "Appears in " << folders.size() << " Folders" << endl;
+}
diff --git a/13/StrFolder.h b/13/StrFolder.h
new file mode 100644 (file)
index 0000000..0052c98
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef STRFOLDER_H
+#define STRFOLDER_H
+
+#include "String.h"
+#include <set>
+
+class Folder;
+
+class Message {
+       friend void swap(Message&, Message&);
+       friend class Folder;
+public:
+    // folders is implicitly initialized to the empty set 
+    explicit Message(const String &str = ""): 
+               contents(str) { }  
+
+    // copy control to manage pointers to this Message
+    Message(const Message&);             // copy constructor
+    Message& operator=(const Message&);  // copy assignment
+    ~Message();                          // destructor
+    Message(Message&&);            // move constructor
+    Message& operator=(Message&&); // move assignment
+
+    // add/remove this Message from the specified Folder's set of messages
+    void save(Folder&);   
+    void remove(Folder&); 
+    void debug_print(); // print contents and it's list of Folders, 
+                        // printing each Folder as well
+private:
+    String contents;      // actual message text
+    std::set<Folder*> folders; // Folders that have this Message
+
+    // utility functions used by copy constructor, assignment, and destructor
+    // add this Message to the Folders that point to the parameter
+    void add_to_Folders(const Message&);
+       void move_Folders(Message*);
+    // remove this Message from every Folder in folders
+    void remove_from_Folders(); 
+
+    // used by Folder class to add self to this Message's set of Folder's
+    void addFldr(Folder *f) { folders.insert(f); }
+    void remFldr(Folder *f) { folders.erase(f); }
+};
+// declaration for swap should be in the same header as Message itself
+void swap(Message&, Message&);
+
+class Folder {
+       friend void swap(Message&, Message&);
+       friend class Message;
+public:
+    ~Folder(); // remove self from Messages in msgs
+    Folder(const Folder&); // add new folder to each Message in msgs
+    Folder& operator=(const Folder&); // delete Folder from lhs messages
+                                      // add Folder to rhs messages
+    Folder(Folder&&);   // move Messages to this Folder 
+    Folder& operator=(Folder&&); // delete Folder from lhs messages
+                                 // add Folder to rhs messages
+
+    Folder() = default; // defaults ok
+
+    void save(Message&);   // add this message to folder
+    void remove(Message&); // remove this message from this folder
+    
+    void debug_print(); // print contents and it's list of Folders, 
+private:
+    std::set<Message*> msgs;  // messages in this folder
+
+    void add_to_Messages(const Folder&);// add this Folder to each Message
+    void remove_from_Msgs();     // remove this Folder from each Message
+    void addMsg(Message *m) { msgs.insert(m); }
+    void remMsg(Message *m) { msgs.erase(m); }
+       void move_Messages(Folder*); // move Message pointers to point to this Folder
+};
+
+#endif
diff --git a/13/StrFolderMain.cc b/13/StrFolderMain.cc
new file mode 100644 (file)
index 0000000..d85b86a
--- /dev/null
@@ -0,0 +1,132 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <vector>
+using std::vector;
+
+#include "StrFolder.h"
+
+#include "String.h"
+
+int main()
+{
+       String s1("contents1");
+       String s2("contents2");
+       String s3("contents3");
+       String s4("contents4");
+       String s5("contents5");
+       String s6("contents6");
+       
+       // all new messages, no copies yet
+       Message m1(s1);
+       Message m2(s2);
+       Message m3(s3);
+       Message m4(s4);
+       Message m5(s5);
+       Message m6(s6);
+
+       Folder f1;
+       Folder f2;
+
+       m1.save(f1); m3.save(f1); m5.save(f1);
+       m1.save(f2);
+       m2.save(f2); m4.save(f2); m6.save(f2);
+       
+       m1.debug_print();
+       f2.debug_print();
+
+       // create some copies
+       Message c1(m1);
+       Message c2(m2), c4(m4), c6(m6);
+       
+       m1.debug_print();
+       f2.debug_print();
+
+       // now some assignments
+       m2 = m3;
+       m4 = m5;
+       m6 = m3;
+       m1 = m5;
+
+       m1.debug_print();
+       f2.debug_print();
+
+       // finally, self-assignment
+       m2 = m2;
+       m1 = m1;
+
+       m1.debug_print();
+       f2.debug_print();
+
+       vector<Message> vm;
+       cout << "capacity: " << vm.capacity() << endl;
+       vm.push_back(m1);
+
+       cout << "capacity: " << vm.capacity() << endl;
+       vm.push_back(m2);
+
+       cout << "capacity: " << vm.capacity() << endl;
+       vm.push_back(m3);
+
+       cout << "capacity: " << vm.capacity() << endl;
+       vm.push_back(m4);
+
+       cout << "capacity: " << vm.capacity() << endl;
+       vm.push_back(m5);
+
+       cout << "capacity: " << vm.capacity() << endl;
+       vm.push_back(m6);
+
+       vector<Folder> vf;
+       cout << "capacity: " << vf.capacity() << endl;
+       vf.push_back(f1);
+
+       cout << "capacity: " << vf.capacity() << endl;
+       vf.push_back(f2);
+
+       cout << "capacity: " << vf.capacity() << endl;
+       vf.push_back(Folder(f1));
+
+       cout << "capacity: " << vf.capacity() << endl;
+       vf.push_back(Folder(f2));
+
+       cout << "capacity: " << vf.capacity() << endl;
+       vf.push_back(Folder());
+
+       Folder f3;
+       f3.save(m6);
+       cout << "capacity: " << vf.capacity() << endl;
+       vf.push_back(f3);
+
+
+       return 0;
+}
diff --git a/13/StrVec.cc b/13/StrVec.cc
new file mode 100644 (file)
index 0000000..13ee9da
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "StrVec.h"
+
+#include <string>
+using std::string;
+
+#include <memory>
+using std::allocator;
+
+// errata fixed in second printing -- 
+// StrVec's allocator should be a static member not an ordinary member
+
+// definition for static data member
+allocator<string> StrVec::alloc;  
+
+// all other StrVec members are inline and defined inside StrVec.h
diff --git a/13/StrVec.h b/13/StrVec.h
new file mode 100644 (file)
index 0000000..fa7f201
--- /dev/null
@@ -0,0 +1,236 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef STRVEC_H
+#define STRVEC_H
+
+#include <iostream>
+#include <memory>
+#include <utility>
+#include <initializer_list>
+
+// simplified implementation of the memory allocation strategy for a vector-like class
+class StrVec {
+public:
+       // copy control members
+    StrVec(): 
+         elements(nullptr), first_free(nullptr), cap(nullptr) { }
+
+       StrVec(const StrVec&);            // copy constructor
+       StrVec &operator=(const StrVec&); // copy assignment
+
+       StrVec(StrVec&&) noexcept;            // move constructor
+       StrVec &operator=(StrVec&&) noexcept; // move assignment
+
+       ~StrVec() noexcept;                   // destructor
+
+       // additional constructor
+       StrVec(std::initializer_list<std::string>);
+
+    void push_back(const std::string&);  // copy the element
+    void push_back(std::string&&);       // move the element
+
+       // add elements
+    size_t size() const { return first_free - elements; }
+    size_t capacity() const { return cap - elements; }
+
+       // iterator interface
+       std::string *begin() const { return elements; }
+       std::string *end() const { return first_free; }
+    
+       // operator functions covered in chapter 14
+       StrVec &operator=(std::initializer_list<std::string>);   
+
+       std::string& operator[](std::size_t n) 
+               { return elements[n]; }
+
+       const std::string& operator[](std::size_t n) const 
+               { return elements[n]; }
+       
+       // emplace member covered in chapter 16
+       template <class... Args> void emplace_back(Args&&...);
+private:
+    static std::allocator<std::string> alloc; // allocates the elements
+
+       // utility functions:
+       //  used by members that add elements to the StrVec
+       void chk_n_alloc() 
+               { if (size() == capacity()) reallocate(); }
+    // used by the copy constructor, assignment operator, and destructor
+       std::pair<std::string*, std::string*> alloc_n_copy
+           (const std::string*, const std::string*);
+       void free();             // destroy the elements and free the space
+    void reallocate();       // get more space and copy the existing elements
+    std::string *elements;   // pointer to the first element in the array
+    std::string *first_free; // pointer to the first free element in the array
+    std::string *cap;        // pointer to one past the end of the array
+};
+
+#include <algorithm>
+
+inline
+StrVec::~StrVec() noexcept { free(); }
+
+inline
+std::pair<std::string*, std::string*> 
+StrVec::alloc_n_copy(const std::string *b, const std::string *e)
+{
+       // allocate space to hold as many elements as are in the range
+       auto data = alloc.allocate(e - b); 
+
+       // initialize and return a pair constructed from data and
+       // the value returned by uninitialized_copy
+       return {data, uninitialized_copy(b, e, data)};
+}
+
+inline
+StrVec::StrVec(StrVec &&s) noexcept  // move won't throw any exceptions
+  // member initializers take over the resources in s
+  : elements(s.elements), first_free(s.first_free), cap(s.cap)
+{
+       // leave s in a state in which it is safe to run the destructor
+       s.elements = s.first_free = s.cap = nullptr;
+}
+
+inline
+StrVec::StrVec(const StrVec &s)
+{
+       // call alloc_n_copy to allocate exactly as many elements as in s
+       auto newdata = alloc_n_copy(s.begin(), s.end());
+       elements = newdata.first; 
+       first_free = cap = newdata.second;
+}
+
+inline
+void StrVec::free()
+{
+    // may not pass deallocate a 0 pointer; if elements is 0, there's no work to do
+       if (elements) {
+       // destroy the old elements in reverse order
+               for (auto p = first_free; p != elements; /* empty */)
+                       alloc.destroy(--p);  
+               alloc.deallocate(elements, cap - elements);
+       }
+}
+       
+inline
+StrVec &StrVec::operator=(std::initializer_list<std::string> il)
+{
+       // alloc_n_copy allocates space and copies elements from the given range
+       auto data = alloc_n_copy(il.begin(), il.end());
+       free();   // destroy the elements in this object and free the space
+       elements = data.first; // update data members to point to the new space
+       first_free = cap = data.second;
+       return *this;
+}
+
+inline
+StrVec &StrVec::operator=(StrVec &&rhs) noexcept
+{
+       // direct test for self-assignment
+       if (this != &rhs) {
+               free();                   // free existing elements 
+               elements = rhs.elements;  // take over resources from rhs
+               first_free = rhs.first_free;
+               cap = rhs.cap;
+               // leave rhs in a destructible state
+               rhs.elements = rhs.first_free = rhs.cap = nullptr;
+       }
+       return *this;
+}
+
+inline
+StrVec &StrVec::operator=(const StrVec &rhs)
+{
+       // call alloc_n_copy to allocate exactly as many elements as in rhs
+       auto data = alloc_n_copy(rhs.begin(), rhs.end());
+       free();
+       elements = data.first;
+       first_free = cap = data.second;
+       return *this;
+}
+
+inline
+void StrVec::reallocate()
+{
+    // we'll allocate space for twice as many elements as the current size
+    auto newcapacity = size() ? 2 * size() : 1;
+
+       // allocate new memory
+       auto newdata = alloc.allocate(newcapacity);
+
+       // move the data from the old memory to the new
+       auto dest = newdata;  // points to the next free position in the new array
+    auto elem = elements; // points to the next element in the old array
+       for (size_t i = 0; i != size(); ++i)
+               alloc.construct(dest++, std::move(*elem++));
+
+       free();  // free the old space once we've moved the elements
+
+    // update our data structure to point to the new elements
+    elements = newdata;
+    first_free = dest;
+    cap = elements + newcapacity;
+}
+
+inline
+StrVec::StrVec(std::initializer_list<std::string> il)
+{
+       // call alloc_n_copy to allocate exactly as many elements as in il
+       auto newdata = alloc_n_copy(il.begin(), il.end());
+       elements = newdata.first;
+       first_free = cap = newdata.second;
+}
+
+inline
+void StrVec::push_back(const std::string& s)
+{
+    chk_n_alloc(); // ensure that there is room for another element
+    // construct a copy of s in the element to which first_free points
+    alloc.construct(first_free++, s);  
+}
+
+inline
+void StrVec::push_back(std::string &&s) 
+{
+    chk_n_alloc(); // reallocates the StrVec if necessary
+       alloc.construct(first_free++, std::move(s));
+}
+
+// emplace member covered in chapter 16
+template <class... Args>
+inline
+void StrVec::emplace_back(Args&&... args)
+{
+    chk_n_alloc(); // reallocates the StrVec if necessary
+       alloc.construct(first_free++, std::forward<Args>(args)...);
+}
+
+#endif
diff --git a/13/String.cc b/13/String.cc
new file mode 100644 (file)
index 0000000..089da48
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstring>
+using std::strlen;
+
+#include <algorithm>
+using std::copy; 
+
+#include <cstddef>
+using std::size_t; 
+
+#include <iostream>
+using std::ostream; 
+
+#include <utility>
+using std::swap;
+
+#include <initializer_list>
+using std::initializer_list;
+
+#include <memory>
+using std::uninitialized_copy;
+
+#include "String.h"
+
+// define the static allocator member
+std::allocator<char> String::a;
+
+// copy-assignment operator
+String & String::operator=(const String &rhs)
+{
+       // copying the right-hand operand before deleting the left handles self-assignment
+    auto newp = a.allocate(rhs.sz); // copy the underlying string from rhs
+       uninitialized_copy(rhs.p, rhs.p + rhs.sz, newp);
+
+       if (p)
+               a.deallocate(p, sz); // free the memory used by the left-hand operand
+       p = newp;    // p now points to the newly allocated string
+       sz = rhs.sz; // update the size
+
+    return *this;     
+}
+
+// move assignment operator
+String & String::operator=(String &&rhs) noexcept
+{
+       // explicit check for self-assignment
+       if (this != &rhs) {
+               if (p)
+                       a.deallocate(p, sz);  // do the work of the destructor
+               p = rhs.p;    // take over the old memory
+               sz = rhs.sz;
+               rhs.p = 0;    // deleting rhs.p is safe
+               rhs.sz = 0;
+       }
+    return *this; 
+}
+
+String& String::operator=(const char *cp)
+{
+       if (p) a.deallocate(p, sz);
+       p = a.allocate(sz = strlen(cp));
+       uninitialized_copy(cp, cp + sz, p);
+       return *this;
+}
+
+String& String::operator=(char c)
+{
+       if(p) a.deallocate(p, sz);
+       p = a.allocate(sz = 1);
+       *p = c;
+       return *this;
+}
+
+String& String::operator=(initializer_list<char> il)
+{
+       // no need to check for self-assignment
+       if (p)
+               a.deallocate(p, sz);        // do the work of the destructor
+       p = a.allocate(sz = il.size()); // do the work of the copy constructor
+       uninitialized_copy(il.begin(), il.end(), p);
+       return *this;
+}
+// named functions for operators
+ostream &print(ostream &os, const String &s)
+{
+       auto p = s.begin();
+       while (p != s.end())
+               os << *p++ ;
+       return os;
+}
+
+String add(const String &lhs, const String &rhs) 
+{
+       String ret;
+       ret.sz = rhs.size() + lhs.size();   // size of the combined String
+       ret.p = String::a.allocate(ret.sz); // allocate new space
+       uninitialized_copy(lhs.begin(), lhs.end(), ret.p); // copy the operands
+       uninitialized_copy(rhs.begin(), rhs.end(), ret.p + lhs.sz);
+       return ret;  // return a copy of the newly created String
+}
+       
+// return plural version of word if ctr isn't 1
+String make_plural(size_t ctr, const String &word,
+                               const String &ending)
+{
+        return (ctr != 1) ?  add(word, ending) : word;
+}
+
+// chapter 14 will explain overloaded operators
+ostream &operator<<(ostream &os, const String &s)
+{
+       return print(os, s);
+}
+
+String operator+(const String &lhs, const String &rhs) 
+{
+       return add(lhs, rhs);
+}
+
diff --git a/13/String.h b/13/String.h
new file mode 100644 (file)
index 0000000..d377501
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef STRING_H
+#define STRING_H
+
+#include <cstring>
+#include <algorithm>
+#include <cstddef>
+#include <iostream>
+#include <initializer_list>
+#include <iostream>
+#include <memory>
+
+class String {
+friend String operator+(const String&, const String&);
+friend String add(const String&, const String&);
+friend std::ostream &operator<<(std::ostream&, const String&);
+friend std::ostream &print(std::ostream&, const String&);
+
+public:
+       String() = default;
+
+       // cp points to a null terminated array, 
+       // allocate new memory & copy the array
+       String(const char *cp) : 
+                 sz(std::strlen(cp)), p(a.allocate(sz))
+                 { std::uninitialized_copy(cp, cp + sz, p); }
+
+       // copy constructor: allocate a new copy of the characters in s
+       String(const String &s):sz(s.sz), p(a.allocate(s.sz))
+                 { std::uninitialized_copy(s.p, s.p + sz , p); }
+
+       // move constructor: copy the pointer, not the characters, 
+       // no memory allocation or deallocation
+       String(String &&s) noexcept : sz(s.size()), p(s.p) 
+                 { s.p = 0; s.sz = 0; }
+
+       String(size_t n, char c) : sz(n), p(a.allocate(n))
+                 { std::uninitialized_fill_n(p, sz, c); }
+
+       // allocates a new copy of the data in the right-hand operand; 
+       // deletes the memory used by the left-hand operand
+       String &operator=(const String &);
+       // moves pointers from right- to left-hand operand
+       String &operator=(String &&) noexcept;
+
+       // unconditionally delete the memory because each String has its own memory
+       ~String() noexcept { if (p) a.deallocate(p, sz); }
+
+       // additional assignment operators
+       String &operator=(const char*);         // car = "Studebaker"
+       String &operator=(char);                // model = 'T'
+       String &
+       operator=(std::initializer_list<char>); // car = {'a', '4'}
+       
+       const char *begin()                         { return p; }
+       const char *begin() const                   { return p; }
+       const char *end()                      { return p + sz; }
+       const char *end() const                { return p + sz; }
+
+       size_t size() const                        { return sz; }
+       void swap(String &s)
+                       { auto tmp = p; p = s.p; s.p = tmp; 
+                         auto cnt = sz; sz = s.sz; s.sz = cnt; }
+private:
+       std::size_t sz = 0;
+       char *p = nullptr;
+       static std::allocator<char> a;
+};
+String make_plural(size_t ctr, const String &, const String &);
+inline
+void swap(String &s1, String &s2)
+{
+       s1.swap(s2);
+}
+
+#endif
diff --git a/13/copyNoMove.cc b/13/copyNoMove.cc
new file mode 100644 (file)
index 0000000..3225e33
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <utility>
+#include <iostream>
+
+class Foo {
+public:
+       Foo() = default;  // default constructor needed because Foo has a copy constructor
+       Foo(const Foo&);  // copy constructor
+       // other members, but Foo does not define a move constructor
+};
+
+Foo::Foo(const Foo&) { std::cout << "Foo copy ctor" << std::endl; }
+
+int main()
+{
+       Foo x;
+       Foo y(x);            // copy constructor; x is an lvalue
+       Foo z(std::move(x)); // copy constructor, because there is no move constructor
+}
diff --git a/13/data/storyDataFile b/13/data/storyDataFile
new file mode 100644 (file)
index 0000000..1f06b36
--- /dev/null
@@ -0,0 +1,10 @@
+Alice Emma has long flowing red hair. 
+Her Daddy says when the wind blows 
+through her hair, it looks almost alive, 
+like a fiery bird in flight. 
+A beautiful fiery bird, he tells her, 
+magical but untamed. 
+"Daddy, shush, there is no such thing," 
+she tells him, at the same time wanting 
+him to tell her more.
+Shyly, she asks, "I mean, Daddy, is there?"
diff --git a/13/makefile b/13/makefile
new file mode 100644 (file)
index 0000000..99f8b28
--- /dev/null
@@ -0,0 +1,27 @@
+# executable files for this directory
+OBJECTS = FolderMain.exe HasPtrUseCount.exe \
+                 HasPtrValueLike.exe copyNoMove.exe \
+                 moveHP.exe serial.exe StrFolderMain.exe \
+                 useStrVec.exe substr.exe 
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../1 -I../7
+
+Folder.o FolderMain.o: Folder.h
+StrFolder.o StrFolderMain.o: StrFolder.h String.h
+String.o: String.h
+useStrVec.o StrVec.o: String.h StrVec.h
+
+useStrVec.exe: StrVec.o useStrVec.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) StrVec.o useStrVec.o -o useStrVec.exe
+
+FolderMain.exe: Folder.o FolderMain.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) Folder.o FolderMain.o -o FolderMain.exe
+
+StrFolderMain.exe: StrFolder.o StrFolderMain.o String.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) StrFolder.o StrFolderMain.o String.o \
+               -o StrFolderMain.exe
+
diff --git a/13/moveHP.cc b/13/moveHP.cc
new file mode 100644 (file)
index 0000000..1ec3437
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+#include <string>
+#include <utility>
+// for swap but we do not provide a using declaration for swap
+
+// HasPtr with added move constructor
+class HasPtr {
+       friend void swap(HasPtr&, HasPtr&);
+public:
+       // default constructor and constructor that takes a string
+    HasPtr(const std::string &s = std::string()): 
+               ps(new std::string(s)), i(0) { }
+
+       // copy constructor
+    HasPtr(const HasPtr &p): 
+               ps(new std::string(*p.ps)), i(p.i) { }
+
+       // move constructor
+       HasPtr(HasPtr &&p) noexcept : ps(p.ps), i(p.i) {p.ps = 0;}
+
+       // assignment operator is both the move- and copy-assignment operator
+       HasPtr& operator=(HasPtr rhs) 
+                      { swap(*this, rhs); return *this; }
+
+       // destructor
+       ~HasPtr() { delete ps; }
+private:
+    std::string *ps;
+    int    i;
+};
+
+inline
+void swap(HasPtr &lhs, HasPtr &rhs)
+{
+       using std::swap;
+       swap(lhs.ps, rhs.ps); // swap the pointers, not the string data
+       swap(lhs.i, rhs.i);   // swap the int members
+}
+
+int main()
+{
+       HasPtr hp("hi mom");
+       HasPtr hp2(hp);
+       
+       hp = hp2; // hp2 is an lvalue; copy constructor used to copy hp2 
+       hp = std::move(hp2); // move constructor moves hp2
+}
diff --git a/13/runpgms b/13/runpgms
new file mode 100644 (file)
index 0000000..6bdbf11
--- /dev/null
@@ -0,0 +1,5 @@
+
+../noinput FolderMain.exe HasPtrUseCount.exe \
+                  HasPtrValueLike.exe copyNoMove.exe \
+                  moveHP.exe serial.exe \
+                  StrFolderMain.exe substr.exe useStrVec.exe
diff --git a/13/serial.cc b/13/serial.cc
new file mode 100644 (file)
index 0000000..2f7f1cd
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <cstddef>
+using std::size_t;
+
+struct numbered {
+       static size_t sn;
+       numbered() : mysn(sn++) { }
+       numbered(const numbered &) : mysn(sn++) { }
+       numbered &operator=(const numbered &) { return *this; }
+       size_t mysn;
+};
+
+size_t numbered::sn = 0;
+
+void f (numbered s) { cout << s.mysn << endl; }
+
+int main()
+{
+       numbered a, b = a, c = b; 
+       f(a); f(b); f(c);
+}
diff --git a/13/small_SI.h b/13/small_SI.h
new file mode 100644 (file)
index 0000000..74448c9
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced. Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/
+#ifndef SMALL_SI_H
+#define SMALL_SI_H
+
+// This file isn't used in our code, but illustrates the
+// definitions that are equivalent to Sales_data's 
+// synthesized copy control members 
+
+#include <string>
+
+class Sales_data {
+public:
+    Sales_data(const std::string & s = ""): bookNo(s),
+                                          units_sold(0), revenue(0.0) { }
+
+       Sales_data(const std::string &book, unsigned cnt, double price):
+               bookNo(book), units_sold(cnt), revenue(cnt * price) { }
+
+       // equivalent to the synthesized copy constructor
+       Sales_data(const Sales_data &rhs): bookNo(rhs.bookNo),
+                    units_sold(rhs.units_sold), revenue(rhs.revenue) { }
+
+       // equivalent to the synthesized destructor
+       // no work to do other than destroying the members, 
+       // which happens automatically
+    ~Sales_data() { } 
+   
+    Sales_data& operator=(const Sales_data &); // assignment operator
+private:
+    std::string bookNo;
+    int units_sold = 0;
+    double revenue = 0.0;
+};
+
+// equivalent to the synthesized copy-assignment operator
+Sales_data& 
+Sales_data::operator=(const Sales_data &rhs)
+{
+       bookNo = rhs.bookNo;          // calls the string::operator=
+       units_sold = rhs.units_sold;  // uses the built-in int assignment
+       revenue = rhs.revenue;        // uses the built-in double assignment
+       return *this;                 // return a reference to this object
+}
+
+#endif
diff --git a/13/substr.cc b/13/substr.cc
new file mode 100644 (file)
index 0000000..6b27b7c
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl; 
+
+#include <string>
+using std::string;
+
+string::size_type 
+position(const string &a, const string &b, const string &c)
+{
+       return (a + b).find(c);
+}
+
+int main()
+{
+       string s1 = "a value", s2 = "another";
+       auto x = position(s1, s2, "val");
+       if (x == string::npos)
+               cout << "not found" << endl;
+       else if (x < s1.size())
+               cout << "value is in first parameter" << endl;
+       else
+               cout << "value is in second parameter" << endl;
+
+       auto n = (s1 + s2).find('a');
+       cout << "n = " << n << endl;
+       s1 + s2 = "wow!";
+}
+               
diff --git a/13/useStrVec.cc b/13/useStrVec.cc
new file mode 100644 (file)
index 0000000..9698c99
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "StrVec.h"
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+using std::istream;
+
+#include <fstream>
+using std::ifstream;
+
+void print(const StrVec &svec)
+{
+       for (auto it : svec)
+               cout << it << " " ;
+       cout <<endl;
+}
+
+StrVec getVec(istream &is)
+{
+       StrVec svec;
+       string s;
+       while (is >> s)
+               svec.push_back(s);
+       return svec;
+}
+       
+int main()
+{
+       StrVec sv = {"one", "two", "three"};
+       // run the string empty funciton on the first element in sv
+       if (!sv[0].empty()) 
+               sv[0] = "None"; // assign a new value to the first string 
+
+       // we'll call getVec a couple of times
+       // and will read the same file each time
+       ifstream in("data/storyDataFile");
+       StrVec svec = getVec(in);
+       print(svec);
+       in.close();
+
+       cout << "copy " << svec.size() << endl;
+       auto svec2 = svec;
+       print(svec2);
+
+       cout << "assign" << endl;
+       StrVec svec3;
+       svec3 = svec2;
+       print(svec3);
+
+       StrVec v1, v2;
+       v1 = v2;                   // v2 is an lvalue; copy assignment
+
+       in.open("data/storyDataFile");
+       v2 = getVec(in);          // getVec(in) is an rvalue; move assignment
+       in.close();
+
+       StrVec vec;  // empty StrVec
+       string s = "some string or another";
+       vec.push_back(s);      // calls push_back(const string&)
+       vec.push_back("done"); // calls push_back(string&&)
+
+       // emplace member covered in chpater 16
+       s = "the end";
+       vec.emplace_back(10, 'c'); // adds cccccccccc as a new last element
+       vec.emplace_back(s);  // uses the string copy constructor
+       string s1 = "the beginning", s2 = s;
+       vec.emplace_back(s1 + s2); // uses the move constructor
+
+       return 0;
+}
+
diff --git a/14/README b/14/README
new file mode 100644 (file)
index 0000000..c7c82c4
--- /dev/null
+++ b/14/README
@@ -0,0 +1,13 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   add_item         add       
+   count-size       newcount-size
+   count-size       fcnobj
+   word_echo        readStr
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/14/Sales_data.cc b/14/Sales_data.cc
new file mode 100644 (file)
index 0000000..bfaa1d1
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Sales_data.h"
+#include <string>
+using std::istream; using std::ostream;
+
+Sales_data::Sales_data(istream &is) 
+{
+       is >> *this; // read a transaction from is into this object
+}
+
+double Sales_data::avg_price() const
+{
+       if (units_sold)
+               return revenue/units_sold;
+       else
+               return 0;
+}
+
+// member binary operator: left-hand operand is bound to the implicit this pointer
+// assumes that both objects refer to the same book
+Sales_data& Sales_data::operator+=(const Sales_data &rhs)
+{
+       units_sold += rhs.units_sold;
+       revenue += rhs.revenue;
+       return *this;
+}
+
+// assumes that both objects refer to the same book
+Sales_data 
+operator+(const Sales_data &lhs, const Sales_data &rhs)
+{
+       Sales_data sum = lhs;  // copy data members from lhs into sum
+       sum += rhs;            // add rhs into sum
+       return sum;
+}
+
+istream &operator>>(istream &is, Sales_data &item)
+{
+       double price;  // no need to initialize; we'll read into price before we use it
+       is >> item.bookNo >> item.units_sold >> price;
+       if (is)        // check that the inputs succeeded
+       item.revenue = item.units_sold * price;
+       else
+       item = Sales_data(); // input failed: give the object the default state
+       return is;
+}
+
+ostream &operator<<(ostream &os, const Sales_data &item)
+{
+       os << item.isbn() << " " << item.units_sold << " " 
+          << item.revenue << " " << item.avg_price();
+       return os;
+}
+
+// operators replace these original named functions
+istream &read(istream &is, Sales_data &item)
+{
+       double price = 0;
+       is >> item.bookNo >> item.units_sold >> price;
+       item.revenue = price * item.units_sold;
+       return is;
+}
+ostream &print(ostream &os, const Sales_data &item)
+{
+       os << item.isbn() << " " << item.units_sold << " " 
+          << item.revenue << " " << item.avg_price();
+       return os;
+}
+       
+Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
+{
+       Sales_data sum = lhs;  // copy data members from lhs into sum
+       sum += rhs;            // add rhs into sum
+       return sum;
+}
+
diff --git a/14/Sales_data.h b/14/Sales_data.h
new file mode 100644 (file)
index 0000000..a77159c
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef SALES_DATA_H
+#define SALES_DATA_H
+
+#include <string>
+#include <iostream>
+
+class Sales_data {
+friend std::ostream &operator<<
+                         (std::ostream&, const Sales_data&);
+friend std::istream &operator>>(std::istream&, Sales_data&);
+friend bool operator==(const Sales_data &, const Sales_data &);
+
+friend std::ostream &print(std::ostream&, const Sales_data&);
+friend std::istream &read(std::istream&, Sales_data&);
+
+public:
+       // constructors
+       Sales_data() = default;
+       Sales_data(const std::string &s): bookNo(s) { }
+       Sales_data(const std::string &s, unsigned n, double p):
+                  bookNo(s), units_sold(n), revenue(p*n) { }
+       Sales_data(std::istream &);
+
+       std::string isbn() const { return bookNo; }
+       Sales_data& operator+=(const Sales_data&);
+private:
+       double avg_price() const;  
+       std::string bookNo;
+       unsigned units_sold = 0;
+       double revenue = 0.0;
+};
+
+// non-member Sales_data operations
+inline
+bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
+{ return lhs.isbn() < rhs.isbn(); }
+
+inline
+bool operator==(const Sales_data &lhs, const Sales_data &rhs)
+{
+       return lhs.isbn() == rhs.isbn() && 
+              lhs.units_sold == rhs.units_sold && 
+              lhs.revenue == rhs.revenue;
+}
+inline
+bool operator!=(const Sales_data &lhs, const Sales_data &rhs)
+{
+       return !(lhs == rhs);
+}
+
+// old versions
+Sales_data add(const Sales_data&, const Sales_data&);
+std::ostream &print(std::ostream&, const Sales_data&);
+std::istream &read(std::istream&, Sales_data&);
+
+// new operator functions
+Sales_data operator+(const Sales_data&, const Sales_data&);
+std::ostream &operator<<(std::ostream&, const Sales_data&);
+std::istream &operator>>(std::istream&, Sales_data&);
+#endif
diff --git a/14/absInt.cc b/14/absInt.cc
new file mode 100644 (file)
index 0000000..ac72b84
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include <iterator>
+using std::inserter;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <algorithm>
+using std::transform;
+
+struct absInt {
+    int operator()(int val) const {
+        return val < 0 ? -val : val;
+    }
+};
+
+int main() {
+    int i = -42;
+    absInt absObj;           // object that has a function-call operator
+    unsigned ui = absObj(i); // passes i to absObj.operator()
+    cout << i << " " << ui << endl;
+
+       // store collection of positive and negative integers in vi
+       vector<int> vi;
+       while (cin >> i)
+               vi.push_back(i);
+
+       // call absInt to store the absolute value of those ints in vu
+       vector<unsigned> vu;
+       transform(vi.begin(), vi.end(), back_inserter(vu), absInt());
+
+       // print contents of vu using a lambda
+       for_each(vu.begin(), vu.end(), [](unsigned i) { cout << i << " "; });
+       cout << endl;
+
+       vector<unsigned> vu2;
+       // similar transformation but using a lambda
+       transform(vi.begin(), vi.end(), back_inserter(vu2),
+                 [](int i) { return i < 0 ? -i : i; });
+       if (vu == vu2)
+               cout << "as expected" << endl;
+       else {
+               cout << "something's wrong, vectors differ" << endl;
+               for_each(vu.begin(), vu.end(), [](unsigned i) { cout << i << " "; });
+       }
+       cout << endl;
+
+    return 0;
+}
diff --git a/14/add.cc b/14/add.cc
new file mode 100644 (file)
index 0000000..6fa4cc8
--- /dev/null
+++ b/14/add.cc
@@ -0,0 +1,53 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include "Sales_data.h"
+#include "Sales_item.h"
+
+int main() 
+{
+    Sales_item item1, item2;
+    cin >> item1 >> item2;   //read a pair of transactions
+    // print the sum of two Sales_items
+    cout << item1 + item2 << endl; 
+
+       Sales_data data1, data2;
+    read(read(cin, data1), data2);   //read a pair of transactions
+    print(cout, add(data1, data2));  // print the sum of two Sales_datas
+       cout << std::endl; 
+
+       cin >> data1 >> data2; // use input operator to read Sales_datas
+    // use operators to print the sum of two Sales_datas
+    cout << data1 + data2 << endl;
+
+    return 0;
+}
diff --git a/14/calc.cc b/14/calc.cc
new file mode 100644 (file)
index 0000000..05442d4
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl; using std::ostream;
+
+#include <map>
+using std::map;
+
+#include <string>
+using std::string;
+
+#include <functional>
+using std::bind; using std::function;
+using namespace std::placeholders;
+
+// ordinary function
+int add(int i, int j) { return i + j; }
+
+// lambda, which generates an unnamed function-object class
+auto mod = [](int i, int j) { return i % j; };
+
+// function-object class
+// In the first printing we named this struct div, but that name conflicts with
+// the name of a C library function.  Compilers are permitted to put
+// C library names in the global namespace.  Future printings will
+// change the name of this calss to divide.
+struct divide {
+    int operator()(int denominator, int divisor) {
+        return denominator / divisor;
+    }
+};
+
+int main()
+{
+       function<int(int, int)> f1 = add;      // function pointer
+       function<int(int, int)> f2 = divide(); // callable class type
+       function<int(int, int)> f3 = [](int i, int j) // lambda
+                                    { return i * j; };
+       cout << f1(4,2) << endl; // prints 6
+       cout << f2(4,2) << endl; // prints 2
+       cout << f3(4,2) << endl; // prints 8
+
+       // table of callable objects corresponding to each binary operator
+       // all the callables must take two ints and return an int
+       // an element can be a function pointer, function object, or lambda
+       map<string, function<int(int, int)>> binops = {
+               {"+", add},                  // function pointer
+               {"-", std::minus<int>()},    // library function object
+               {"/",  divide()},            // user-defined function object
+               {"*", [](int i, int j) { return i * j; }}, // unnamed lambda
+               {"%", mod} };                // named lambda object
+
+       cout << binops["+"](10, 5) << endl; // calls add(10, 5)
+       cout << binops["-"](10, 5) << endl; // uses the call operator of the minus<int> object
+       cout << binops["/"](10, 5) << endl; // uses the call operator of the divide object
+       cout << binops["*"](10, 5) << endl; // calls the lambda function object
+       cout << binops["%"](10, 5) << endl; // calls the lambda function object
+
+       return 0;
+}
diff --git a/14/data/absInt b/14/data/absInt
new file mode 100644 (file)
index 0000000..9726e80
--- /dev/null
@@ -0,0 +1,15 @@
+-42
+65
+-95
+100
+39
+-67
+95
+76
+-88
+76
+83
+92
+76
+-93
+0
diff --git a/14/data/add_item b/14/data/add_item
new file mode 100644 (file)
index 0000000..647b742
--- /dev/null
@@ -0,0 +1,9 @@
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
+
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
+
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
+
diff --git a/14/data/count-size b/14/data/count-size
new file mode 100644 (file)
index 0000000..da448a3
--- /dev/null
@@ -0,0 +1 @@
+the quick red fox jumps over the slow red turtle
diff --git a/14/data/word_echo b/14/data/word_echo
new file mode 100644 (file)
index 0000000..e544f33
--- /dev/null
@@ -0,0 +1,2 @@
+Now is the time for all good boys
+to come to the aid of the party
diff --git a/14/directcall.cc b/14/directcall.cc
new file mode 100644 (file)
index 0000000..6a4aa4c
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Sales_data.h"
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+int main()
+{
+       Sales_data data1, data2;
+       std::cin >> data1 >> data2;    // read Sales_data transactions
+       cout << data1 + data2 << endl; // write sum of Sales_data objects
+       cout << 42 + 5 << endl;        // write sum of ints
+       
+       // equivalent calls to a nonmember operator function
+       data1 + data2;            // normal expression
+       operator+(data1, data2);  // equivalent function call
+       cout << operator+(data1, data2) << endl;
+       
+       data1 += data2;           // expression-based ``call''
+       data1.operator+=(data2);  // equivalent call to a member operator function
+       cout << data1 << endl;
+       
+       return 0;
+}
diff --git a/14/fcnobj.cc b/14/fcnobj.cc
new file mode 100644 (file)
index 0000000..e43f8ef
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <functional>
+using std::plus; using std::negate;
+using std::function; using std::placeholders::_1;
+using std::bind; using std::less_equal; 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <algorithm>
+using std::count_if; 
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cin;
+
+#include <string>
+using std::string; 
+
+bool size_compare(const string &s, string::size_type sz)
+{
+    return s.size() >= sz;
+}
+
+int main() {
+
+       cout << plus<int>()(3,4) << endl; // prints 7
+       
+       plus<int> intAdd;      // object that can add two int values
+       negate<int> intNegate; // object that can negate an int value
+       
+       // uses intAdd::operator(int, int) to add 10 and 20
+       int sum = intAdd(10, 20);         // equivalent to sum = 30
+       cout << sum << endl;
+       
+       sum = intNegate(intAdd(10, 20));  // equivalent to sum = -30
+       cout << sum << endl;
+       
+       // uses intNegate::operator(int) to generate -10 
+       // as the second argument to intAdd::operator(int, int)
+       sum = intAdd(10, intNegate(10));  // sum = 0
+       
+       cout << sum << endl;
+       
+       vector<int> vec = {0,1,2,3,4,5,16,17,18,19};
+       
+       // bind second argument to less_equal
+       cout << count_if(vec.begin(), vec.end(),
+                            bind(less_equal<int>(), _1, 10));  
+       cout << endl;
+       
+       vector<string> svec;
+       string in;
+       while (cin >> in) 
+               svec.push_back(in);
+
+       function<decltype(size_compare)> fp1 = size_compare;
+
+       //decltype(fp1)::result_type ret;
+       function<bool(const string&)> fp2 = bind(size_compare, _1, 6);
+       cout << count_if(svec.begin(), svec.end(), fp2)
+            << endl;
+       cout << count_if(svec.begin(), svec.end(), 
+                        bind(size_compare, _1, 6))
+            << endl;
+
+
+       return 0;
+}
diff --git a/14/makefile b/14/makefile
new file mode 100644 (file)
index 0000000..574c95d
--- /dev/null
@@ -0,0 +1,17 @@
+# executable files for this directory
+OBJECTS = absInt.exe add.exe calc.exe directcall.exe \
+                 fcnobj.exe newcount-size.exe readStr.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../1 -I../6
+
+add.o directcall.o Sales_data.o: Sales_data.h
+
+add.exe: add.o Sales_data.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) add.o Sales_data.o -o add.exe
+
+directcall.exe: directcall.o Sales_data.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) directcall.o Sales_data.o -o directcall.exe
diff --git a/14/newcount-size.cc b/14/newcount-size.cc
new file mode 100644 (file)
index 0000000..87c581b
--- /dev/null
@@ -0,0 +1,156 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "make_plural.h"
+
+#include <iostream>
+using std::cin; using std::cerr; using std::cout; using std::endl;
+using std::ostream;
+
+#include <algorithm>
+using std::for_each; using std::find_if; using std::stable_sort;
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+#include <fstream>
+using std::ifstream;
+
+#include <cstddef>
+using std::size_t;
+
+// comparison function to be used to sort by word length
+bool isShorter(const string &s1, const string &s2)
+{
+    return s1.size() < s2.size();
+}
+
+// determine whether a length of a given word is 6 or more
+bool GT(const string &s, string::size_type m) 
+{
+    return s.size() >= m;
+}
+
+class SizeComp {
+public:
+       SizeComp() = delete;  // no default constructor
+       SizeComp &operator=(const SizeComp&) = delete; // no assignment
+       ~SizeComp() = default;
+
+       // constructor with a parameter for each captured variable
+       SizeComp(size_t n): sz(n) { }  
+
+       // call operator with the same return type, 
+       // parameters, and body as the lambda
+       bool operator()(const string &s) const { return s.size() >= sz; }
+private:
+       size_t sz;  // a data member for each variable captured by value
+};
+
+class PrintString {
+public:
+       PrintString() = delete;   // no default constructor
+       PrintString(ostream &o) : os(o) { }
+       void operator()(const string &s) const { os << s << " "; }
+private:
+       ostream &os;
+};
+
+class ShorterString {
+public:
+       bool operator()(const string &s1, const string &s2) const
+       { return s1.size() < s2.size(); }
+};
+
+void elimDups(vector<string> &words)
+{
+    // sort words alphabetically so we can find the duplicates
+    sort(words.begin(), words.end());
+
+       // print the sorted contents
+       for_each(words.begin(), words.end(), PrintString(cerr));
+       cerr << endl;
+
+    // unique reorders the input so that each word appears once in the
+    // front part of the range 
+       // returns an iterator one past the unique range;
+    auto end_unique = unique(words.begin(), words.end());
+
+    // erase uses a vector operation to remove the nonunique elements
+    words.erase(end_unique, words.end());
+
+       // print the reduced vector
+       for_each(words.begin(), words.end(), PrintString(cerr));
+       cerr << endl;
+}
+
+void biggies(vector<string> &words, vector<string>::size_type sz)
+{
+       elimDups(words);  // puts words in alphabetic order and removes duplicates
+
+    // sort words by size using object of type ShorterString
+       // maintaining alphabetic order for words of the same size
+    stable_sort(words.begin(), words.end(), ShorterString());
+
+       // use object of type SizeComp to find
+       // the first element whose size() is >= sz
+    auto wc = find_if(words.begin(), words.end(), SizeComp(sz));
+
+       // compute the number of elements with size >= sz 
+       auto count = words.end() - wc;
+
+       // print results
+    cout << count << " " << make_plural(count, "word", "s")
+         << " " << sz << " characters or longer" << endl;
+
+       // use object of type PrintString
+       // to print the contents of words, each one followed by a space
+       for_each(wc, words.end(), PrintString(cout));
+       cout << endl;
+}
+
+int main()
+{
+    vector<string> words;
+
+    // copy contents of each book into a single vector
+    string next_word;
+    while (cin >> next_word) {
+        // insert next book's contents at end of words
+        words.push_back(next_word);
+    }
+
+       biggies(words, 6);
+
+       return 0;
+}
+
diff --git a/14/readStr.cc b/14/readStr.cc
new file mode 100644 (file)
index 0000000..8345aa3
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::for_each;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl; using std::cerr;
+using std::istream; using std::ostream;
+
+#include <string>
+using std::string; 
+
+#include <vector>
+using std::vector;
+
+class PrintString {
+public:
+    PrintString(ostream &o = cout, char c = ' '): 
+               os(o), sep(c) { }
+    void operator()(const string &s) const { os << s << sep; }
+private:
+    ostream &os;   // stream on which to write
+       char sep;      // character to print after each output
+};
+
+class ReadLine {
+public:
+       ReadLine() = delete;
+       ReadLine(istream &i) : is(i) { }
+       bool operator()(string &s) const { return getline(is, s); }
+private:
+       istream &is;
+};
+
+int main()
+{
+       vector<string> vs;
+       ReadLine rl(cin);    // object that read lines from cin
+       string s;
+       while (rl(s))        // store what rl reads into s
+               vs.push_back(s);
+
+       cout << "read : " << vs.size() << " elements" << endl;
+       PrintString printer;   // uses the defaults; prints to cout 
+       printer(s);            // prints s followed by a space on cout
+
+       PrintString errors(cerr, '\n');
+       errors(s);             // prints s followed by a newline on cerr
+
+       cerr << "for_each printing to cerr" << endl;
+       for_each(vs.begin(), vs.end(), PrintString(cerr, '\n'));
+}
+
diff --git a/14/runpgms b/14/runpgms
new file mode 100644 (file)
index 0000000..d482966
--- /dev/null
@@ -0,0 +1,9 @@
+echo "absInt:" && ./absInt.exe < data/absInt
+echo "add: " && ./add.exe < data/add_item
+echo "fcnobj: " && ./fcnobj.exe < data/count-size
+echo "directcall: " && ./directcall.exe < data/add_item
+echo "newcount-size: " && ./newcount-size.exe < data/count-size
+echo "readStr: " && ./readStr.exe < data/word_echo
+
+../noinput calc.exe 
+                  
diff --git a/15/Basket.cc b/15/Basket.cc
new file mode 100644 (file)
index 0000000..cb662d0
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Quote.h"
+#include "Basket.h"
+
+#include <cstddef>
+using std::size_t;
+
+#include <set>
+using std::multiset; 
+
+#include <string>
+using std::string; 
+
+#include <iostream>
+using std::ostream; using std::endl; 
+using std::cout;
+
+// debugging routine to check contents in a Basket
+void Basket::display(ostream &os) const
+{
+    os << "Basket size: " << items.size() << endl;
+
+    // print each distinct ISBN in the Basket along with
+    // count of how many copies are ordered and what their price will be
+    // upper_bound returns an iterator to the next item in the set
+    for (auto next_item = items.cbegin();
+              next_item != items.cend();
+              next_item = items.upper_bound(*next_item))
+    {
+        // we know there's at least one element with this key in the Basket
+        os << (*next_item)->isbn() << " occurs " 
+           << items.count(*next_item) << " times" 
+           << " for a price of " 
+           << (*next_item)->net_price(items.count(*next_item)) 
+           << endl;
+    }
+}
+
+double Basket::total_receipt(ostream &os) const
+{
+    double sum = 0.0;    // holds the running total 
+
+
+    // iter refers to the first element in a batch of elements with the same ISBN
+    // upper_bound returns an iterator to the element just past the end of that batch
+    for (auto iter = items.cbegin(); 
+              iter != items.cend();
+              iter = items.upper_bound(*iter)) {
+        // we know there's at least one element with this key in the Basket
+               // print the line item for this book
+        sum += print_total(os, **iter, items.count(*iter));  
+    } 
+       os << "Total Sale: " << sum << endl; // print the final overall total
+    return sum;
+}
+
diff --git a/15/Basket.h b/15/Basket.h
new file mode 100644 (file)
index 0000000..6e5d784
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef BASKET_H
+#define BASKET_H
+
+#include <iostream>
+#include <string>
+#include <set>
+#include <map>
+#include <utility>
+#include <cstddef>
+#include <stdexcept>
+#include <memory>
+#include "Quote.h"
+
+// holds items being purchased
+class Basket {
+public:
+       // Basket uses synthesized default constructor and copy-control members
+       void add_item(const std::shared_ptr<Quote> &sale)  
+        { items.insert(sale); }
+
+       void add_item(const Quote& sale) // copy the given object
+      { items.insert(std::shared_ptr<Quote>(sale.clone())); }
+
+       void add_item(Quote&& sale)      // move the given object
+      { items.insert(
+             std::shared_ptr<Quote>(std::move(sale).clone())); }
+
+    // prints the total price for each book 
+       // and the overall total for all items in the basket
+    double total_receipt(std::ostream&) const;
+
+       // for debugging purposes, prints contents of the basket
+       void display (std::ostream&) const;
+private:
+       // function to compare shared_ptrs needed by the multiset member
+       static bool compare(const std::shared_ptr<Quote> &lhs,
+                           const std::shared_ptr<Quote> &rhs)
+               { return lhs->isbn() < rhs->isbn(); }
+
+       // multiset to hold multiple quotes, ordered by the compare member
+    std::multiset<std::shared_ptr<Quote>, decltype(compare)*> 
+                     items{compare}; 
+};
+
+#endif
diff --git a/15/Basket_main.cc b/15/Basket_main.cc
new file mode 100644 (file)
index 0000000..6116b81
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <memory>
+using std::shared_ptr; using std::make_shared;
+#include "Basket.h"
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       Basket sale;
+       sale.add_item(shared_ptr<Quote>(new Quote("123", 45)));
+       sale.add_item(shared_ptr<Quote>(new Quote("123", 45)));
+       sale.add_item(shared_ptr<Quote>(new Quote("123", 45)));
+       sale.add_item(make_shared<Bulk_quote>("345", 45, 3, .15));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("345", 45, 3, .15)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("345", 45, 3, .15)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("345", 45, 3, .15)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("345", 45, 3, .15)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("345", 45, 3, .15)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("345", 45, 3, .15)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("345", 45, 3, .15)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("678", 55, 5, .25)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("678", 55, 5, .25)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("678", 55, 5, .25)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("678", 55, 5, .25)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("678", 55, 5, .25)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("678", 55, 5, .25)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("678", 55, 5, .25)));
+       sale.add_item(shared_ptr<Quote>(new Bulk_quote("678", 55, 5, .25)));
+       sale.add_item(shared_ptr<Quote>(new Lim_quote("abc", 35, 2, .10)));
+       sale.add_item(shared_ptr<Quote>(new Lim_quote("abc", 35, 2, .10)));
+       sale.add_item(shared_ptr<Quote>(new Lim_quote("abc", 35, 2, .10)));
+       sale.add_item(shared_ptr<Quote>(new Lim_quote("abc", 35, 2, .10)));
+       sale.add_item(shared_ptr<Quote>(new Lim_quote("abc", 35, 2, .10)));
+       sale.add_item(shared_ptr<Quote>(new Lim_quote("abc", 35, 2, .10)));
+       sale.add_item(shared_ptr<Quote>(new Quote("def", 35)));
+       sale.add_item(shared_ptr<Quote>(new Quote("def", 35)));
+
+       sale.total_receipt(cout);
+
+       Basket bsk;
+       // arguments are the ISBN, price, minimum quantity, and discount
+       bsk.add_item(shared_ptr<Quote>(new Bulk_quote("0-201-82470-1", 50, 5, .19)));
+       bsk.add_item(shared_ptr<Quote>(new Bulk_quote("0-201-82470-1", 50, 5, .19)));
+       bsk.add_item(shared_ptr<Quote>(new Bulk_quote("0-201-82470-1", 50, 5, .19)));
+       bsk.add_item(shared_ptr<Quote>(new Bulk_quote("0-201-82470-1", 50, 5, .19)));
+       bsk.add_item(shared_ptr<Quote>(new Bulk_quote("0-201-82470-1", 50, 5, .19)));
+       bsk.add_item(shared_ptr<Quote>(new Lim_quote("0-201-54848-8", 35, 2, .10)));
+       bsk.add_item(shared_ptr<Quote>(new Lim_quote("0-201-54848-8", 35, 2, .10)));
+       bsk.add_item(shared_ptr<Quote>(new Lim_quote("0-201-54848-8", 35, 2, .10)));
+       bsk.total_receipt(cout);
+}
diff --git a/15/Query.cc b/15/Query.cc
new file mode 100644 (file)
index 0000000..140b4cb
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Query.h"
+#include "TextQuery.h"
+
+#include <memory>
+using std::shared_ptr; using std::make_shared;
+
+#include <set>
+using std::set;
+
+#include <algorithm>
+using std::set_intersection;
+
+#include <iostream>
+using std::ostream;
+
+#include <cstddef>
+using std::size_t;
+
+#include <iterator>
+using std::inserter; 
+
+// returns the lines not in its operand's result set
+QueryResult
+NotQuery::eval(const TextQuery& text) const
+{
+    // virtual call to eval through the Query operand 
+    auto result = query.eval(text);
+
+       // start out with an empty result set
+    auto ret_lines = make_shared<set<line_no>>();
+
+       // we have to iterate through the lines on which our operand appears
+       auto beg = result.begin(), end = result.end();
+
+    // for each line in the input file, if that line is not in result,
+    // add that line number to ret_lines
+       auto sz = result.get_file()->size();
+    for (size_t n = 0; n != sz; ++n) {
+               // if we haven't processed all the lines in result
+               // check whether this line is present
+               if (beg == end || *beg != n) 
+                       ret_lines->insert(n);  // if not in result, add this line 
+               else if (beg != end) 
+                       ++beg; // otherwise get the next line number in result if there is one
+       }
+       return QueryResult(rep(), ret_lines, result.get_file());
+}
+
+// returns the intersection of its operands' result sets
+QueryResult
+AndQuery::eval(const TextQuery& text) const
+{
+    // virtual calls through the Query operands to get result sets for the operands
+    auto left = lhs.eval(text), right = rhs.eval(text);
+
+       // set to hold the intersection of left and right
+    auto ret_lines = make_shared<set<line_no>>();  
+
+    // writes the intersection of two ranges to a destination iterator
+    // destination iterator in this call adds elements to ret
+    set_intersection(left.begin(), left.end(), 
+                   right.begin(), right.end(),
+                   inserter(*ret_lines, ret_lines->begin()));
+    return QueryResult(rep(), ret_lines, left.get_file());
+}
+
+// returns the union of its operands' result sets
+QueryResult
+OrQuery::eval(const TextQuery& text) const
+{
+    // virtual calls through the Query members, lhs and rhs 
+       // the calls to eval return the QueryResult for each operand
+    auto right = rhs.eval(text), left = lhs.eval(text);  
+
+       // copy the line numbers from the left-hand operand into the result set
+       auto ret_lines = 
+            make_shared<set<line_no>>(left.begin(), left.end());
+
+       // insert lines from the right-hand operand
+       ret_lines->insert(right.begin(), right.end());
+       // return the new QueryResult representing the union of lhs and rhs
+    return QueryResult(rep(), ret_lines, left.get_file());
+}
+
diff --git a/15/Query.h b/15/Query.h
new file mode 100644 (file)
index 0000000..0f2fa87
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef QUERY_H
+#define QUERY_H
+#include "TextQuery.h"
+#include <string>
+#include <set>
+#include <iostream>
+#include <fstream>
+#include <sstream>
+#include <memory>
+
+// abstract class acts as a base class for concrete query types; all members are private
+class Query_base {
+    friend class Query;  
+protected:
+    using line_no = TextQuery::line_no; // used in the eval functions
+    virtual ~Query_base() = default;
+private:
+    // eval returns the QueryResult that matches this Query
+    virtual QueryResult eval(const TextQuery&) const = 0; 
+    // rep is a string representation of the query
+       virtual std::string rep() const = 0;
+};
+
+// interface class to manage the Query_base inheritance hierarchy
+class Query {
+    // these operators need access to the shared_ptr constructor
+    friend Query operator~(const Query &);
+    friend Query operator|(const Query&, const Query&);
+    friend Query operator&(const Query&, const Query&);
+public:
+    Query(const std::string&);  // builds a new WordQuery
+
+    // interface functions: call the corresponding Query_base operations
+    QueryResult eval(const TextQuery &t) const 
+                            { return q->eval(t); }
+       std::string rep() const { return q->rep(); }
+private:
+    Query(std::shared_ptr<Query_base> query): q(query) { }
+    std::shared_ptr<Query_base> q;
+};
+inline 
+std::ostream &
+operator<<(std::ostream &os, const Query &query) 
+{
+       // Query::rep makes a virtual call through its Query_base pointer to rep() 
+       return os << query.rep(); 
+}
+
+class WordQuery: public Query_base {
+    friend class Query; // Query uses the WordQuery constructor
+    WordQuery(const std::string &s): query_word(s) { }
+
+    // concrete class: WordQuery defines all inherited pure virtual functions
+    QueryResult eval(const TextQuery &t) const
+                     { return t.query(query_word); }
+       std::string rep() const { return query_word; }
+    std::string query_word;   // word for which to search 
+};
+
+inline
+Query::Query(const std::string &s): q(new WordQuery(s)) { }
+
+class NotQuery: public Query_base {
+    friend Query operator~(const Query &);
+    NotQuery(const Query &q): query(q) { }
+
+    // concrete class: NotQuery defines all inherited pure virtual functions
+       std::string rep() const {return "~(" + query.rep() + ")";}
+    QueryResult eval(const TextQuery&) const;
+    Query query;
+};
+
+class BinaryQuery: public Query_base {
+protected:
+    BinaryQuery(const Query &l, const Query &r, std::string s): 
+          lhs(l), rhs(r), opSym(s) { }
+
+    // abstract class: BinaryQuery doesn't define eval 
+       std::string rep() const { return "(" + lhs.rep() + " " 
+                                            + opSym + " " 
+                                                + rhs.rep() + ")"; }
+
+    Query lhs, rhs;    // right- and left-hand operands
+    std::string opSym; // name of the operator
+};
+    
+class AndQuery: public BinaryQuery {
+    friend Query operator&(const Query&, const Query&);
+    AndQuery(const Query &left, const Query &right): 
+                        BinaryQuery(left, right, "&") { }
+
+    // concrete class: AndQuery inherits rep and defines the remaining pure virtual
+    QueryResult eval(const TextQuery&) const;
+};
+
+class OrQuery: public BinaryQuery {
+    friend Query operator|(const Query&, const Query&);
+    OrQuery(const Query &left, const Query &right): 
+                BinaryQuery(left, right, "|") { }
+
+    QueryResult eval(const TextQuery&) const;
+};
+
+inline Query operator&(const Query &lhs, const Query &rhs)
+{
+    return std::shared_ptr<Query_base>(new AndQuery(lhs, rhs));
+}
+
+inline Query operator|(const Query &lhs, const Query &rhs)
+{
+    return std::shared_ptr<Query_base>(new OrQuery(lhs, rhs));
+}
+
+inline Query operator~(const Query &operand)
+{
+    return std::shared_ptr<Query_base>(new NotQuery(operand));
+}
+
+std::ifstream& open_file(std::ifstream&, const std::string&);
+TextQuery get_file(int, char**);
+bool get_word(std::string&);
+bool get_words(std::string&, std::string&);
+std::ostream &print(std::ostream&, const QueryResult&);
+
+#endif
diff --git a/15/Quote.cc b/15/Quote.cc
new file mode 100644 (file)
index 0000000..28f4783
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Quote.h"
+
+#include <algorithm>
+using std::min;
+
+#include <cstddef>
+using std::size_t;
+
+#include <iostream>
+using std::ostream; using std::endl; 
+using std::cout;
+
+// calculate and print the price for the given number of copies, applying any discounts 
+double print_total(ostream &os, 
+                   const Quote &item, size_t n)
+{
+       // depending on the type of the object bound to the item parameter
+       // calls either Quote::net_price or Bulk_quote::net_price
+       double ret = item.net_price(n); 
+    os << "ISBN: " << item.isbn() // calls Quote::isbn
+       << " # sold: " << n << " total due: " << ret << endl;
+       return ret;
+}
+
+// if the specified number of items are purchased, use the discounted price 
+double Bulk_quote::net_price(size_t cnt) const
+{
+    if (cnt >= quantity)
+        return cnt * (1 - discount) * price;
+    else
+        return cnt * price;
+}
+
+// use discounted price for up to a specified number of items
+// additional items priced at normal, undiscounted price
+double Lim_quote::net_price(size_t cnt) const
+{
+    size_t discounted = min(cnt, quantity);
+    size_t undiscounted = cnt - discounted;
+    return discounted * (1 - discount) * price 
+           + undiscounted * price;
+}
+
diff --git a/15/Quote.h b/15/Quote.h
new file mode 100644 (file)
index 0000000..059c719
--- /dev/null
@@ -0,0 +1,142 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef QUOTE_H
+#define QUOTE_H
+
+#include "Version_test.h"
+
+#include <memory>
+#include <iostream>
+#include <string>
+#include <cstddef>
+
+// Item sold at an undiscounted price
+// derived classes will define various discount strategies
+class Quote {
+friend std::istream& operator>>(std::istream&, Quote&);
+friend std::ostream& operator<<(std::ostream&, const Quote&);
+public:
+       Quote() = default;  
+    Quote(const std::string &book, double sales_price):
+                     bookNo(book), price(sales_price) { }
+
+    // virtual destructor needed 
+       // if a base pointer pointing to a derived object is deleted
+    virtual ~Quote() = default; // dynamic binding for the destructor
+
+    std::string isbn() const { return bookNo; }
+
+    // returns the total sales price for the specified number of items
+    // derived classes will override and apply different discount algorithms
+    virtual double net_price(std::size_t n) const 
+               { return n * price; }
+
+       // virtual function to return a dynamically allocated copy of itself
+
+#ifdef REFMEMS
+    virtual Quote* clone() const & {return new Quote(*this);}
+    virtual Quote* clone() && {return new Quote(std::move(*this));}
+#else
+       // without reference qualification on member functions
+       // we can't overloaded on rvalue reference and const lvalue reference
+       // so for now we just implement a single version that copies itself
+    virtual Quote* clone() const {return new Quote(*this);}
+#endif
+private:
+    std::string bookNo; // ISBN number of this item
+protected:
+    double price = 0.0; // normal, undiscounted price
+};
+
+// abstract base class to hold the discount rate and quantity
+// derived classes will implement pricing strategies using these data
+class Disc_quote : public Quote {
+public:
+    // other members as before
+    Disc_quote() = default;
+    Disc_quote(const std::string& book, double price,
+              std::size_t qty, double disc):
+                 Quote(book, price),
+                 quantity(qty), discount(disc) { }
+
+    double net_price(std::size_t) const = 0;
+
+    std::pair<size_t, double> discount_policy() const
+        { return {quantity, discount}; }
+protected:
+    std::size_t quantity = 0; // purchase size for the discount to apply
+    double discount = 0.0;    // fractional discount to apply
+};
+
+// the discount kicks in when a specified number of copies of the same book are sold
+// the discount is expressed as a fraction used to reduce the normal price
+
+class Bulk_quote : public Disc_quote { // Bulk_quote inherits from Quote
+public:
+    Bulk_quote() = default;  
+    Bulk_quote(const std::string& book, double p, 
+                  std::size_t qty, double disc) :
+               Disc_quote(book, p, qty, disc) { }
+
+    // overrides the base version in order to implement the bulk purchase discount policy
+    double net_price(std::size_t) const override;
+
+#ifdef REFMEMS
+    Bulk_quote* clone() const & {return new Bulk_quote(*this);}
+    Bulk_quote* clone() && {return new Bulk_quote(std::move(*this));}
+#else
+    Bulk_quote* clone() const {return new Bulk_quote(*this);}
+#endif
+};
+
+// discount (a fraction off list) for only a specified number of copies, 
+// additional copies sold at standard price
+class Lim_quote : public Disc_quote {
+public:
+    Lim_quote(const std::string& book = "", 
+             double sales_price = 0.0,
+             std::size_t qty = 0, double disc_rate = 0.0):
+                 Disc_quote(book, sales_price, qty, disc_rate) { }
+
+    // overrides base version so as to implement limited discount policy
+    double net_price(std::size_t) const;
+
+#ifdef REFMEMS
+    Lim_quote* clone() const & { return new Lim_quote(*this); }
+    Lim_quote* clone() && { return new Lim_quote(std::move(*this)); }
+#else
+    Lim_quote* clone() const { return new Lim_quote(*this); }
+#endif
+};
+
+double print_total(std::ostream &, const Quote&, std::size_t);
+
+#endif
+
diff --git a/15/README b/15/README
new file mode 100644 (file)
index 0000000..970d20c
--- /dev/null
+++ b/15/README
@@ -0,0 +1,15 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   storyDatafile    andQueryTest
+   storyDatafile    and_orQueryTest
+   storyDatafile    wordQueryTest
+   two              andQueryTest
+   three            and_orQueryTest  
+   querymain        wordQueryTest
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/15/andQueryTest.cc b/15/andQueryTest.cc
new file mode 100644 (file)
index 0000000..6e7a3a7
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Query.h"
+#include "TextQuery.h"
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <set>
+using std::set;
+
+int main(int argc, char **argv)
+{
+       // gets file to read and builds map to support queries
+       TextQuery file = get_file(argc, argv);
+
+    do {
+        string sought1, sought2;
+        // stop if hit eof on input or a "q" is entered
+        if (!get_words(sought1, sought2)) break;
+    
+        // find all the occurrences of the requested string
+        Query andq = Query(sought1) & Query(sought2);
+        cout << "\nExecuting query for: " << andq << endl;
+        auto results = andq.eval(file);
+        // report matches
+        print(cout, results);
+    
+        results = Query(sought1).eval(file);
+        cout << "\nExecuted query: " << Query(sought1) << endl;
+        // report matches
+        print(cout, results);
+    
+        results = Query(sought2).eval(file);
+        cout << "\nExecuted query: " << Query(sought2) << endl;
+        // report matches
+        print(cout, results);
+    } while(true);
+
+    return 0;
+}
diff --git a/15/and_orQueryTest.cc b/15/and_orQueryTest.cc
new file mode 100644 (file)
index 0000000..41d5c44
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Query.h"
+#include "TextQuery.h"
+#include <string>
+#include <set>
+#include <iostream>
+
+using std::set;
+using std::string;
+using std::cin; using std::cout; using std::cerr;
+using std::endl;
+
+int main(int argc, char **argv)
+{
+    // gets file to read and builds map to support queries
+    TextQuery file = get_file(argc, argv);
+
+    // iterate with the user: prompt for a word to find and print results
+    while (true) {
+        string sought1, sought2, sought3;
+        if (!get_words(sought1, sought2)) break;
+        cout << "\nenter third word: " ;
+        cin  >> sought3;
+        // find all the occurrences of the requested string
+        Query q = Query(sought1) & Query(sought2) 
+                             | Query(sought3);
+        cout << "\nExecuting Query for: " << q << endl;
+        const auto results = q.eval(file);
+        // report matches
+               print(cout, results);
+     } 
+     return 0;
+}
diff --git a/15/data/querymain b/15/data/querymain
new file mode 100644 (file)
index 0000000..d503daf
--- /dev/null
@@ -0,0 +1,3 @@
+Alice
+container
+Daddy
diff --git a/15/data/storyDataFile b/15/data/storyDataFile
new file mode 100644 (file)
index 0000000..1f06b36
--- /dev/null
@@ -0,0 +1,10 @@
+Alice Emma has long flowing red hair. 
+Her Daddy says when the wind blows 
+through her hair, it looks almost alive, 
+like a fiery bird in flight. 
+A beautiful fiery bird, he tells her, 
+magical but untamed. 
+"Daddy, shush, there is no such thing," 
+she tells him, at the same time wanting 
+him to tell her more.
+Shyly, she asks, "I mean, Daddy, is there?"
diff --git a/15/data/three b/15/data/three
new file mode 100644 (file)
index 0000000..0c2b68c
--- /dev/null
@@ -0,0 +1,3 @@
+fiery
+bird
+wind
diff --git a/15/data/two b/15/data/two
new file mode 100644 (file)
index 0000000..59532a6
--- /dev/null
@@ -0,0 +1,2 @@
+hair
+Alice
diff --git a/15/get_print.cc b/15/get_print.cc
new file mode 100644 (file)
index 0000000..fa89d48
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Query.h"
+#include "TextQuery.h"
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::cin; 
+
+#include <fstream>
+using std::ifstream;
+
+#include <stdexcept>
+using std::runtime_error;
+
+// these functions are declared in Query.h
+TextQuery get_file(int argc, char **argv)
+{
+    // get a file to read from which user will query words
+       ifstream infile;
+       if (argc == 2)
+       infile.open(argv[1]);
+    if (!infile) {
+        throw runtime_error("No input file!");
+    }
+
+    return TextQuery(infile);  // builds query map
+}
+
+bool get_word(string &s1)
+{
+    cout << "enter a word to search for, or q to quit: ";
+    cin >> s1;
+    if (!cin || s1 == "q") return false;
+    else return true;
+}
+
+bool get_words(string &s1, string &s2)
+{
+
+    // iterate with the user: prompt for a word to find and print results
+    cout << "enter two words to search for, or q to quit: ";
+    cin  >> s1;
+
+    // stop if hit eof on input or a "q" is entered
+    if (!cin || s1 == "q") return false;
+    cin >> s2;
+    return true;
+}
+
diff --git a/15/makefile b/15/makefile
new file mode 100644 (file)
index 0000000..69ca0e5
--- /dev/null
@@ -0,0 +1,44 @@
+# executable files for this directory
+OBJECTS = andQueryTest.exe and_orQueryTest.exe \
+                 Basket_main.exe wordQueryTest.exe \
+                 sample.exe scope.exe useQuote.exe \
+                 vecBasket.exe virtualsNscope.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../12 -I../6
+
+sample.o useQuote.o Quote.o: Quote.h
+vecBasket.o Basket_main.o Basket.o: Basket.h Quote.h
+
+get_print.o andQueryTest.o and_orQueryTest.o \
+wordQueryTest.o Query.o: Query.h ../12/TextQuery.h
+
+wordQueryTest.exe: Query.o wordQueryTest.o ../12/TextQuery.o get_print.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) Query.o wordQueryTest.o \
+               ../12/TextQuery.o get_print.o -o wordQueryTest.exe
+
+andQueryTest.exe: Query.o andQueryTest.o ../12/TextQuery.o get_print.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) Query.o andQueryTest.o \
+               ../12/TextQuery.o get_print.o -o andQueryTest.exe
+
+and_orQueryTest.exe: Query.o and_orQueryTest.o ../12/TextQuery.o get_print.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) Query.o and_orQueryTest.o \
+               ../12/TextQuery.o get_print.o -o and_orQueryTest.exe
+
+useQuote.exe: useQuote.o Quote.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) useQuote.o Quote.o -o useQuote.exe
+
+vecBasket.exe: Basket.o vecBasket.o Quote.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) vecBasket.o Basket.o Quote.o \
+                       -o vecBasket.exe
+
+Basket_main.exe: Basket.o Basket_main.o Quote.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) Basket_main.o Basket.o Quote.o \
+                       -o Basket_main.exe
+
+sample.exe: sample.o Quote.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) sample.o Quote.o -o sample.exe
+
diff --git a/15/runpgms b/15/runpgms
new file mode 100644 (file)
index 0000000..bc9bd8e
--- /dev/null
@@ -0,0 +1,7 @@
+echo "andQueryTest: " && ./andQueryTest.exe data/storyDataFile < data/two
+echo "and_orQueryTest: " && ./and_orQueryTest.exe data/storyDataFile < data/three
+echo "wordQueryTest: " && ./wordQueryTest.exe data/storyDataFile < data/querymain
+
+../noinput Basket_main.exe sample.exe \
+                  scope.exe  useQuote.exe vecBasket.exe \
+                  virtualsNscope.exe
diff --git a/15/sample.cc b/15/sample.cc
new file mode 100644 (file)
index 0000000..1e65cdf
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+#include <cstddef>
+using std::size_t;
+
+using std::cout; 
+using std::endl;
+#include "Quote.h"
+
+int main()
+{
+       Quote basic("0-201-54848-8", 45);
+       Bulk_quote bulk("0-201-82470-1", 45, 3, .15);
+
+       // basic has type Quote; bulk has type Bulk_quote
+       print_total(cout, basic, 20); // calls Quote version of net_price 
+       print_total(cout, bulk, 20);  // calls Bulk_quote version of net_price 
+       Quote base("0-201-82470-1", 50);
+       Bulk_quote derived("0-201-82470-1", 50, 5, .19);
+       cout << derived.net_price(20); // calls Bulk_quote::net_price
+       cout << endl;
+
+       base = derived;        // copies the Quote part of derived into base
+       cout << base.net_price(20);    // calls Quote::net_price
+       cout << endl;
+
+       Quote &item = derived; // dynamic and static types of item differ
+       cout << item.net_price(20);    // calls Bulk_quote::net_price
+
+       item.isbn();           // isbn is not virtual, calls Bulk::isbn
+       cout << endl;
+
+       return 0;
+}
+
+
diff --git a/15/scope.cc b/15/scope.cc
new file mode 100644 (file)
index 0000000..4465c15
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+struct Base {
+    Base(): mem(0) { }
+protected:
+    int mem;
+};
+
+struct Derived : Base {
+    Derived(int i): mem(i) { } // initializes Derived::mem to i
+                                  // Base::mem is default initialized
+    int get_mem() { return mem; }  // returns Derived::mem
+    int get_base_mem() { return Base::mem; }
+       // . . .
+protected:
+    int mem;   // hides mem in the base
+};
+
+int main()
+{
+    Derived d(42);
+    cout << d.get_mem() << endl;       // prints 42
+    cout << d.get_base_mem() << endl;  // prints 0
+}
+
diff --git a/15/useQuote.cc b/15/useQuote.cc
new file mode 100644 (file)
index 0000000..fcd231c
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::ostream;
+
+#include "Quote.h"
+
+int main()
+{
+       Quote base("0-201-82470-1", 50);
+       print_total(cout, base, 10);    // calls Quote::net_price
+       
+       Bulk_quote derived("0-201-82470-1", 50, 5, .19);
+       print_total(cout, derived, 10); // calls Bulk_quote::net_price
+       
+       Quote *baseP = &derived;
+       
+       // calls the version from the base class 
+       // regardless of the dynamic type of baseP
+       double undiscounted = baseP->Quote::net_price(42);
+}
diff --git a/15/vecBasket.cc b/15/vecBasket.cc
new file mode 100644 (file)
index 0000000..b57d58c
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <vector>
+using std::vector;
+
+#include <memory>
+using std::shared_ptr; using std::make_shared;
+
+#include "Quote.h"
+
+int main ()
+{
+       Quote base("0-201-82470-1", 50);  
+       Bulk_quote bulk("0-201-54848-8", 50, 10, .25); 
+
+       // ok: but probably not what is wanted---
+       //     the objects in basket have no derived members
+       vector<Quote> basket;
+       basket.push_back(Quote("0-201-82470-1", 50));
+
+       // ok, but copies only the Quote part of the object into basket
+       basket.push_back(Bulk_quote("0-201-54848-8", 50, 10, .25));  
+       
+       // calls version defined by Quote, prints 750, i.e., 15 * $50
+       cout << basket.back().net_price(15) << endl;  
+       
+       // better approach---store shared_ptrs
+       vector<shared_ptr<Quote>> basket2;
+       
+       basket2.push_back(make_shared<Quote>("0-201-82470-1", 50));  
+       basket2.push_back(
+               make_shared<Bulk_quote>("0-201-54848-8", 50, 10, .25));  
+       
+       // calls the version defined by Quote; 
+       // prints 562.5, i.e., 15 * $50 less the discount
+       cout << basket2.back()->net_price(15) << endl; 
+
+       return 0;
+}
diff --git a/15/virtualsNscope.cc b/15/virtualsNscope.cc
new file mode 100644 (file)
index 0000000..92436ec
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+class Base {
+public:
+    virtual int fcn();
+};
+
+int Base::fcn() { cout << "Base::fcn()" << endl; return 0; }
+
+class D1 : public Base {
+public:
+    // hides fcn in the base; this fcn is not virtual
+    // D1 inherits the definition of Base::fcn() 
+    int fcn(int);      // parameter list differs from fcn in Base
+       virtual void f2(); // new virtual function that does not exist in Base
+};
+
+int D1::fcn(int) { cout << "D1::fcn(int)" << endl; return 0; }
+void D1::f2() { cout << "D1::f2()" << endl; }
+
+class D2 final : public D1 {
+public:
+    int fcn(int); // nonvirtual function hides D1::fcn(int)
+    int fcn();    // overrides virtual fcn from Base
+       void f2();    // overrides virtual f2 from D1
+};
+
+int D2::fcn(int) { cout << "D2::fcn(int)" << endl; return 0; }
+int D2::fcn() { cout << "D2::fcn()" << endl; return 0; }
+void D2::f2() { cout << "D2::f2()" << endl; }
+
+int main()
+{
+    D1 dobj, *dp = &dobj;
+    dp->fcn(42); // ok: static call to D1::fcn(int)
+
+    Base bobj;  D1 d1obj; D2 d2obj;
+
+    Base *bp1 = &bobj, *bp2 = &d1obj, *bp3 = &d2obj;
+    bp1->fcn(); // virtual call, will call Base::fcn at run time
+    bp2->fcn(); // virtual call, will call Base::fcn at run time
+    bp3->fcn(); // virtual call, will call D2::fcn at run time
+
+       D1 *d1p = &d1obj; D2 *d2p = &d2obj;
+       d1p->f2(); // virtual call, will call D1::f2() at run time
+       d2p->f2(); // virtual call, will call D2::f2() at run time
+       Base *p1 = &d2obj; D1 *p2 = &d2obj; D2 *p3 =  &d2obj;
+       p2->fcn(42);  // statically bound, calls D1::fcn(int)
+       p3->fcn(42);  // statically bound, calls D2::fcn(int)
+
+    Base* bp = &d1obj; D1 *dp1 = &d2obj; D2 *dp2 = &d2obj;
+    dp1->fcn(10); // static call to D1::fcn(int)
+    dp2->fcn(10); // static call to D2::fcn(int)
+}
+
diff --git a/15/wordQueryTest.cc b/15/wordQueryTest.cc
new file mode 100644 (file)
index 0000000..ecd5f4c
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Query.h"
+#include "TextQuery.h"
+#include <string>
+#include <vector>
+#include <map>
+#include <set>
+#include <iostream>
+#include <fstream>
+#include <cctype>
+#include <cstring>
+
+using std::set;
+using std::string;
+using std::map;
+using std::vector;
+using std::cerr;
+using std::cout;
+using std::cin;
+using std::ifstream;
+using std::endl;
+
+int main(int argc, char **argv)
+{
+    TextQuery file = get_file(argc, argv);
+
+    // iterate with the user: prompt for a word to find and print results
+    do {
+        string sought;
+        if (!get_word(sought)) break;
+
+        // find all the occurrences of the requested string
+        // define synonym for the line_no set 
+        Query name(sought);
+        const auto results = name.eval(file);
+        cout << "\nExecuting Query for: " << name << endl;
+
+        // report no matches
+        print(cout, results) << endl;
+    } while (true);  // loop indefinitely; the exit is inside the loop
+    return 0;
+}
diff --git a/16/Array.cc b/16/Array.cc
new file mode 100644 (file)
index 0000000..c1d9f84
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iterator>
+using std::begin; using std::end;
+
+#include <list>
+using std::list;
+
+#include <vector>
+using std::vector; 
+
+#include <iostream>
+using std::cout; using std::endl; 
+
+#include <string>
+using std::string;
+
+#include <initializer_list>
+using std::initializer_list;
+
+#include <cstddef>
+using std::size_t;
+
+#include "Blob.h"
+
+int main()
+{
+    Blob<int> ia;                // empty Blob<int>
+       Blob<int> ia2 = {0,1,2,3,4}; // Blob<int> with five elements
+       vector<int> v1(10, 0); // ten elements initialized to 0
+    Blob<int> ia3(v1.begin(), v1.end());  // copy elements from v1
+    cout << ia << "\n" << ia2 << "\n" << ia3 << endl;
+
+    // these definitions instantiate two distinct Blob types
+    Blob<string> names; // Blob that holds strings
+    Blob<double> prices;// different element type
+
+       // instantiates Blob<string> class and its
+       //  initializer_list<const char*> constructor
+       Blob<string> articles = {"a", "an", "the"}; // three elements
+
+       // instantiates Blob<int> and the initializer_list<int> constructor
+       Blob<int> squares = {0,1,2,3,4,5,6,7,8,9};
+
+       // instantiates Blob<int>::size() const
+    cout << squares << endl;
+       for (size_t i = 0; i != squares.size(); ++i)  
+               squares[i] = i*i; // instantiates Blob<int>::operator[](size_t)
+    cout << squares << endl;
+
+       // instantiates the Blob<int> constructor that has
+       // two vector<long>::iterator parameters
+       vector<long> vl = {0,1,2,3,4,5,6,7,8,9};
+       Blob<int> a1(vl.begin(), vl.end());   // copy from a vector
+
+       // instantiates the Blob<int> class 
+       // and the Blob<int> constructor that has two int* parameters
+       int arr[] = {0,1,2,3,4,5,6,7,8,9};
+       Blob<int> a2(begin(arr), end(arr));   // copy from an array
+
+       list<int> li(10, 0); // 10 elements all zeros
+       Blob<int> zeros(li.begin(), li.end());  // copy from a list
+
+    cout << a1 << "\n" << zeros << endl;
+
+       a1.swap(zeros);
+    cout << a1 << "\n" << zeros << endl;
+
+       list<const char*> w = {"now", "is", "the", "time"};
+
+       // instantiates the Blob<string> class and the Blob<string> 
+       // constructor that has two (list<const char*>::iterator parameters 
+       Blob<string> a3(w.begin(), w.end());  // copy from a list
+
+       return 0;
+}
diff --git a/16/Blob.h b/16/Blob.h
new file mode 100644 (file)
index 0000000..265974e
--- /dev/null
+++ b/16/Blob.h
@@ -0,0 +1,333 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef BLOB_H
+#define BLOB_H
+
+#include <iterator>
+#include <string>
+#include <vector>
+#include <initializer_list>
+#include <cstddef>
+#include <stdexcept>
+#include <utility>
+#include <memory>
+#include <algorithm>
+#include <iostream>
+#include <cstdlib>
+#include <stdexcept>
+
+// forward declarations needed for friend declarations in Blob
+template <typename> class BlobPtr;   
+template <typename> class Blob; // needed for parameters in operator==
+template <typename T> 
+    bool operator==(const Blob<T>&, const Blob<T>&);
+
+template <typename T> class Blob {
+       // each instantiation of Blob grants access to the version of
+       // BlobPtr and the equality operator instantiated with the same type
+       friend class BlobPtr<T>;
+       friend bool operator==<T>
+              (const Blob<T>&, const Blob<T>&);
+public:
+       typedef T value_type;
+       typedef typename std::vector<T>::size_type size_type;
+
+       // constructors
+       Blob(); 
+       Blob(std::initializer_list<T> il);
+       template <typename It> Blob(It b, It e); 
+       Blob(T*, std::size_t);
+
+       // return BlobPtr to the first and one past the last elements
+       BlobPtr<T> begin() { return BlobPtr<T>(*this); }
+       BlobPtr<T> end() 
+           { auto ret = BlobPtr<T>(*this, data->size()); 
+             return ret; }
+
+       // number of elements in the Blob
+       size_type size() const { return data->size(); }
+       bool empty() const { return data->empty(); }
+
+       // add and remove elements
+       void push_back(const T &t) {data->push_back(t);}
+       void push_back(T &&t) { data->push_back(std::move(t)); }
+       void pop_back();
+
+       // element access
+       T& front();
+       T& back();
+       T& at(size_type);
+       const T& back() const;
+       const T& front() const;
+       const T& at(size_type) const;
+       T& operator[](size_type i); 
+       const T& operator[](size_type i) const;
+
+       void swap(Blob &b) { data.swap(b.data); }
+private:
+       std::shared_ptr<std::vector<T>> data; 
+
+       // throws msg if data[i] isn't valid
+       void check(size_type i, const std::string &msg) const;
+};
+
+// constructors
+template <typename T>
+Blob<T>::Blob(T *p, std::size_t n): 
+              data(std::make_shared<std::vector<T>>(p, p + n)) { }
+
+template <typename T>
+Blob<T>::Blob():
+                 data(std::make_shared<std::vector<T>>()) { }
+
+template <typename T>     // type parameter for the class
+template <typename It>    // type parameter for the constructor
+    Blob<T>::Blob(It b, It e):
+              data(std::make_shared<std::vector<T>>(b, e)) { }
+
+template <typename T>
+Blob<T>::Blob(std::initializer_list<T> il): 
+              data(std::make_shared<std::vector<T>>(il)) { }
+
+// check member
+template <typename T>
+void Blob<T>::check(size_type i, const std::string &msg) const
+{
+       if (i >= data->size())
+               throw std::out_of_range(msg);
+}
+
+// element access members
+template <typename T>
+T& Blob<T>::front()
+{
+       // if the vector is empty, check will throw
+       check(0, "front on empty Blob");
+       return data->front();
+}
+
+template <typename T>
+T& Blob<T>::back() 
+{
+       check(0, "back on empty Blob");
+       return data->back();
+}
+
+template <typename T> void Blob<T>::pop_back() 
+{
+       check(0, "pop_back on empty Blob"); 
+       data->pop_back(); 
+}
+
+template <typename T>
+const T& Blob<T>::front() const 
+{
+       check(0, "front on empty Blob");
+       return data->front();
+}
+
+template <typename T>
+const T& Blob<T>::back() const 
+{
+       check(0, "back on empty Blob");
+       return data->back();
+}
+
+template <typename T>
+T& Blob<T>::at(size_type i) 
+{
+       // if i is too big, check will throw, preventing access to a nonexistent element
+       check(i, "subscript out of range");
+       return (*data)[i];  // (*data) is the vector to which this object points
+}
+
+template <typename T>
+const T&
+Blob<T>::at(size_type i) const
+{
+       check(i, "subscript out of range");
+       return (*data)[i];
+}
+
+template <typename T>
+T& Blob<T>::operator[](size_type i)
+{
+       // if i is too big, check will throw, preventing access to a nonexistent element
+       check(i, "subscript out of range");
+       return (*data)[i];
+}
+
+template <typename T>
+const T& 
+Blob<T>::operator[](size_type i) const
+{
+       check(i, "subscript out of range");
+       return (*data)[i];
+}
+
+// operators
+template <typename T>
+std::ostream&
+operator<<(std::ostream &os, const Blob<T> a)
+{
+       os << "< ";
+       for (size_t i = 0; i < a.size(); ++i) 
+               os << a[i] << " ";
+       os << " >";
+       return os;
+}
+
+template <typename T>
+bool
+operator==(const Blob<T> lhs, const Blob<T> rhs)
+{
+       if (rhs.size() != lhs.size())
+               return false;
+       for (size_t i = 0; i < lhs.size(); ++i) {
+               if (lhs[i] != rhs[i])
+                       return false;
+       }
+       return true;
+}
+
+// BlobPtr throws an exception on attempts to access a nonexistent element 
+template <typename T>
+bool operator==(const BlobPtr<T>&, const BlobPtr<T>&);
+
+template <typename T> class BlobPtr : public std::iterator<std::bidirectional_iterator_tag,T> {
+       friend bool 
+       operator==<T>(const BlobPtr<T>&, const BlobPtr<T>&);
+public:
+    BlobPtr(): curr(0) { }
+    BlobPtr(Blob<T> &a, size_t sz = 0): 
+               wptr(a.data), curr(sz) { }
+
+       T &operator[](std::size_t i)
+       { auto p = check(i, "subscript out of range"); 
+         return (*p)[i];  // (*p) is the vector to which this object points
+       }
+
+       const T &operator[](std::size_t i) const
+       { auto p = check(i, "subscript out of range"); 
+         return (*p)[i];  // (*p) is the vector to which this object points
+       }
+    
+    T& operator*() const
+       { auto p = check(curr, "dereference past end"); 
+         return (*p)[curr];  // (*p) is the vector to which this object points
+       }
+    T* operator->() const
+       { // delegate the real work to the dereference operator
+        return & this->operator*(); 
+       }
+
+    // increment and decrement
+    BlobPtr& operator++();       // prefix operators
+    BlobPtr& operator--();
+
+    BlobPtr operator++(int);     // postfix operators
+    BlobPtr operator--(int);
+    
+private:
+       // check returns a shared_ptr to the vector if the check succeeds
+       std::shared_ptr<std::vector<T>> 
+               check(std::size_t, const std::string&) const;
+
+       // store a weak_ptr, which means the underlying vector might be destroyed
+    std::weak_ptr<std::vector<T>> wptr;  
+    std::size_t curr;      // current position within the array
+};
+
+// equality operators
+template <typename T>
+bool operator==(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs)
+{
+       return lhs.wptr.lock().get() == rhs.wptr.lock().get() && 
+              lhs.curr == rhs.curr;
+}
+
+template <typename T>
+bool operator!=(const BlobPtr<T> &lhs, const BlobPtr<T> &rhs)
+{
+       return !(lhs == rhs);
+}
+
+// check member
+template <typename T>
+std::shared_ptr<std::vector<T>> 
+BlobPtr<T>::check(std::size_t i, const std::string &msg) const
+{
+       auto ret = wptr.lock();   // is the vector still around?
+       if (!ret)
+               throw std::runtime_error("unbound BlobPtr");
+       if (i >= ret->size()) 
+               throw std::out_of_range(msg);
+       return ret; // otherwise, return a shared_ptr to the vector
+}
+
+// member operators
+// postfix: increment/decrement the object but return the unchanged value
+template <typename T>
+BlobPtr<T> BlobPtr<T>::operator++(int)
+{
+    // no check needed here; the call to prefix increment will do the check
+    BlobPtr ret = *this;   // save the current value
+    ++*this;     // advance one element; prefix ++ checks the increment
+    return ret;  // return the saved state
+}
+
+template <typename T>
+BlobPtr<T> BlobPtr<T>::operator--(int)
+{
+    // no check needed here; the call to prefix decrement will do the check
+    BlobPtr ret = *this;  // save the current value
+       --*this;     // move backward one element; prefix -- checks the decrement
+    return ret;  // return the saved state
+}
+
+// prefix: return a reference to the incremented/decremented object
+template <typename T>
+BlobPtr<T>& BlobPtr<T>::operator++()
+{
+       // if curr already points past the end of the container, can't increment it
+       check(curr, "increment past end of BlobPtr");
+    ++curr;       // advance the current state
+    return *this;
+}
+
+template <typename T>
+BlobPtr<T>& BlobPtr<T>::operator--()
+{
+       // if curr is zero, decrementing it will yield an invalid subscript
+    --curr;       // move the current state back one element
+    check(-1, "decrement past begin of BlobPtr");
+    return *this;
+}
+#endif
diff --git a/16/DebugDelete.h b/16/DebugDelete.h
new file mode 100644 (file)
index 0000000..93431c3
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef DEBUGDELETE_H
+#define DEBUGDELETE_H
+
+#include <cstddef>
+#include <iostream>
+#include <string>
+
+// function-object class that calls delete on a given pointer
+class DebugDelete {
+public:
+       DebugDelete(const std::string &s = "unique_ptr",
+                std::ostream &strm = std::cerr): os(strm), type(s) { }
+       // as with any function template, the type of T is deduced by the compiler
+       template <typename T> void operator()(T *p) const 
+         { os << "deleting " << type << std::endl; delete p; }
+private:
+       std::ostream &os;  // where to print debugging info
+       std::string type;  // what type of smart pointer we're deleting
+};
+
+#endif
diff --git a/16/README b/16/README
new file mode 100644 (file)
index 0000000..830368d
--- /dev/null
+++ b/16/README
@@ -0,0 +1,12 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   book_sales       compareDef
+   book_trans       multiset  
+   Vecmainin        Vecmain
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/16/SP.cc b/16/SP.cc
new file mode 100644 (file)
index 0000000..3880413
--- /dev/null
+++ b/16/SP.cc
@@ -0,0 +1,69 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <memory>
+using std::unique_ptr; using std::shared_ptr;
+
+// function-object class that calls delete on a given pointer
+#include "DebugDelete.h"
+
+int main()
+{
+       double* p = new double;
+       // an object that can act like a delete expression
+       DebugDelete d("plain pointer");
+       d(p); // calls DebugDelete::operator()(double*), which deletes p
+
+       int* ip = new int;
+       // calls operator()(int*) on a temporary DebugDelete object
+       DebugDelete("plain pointer")(ip); 
+
+       // destroying the the object to which upi points
+       // instantiates DebugDelete::operator()<int>(int *) 
+       unique_ptr<int, DebugDelete> upi(new int, DebugDelete()); 
+
+       // destroying the the object to which ups points
+       // instantiates DebugDelete::operator()<string>(string*)
+       unique_ptr<string, DebugDelete> ups(new string, DebugDelete()); 
+
+       // illustrate other types using DebugDelete as their deleter
+       shared_ptr<int> sp1(new int(42), DebugDelete("shared_ptr"));
+
+       return 0;
+}
+
diff --git a/16/Sales_data.cc b/16/Sales_data.cc
new file mode 100644 (file)
index 0000000..6fccd43
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::istream; using std::ostream;
+
+#include "Sales_data.h"
+
+// define the hash interface for Sales_data
+
+namespace std {
+size_t
+hash<Sales_data>::operator()(const Sales_data& s) const
+{
+    return hash<string>()(s.bookNo) ^
+           hash<unsigned>()(s.units_sold) ^
+           hash<double>()(s.revenue);
+}
+}  // close the std namespace; note: no semicolon after the close curly
+
+// remaining members unchanged from chapter 14
+Sales_data::Sales_data(istream &is) 
+{
+       is >> *this; // read a transaction from is into this object
+}
+
+double Sales_data::avg_price() const
+{
+       if (units_sold)
+               return revenue/units_sold;
+       else
+               return 0;
+}
+
+// member binary operator: left-hand operand is bound to the implicit this pointer
+// assumes that both objects refer to the same book
+Sales_data& Sales_data::operator+=(const Sales_data &rhs)
+{
+       units_sold += rhs.units_sold;
+       revenue += rhs.revenue;
+       return *this;
+}
+
+// assumes that both objects refer to the same book
+Sales_data 
+operator+(const Sales_data &lhs, const Sales_data &rhs)
+{
+       Sales_data sum = lhs;  // copy data members from lhs into sum
+       sum += rhs;            // add rhs into sum
+       return sum;
+}
+
+istream &operator>>(istream &is, Sales_data &item)
+{
+       double price;  // no need to initialize; we'll read into price before we use it
+       is >> item.bookNo >> item.units_sold >> price;
+       if (is)        // check that the inputs succeeded
+       item.revenue = item.units_sold * price;
+       else
+       item = Sales_data(); // input failed: give the object the default state
+       return is;
+}
+
+ostream &operator<<(ostream &os, const Sales_data &item)
+{
+       os << item.isbn() << " " << item.units_sold << " " 
+          << item.revenue << " " << item.avg_price();
+       return os;
+}
+
+// operators replace these original named functions
+istream &read(istream &is, Sales_data &item)
+{
+       double price = 0;
+       is >> item.bookNo >> item.units_sold >> price;
+       item.revenue = price * item.units_sold;
+       return is;
+}
+ostream &print(ostream &os, const Sales_data &item)
+{
+       os << item.isbn() << " " << item.units_sold << " " 
+          << item.revenue << " " << item.avg_price();
+       return os;
+}
+       
+Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
+{
+       Sales_data sum = lhs;  // copy data members from lhs into sum
+       sum += rhs;            // add rhs into sum
+       return sum;
+}
+
diff --git a/16/Sales_data.h b/16/Sales_data.h
new file mode 100644 (file)
index 0000000..fc2182d
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef SALES_DATA_H
+#define SALES_DATA_H
+
+#include <string>
+#include <iostream>
+
+// unchanged from chapter 14 except for added friend declaration for hash
+class Sales_data {
+friend class std::hash<Sales_data>;
+friend std::ostream &operator<<
+                         (std::ostream&, const Sales_data&);
+friend std::istream &operator>>(std::istream&, Sales_data&);
+friend bool operator==(const Sales_data &, const Sales_data &);
+
+friend std::ostream &print(std::ostream&, const Sales_data&);
+friend std::istream &read(std::istream&, Sales_data&);
+public:
+       // constructors
+       Sales_data() = default;
+       Sales_data(const std::string &s): bookNo(s) { }
+       Sales_data(const std::string &s, unsigned n, double p):
+                  bookNo(s), units_sold(n), revenue(p*n) { }
+       Sales_data(std::istream &);
+       std::string isbn() const { return bookNo; }
+       Sales_data& operator+=(const Sales_data&);
+private:
+       double avg_price() const;  
+       std::string bookNo;
+       unsigned units_sold = 0;
+       double revenue = 0.0;
+};
+
+namespace std {
+template <>              // we're defining a specialization with
+struct hash<Sales_data>  // the template parameter of Sales_data
+{
+    // the type used to hash an unordered container must define these types
+    typedef size_t result_type;
+    typedef Sales_data argument_type; // by default, this type needs ==
+
+    size_t operator()(const Sales_data& s) const;
+
+    // our class uses synthesized copy control and default constructor
+    // other members as before
+};
+}  // close the std namespace; note: no semicolon after the close curly
+
+// non-member Sales_data operations
+inline
+bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
+{ return lhs.isbn() < rhs.isbn(); }
+
+inline
+bool operator==(const Sales_data &lhs, const Sales_data &rhs)
+{
+       return lhs.isbn() == rhs.isbn() && 
+              lhs.units_sold == rhs.units_sold && 
+              lhs.revenue == rhs.revenue;
+}
+inline
+bool operator!=(const Sales_data &lhs, const Sales_data &rhs)
+{
+       return !(lhs == rhs);
+}
+
+Sales_data add(const Sales_data&, const Sales_data&);
+std::ostream &print(std::ostream&, const Sales_data&);
+std::istream &read(std::istream&, Sales_data&);
+Sales_data operator+(const Sales_data&, const Sales_data&);
+std::ostream &operator<<(std::ostream&, const Sales_data&);
+std::istream &operator>>(std::istream&, Sales_data&);
+#endif
diff --git a/16/Vec.h b/16/Vec.h
new file mode 100644 (file)
index 0000000..3f0cd35
--- /dev/null
+++ b/16/Vec.h
@@ -0,0 +1,228 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef VEC_H
+#define VEC_H
+
+#include <algorithm>
+#include <memory>
+#include <utility>
+#include <initializer_list>
+
+// simplified-implementation of memory allocation strategy for a vector-like class
+template <typename T>
+class Vec {
+public:
+    Vec() = default;
+       Vec(const Vec&);                 // copy constructor
+       Vec(Vec&&) noexcept;             // move constructor
+       Vec &operator=(const Vec&);      // copy assignment
+       Vec &operator=(Vec&&) noexcept;  // move assignment
+       ~Vec() noexcept;                 // destructor
+
+       // list assignment
+       Vec &operator=(std::initializer_list<T>);   
+
+       // add elements
+    void push_back(const T&);
+    void push_back(T&&);
+       template <class... Args> void emplace_back(Args&&...);
+       
+       // size and capacity
+    size_t size() const { return first_free - elements; }
+    size_t capacity() const { return cap - elements; }
+
+       // element access
+    T& operator[](size_t n) { return elements[n]; }
+    const T& operator[](size_t n) const { return elements[n]; }
+
+       // iterator interface
+       T *begin() const { return elements; }
+       T *end() const { return first_free; }
+private:
+    static std::allocator<T> alloc; // allocates the elements
+       // used by functions that add elements to the Vec
+       void chk_n_alloc() { if (first_free == cap) reallocate(); }
+
+    // utilities used by copy constructor, assignment operator, and destructor
+       std::pair<T*, T*> 
+         alloc_n_copy(const T*, const T*);
+       void free();
+    void reallocate(); // get more space and copy existing elements
+    T* elements = nullptr;   // pointer to first element in the array
+    T* first_free = nullptr; // pointer to first free element in the array
+    T* cap = nullptr;        // pointer to one past the end of the array
+};
+
+
+//definition for the static data member 
+template <typename T> std::allocator<T> Vec<T>::alloc;  
+
+template <typename T>
+inline
+Vec<T>::~Vec() noexcept { free(); }
+
+template <typename T>
+inline
+std::pair<T*, T*> 
+Vec<T>::alloc_n_copy(const T *b, const T *e) 
+{
+       auto data = alloc.allocate(e - b);
+       return {data, uninitialized_copy(b, e, data)};
+}
+
+template <typename T>
+inline
+Vec<T>::Vec(Vec &&s) noexcept : 
+       // take over resources from s
+       elements(s.elements), first_free(s.first_free), cap(s.cap)
+{
+       // leave s in a state in which it is safe to run the destructor
+       s.elements = s.first_free = s.cap = nullptr;
+}
+
+template <typename T>
+inline
+Vec<T>::Vec(const Vec &s)
+{
+       // call copy to allocate exactly as many elements as in s
+       auto newdata = alloc_n_copy(s.begin(), s.end()); 
+       elements = newdata.first; 
+       first_free = cap = newdata.second;
+}
+
+template <typename T>
+inline
+void Vec<T>::free()
+{
+    // destroy the old elements in reverse order
+       for (auto p = first_free; p != elements; /* empty */)
+               alloc.destroy(--p);  // destroy elements in reverse order
+    
+    // deallocate cannot be called on a 0 pointer
+       if (elements)
+               alloc.deallocate(elements, cap - elements);
+}
+       
+template <typename T>
+inline
+Vec<T> &Vec<T>::operator=(std::initializer_list<T> il)
+{
+       // copy allocates space and copies elements from the given range
+       auto data = alloc_n_copy(il.begin(), il.end());
+
+       free();   // destroy the elements in this object and free the space
+
+       elements = data.first; // update data members to point to the new space
+       first_free = cap = data.second;
+
+       return *this;
+}
+
+template <typename T>
+inline
+Vec<T> &Vec<T>::operator=(Vec &&rhs) noexcept
+{
+       // direct test for self-assignment
+       if (this != &rhs)
+               free();  // free existing elements if appropriate
+
+       elements = rhs.elements;  // take over resources from rhs
+       first_free = rhs.first_free;
+       cap = rhs.cap;
+
+       // leave rhs in a destructible state
+       rhs.elements = rhs.first_free = rhs.cap = nullptr;
+
+       return *this;
+}
+
+template <typename T>
+inline
+Vec<T> &Vec<T>::operator=(const Vec &rhs)
+{
+       // call copy to allocate exactly as many elements as in rhs
+       auto data = alloc_n_copy(rhs.begin(), rhs.end());
+       free();
+       elements = data.first;
+       first_free = cap = data.second;
+       return *this;
+}
+
+template <typename T>
+inline
+void Vec<T>::reallocate()
+{
+    // we'll allocate space for twice as many elements as current size
+    auto newcapacity = size() ? 2 * size() : 2;
+
+       // allocate new space
+       auto first = alloc.allocate(newcapacity);
+       auto dest = first;
+       auto elem = elements;
+
+       // move the elements
+       for (size_t i = 0; i != size(); ++i)
+               alloc.construct(dest++, std::move(*elem++));
+       free();  // free the old space once we've moved the elements
+
+    // update our data structure point to the new elements
+    elements = first;
+    first_free = dest;
+    cap = elements + newcapacity;
+}
+
+template <typename T>
+inline
+void Vec<T>::push_back(const T& s)
+{
+    chk_n_alloc(); // reallocates the Vec if necessary
+    // construct a copy s in the element to which first_free points
+    alloc.construct(first_free++, s);  
+}
+
+template <typename T>
+inline
+void Vec<T>::push_back(T&& s) 
+{
+    chk_n_alloc(); // reallocates the Vec if necessary
+       alloc.construct(first_free++, std::move(s));
+}
+
+template <typename T>
+template <class... Args>
+inline
+void Vec<T>::emplace_back(Args&&... args)
+{
+    // any space left?
+    chk_n_alloc(); // reallocates the Vec if necessary
+       alloc.construct(first_free++, std::forward<Args>(args)...);
+}
+
+#endif
diff --git a/16/Vecmain.cc b/16/Vecmain.cc
new file mode 100644 (file)
index 0000000..482c8e0
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Vec.h"
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+using std::istream;
+
+void print(const Vec<string> &svec)
+{
+       for (auto it : svec)
+               cout << it << " " ;
+       cout <<endl;
+}
+
+Vec<string> getVec(istream &is)
+{
+       Vec<string> svec;
+       string s;
+       while (is >> s)
+               svec.push_back(s);
+       return svec;
+}
+       
+int main()
+{
+       Vec<string> svec = getVec(cin);
+       print(svec);
+
+       cout << "copy " << svec.size() << endl;
+       auto svec2 = svec;
+       print(svec2);
+
+       cout << "assign" << endl;
+       Vec<string> svec3;
+       svec3 = svec2;
+       print(svec3);
+
+       Vec<string> v1, v2;
+       Vec<string> getVec(istream &);
+       v1 = v2;           // copy assignment
+       v2 = getVec(cin);  // move assignment
+
+       return 0;
+}
diff --git a/16/array-parms4.cc b/16/array-parms4.cc
new file mode 100644 (file)
index 0000000..f0df1c2
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <cstddef>
+using std::size_t;
+
+// when we call print the compiler will instantiate a version of print
+// with T replaced by the argument's element type, and N by that array's dimension
+template <typename T, size_t N>
+void print(T (&arr)[N])
+{
+    for (auto elem : arr)
+        cout << elem << endl;
+}
+
+int main()
+{
+       int a1[] = {0,1,2,3,4,5,6,7,8,9};
+       int a2[] = {1,3,5};
+       string a3[4];
+
+       print(a1);  // instantiates print(int (&arr)[10])
+
+       print(a2);  // instantiates print(int (&arr)[3])
+
+       print(a3);  // instantiates print(string (&arr)[42])
+
+       return 0;
+}
diff --git a/16/build.cc b/16/build.cc
new file mode 100644 (file)
index 0000000..ca5faa6
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Blob.h"
+#include "compare.h"
+
+#include <string>
+using std::string;
+
+// templateBuild.cc
+// instantiation file must provide a (nonextern) definition for every
+// type and function that other files declare as extern
+template int compare(const int&, const int&);
+template class Blob<string>; // instantiates all members of the class template 
+template class Blob<int>;    // instantiates Blob<int>
diff --git a/16/compare.cc b/16/compare.cc
new file mode 100644 (file)
index 0000000..8a5540d
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+#include <cstring>
+using std::strcmp;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include "compare.h"
+
+int main()
+{
+    // instantiates int compare(const int&, const int&)
+    cout << compare(1, 0) << endl;       // T is int
+
+    // instantiates int compare(const vector<int>&, const vector<int>&)
+    vector<int> vec1{1, 2, 3}, vec2{4, 5, 6};
+    cout << compare(vec1, vec2) << endl; // T is vector<int>
+
+    long l1, l2;
+    int i1, i2;
+    compare(i1, i2);      // instantiate compare(int, int)
+    compare(l1, l2);      // instantiate compare(long, long)
+       compare<int>(i1, l2); // uses compare(int, int)
+       compare<long>(i1, l2);// uses compare(long, long)
+
+    const char *cp1 = "hi", *cp2 = "world";
+    compare(cp1, cp2);          // calls the specialization
+    compare<string>(cp1, cp2);  // converts arguments to string
+
+    return 0;
+}
+
diff --git a/16/compare.h b/16/compare.h
new file mode 100644 (file)
index 0000000..0961292
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef COMPARE_H
+#define COMPARE_H
+#include <cstring>
+
+// implement strcmp-like generic compare function 
+// returns 0 if the values are equal, 1 if v1 is larger, -1 if v1 is smaller
+template <typename T>
+int compare(const T &v1, const T &v2)
+{
+    if (v1 < v2) return -1;
+    if (v2 < v1) return 1;
+    return 0;
+}
+
+// special version of compare to handle C-style character strings
+template <>
+inline
+int compare(const char* const &v1, const char* const &v2)
+{
+    return std::strcmp(v1, v2);
+}
+
+#endif
diff --git a/16/compareDef.cc b/16/compareDef.cc
new file mode 100644 (file)
index 0000000..af75a3b
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Sales_data.h"
+
+#include <iostream>
+using std::cout; using std::endl; using std::cin;
+
+#include <functional>
+using std::less; using std::greater;
+
+// compare has a default template argument, less<T> 
+// and a default function argument, F()
+template <typename T, typename F = less<T>> 
+int compare(const T &v1, const T &v2, F f = F()) 
+{
+       if (f(v1, v2)) return -1;
+       if (f(v2, v1)) return 1;  
+       return 0;
+}
+
+int main()
+{
+       bool i = compare(0, 42); // uses less; i is -1
+
+       // result depends on the isbns in item1 and item2
+       Sales_data item1(cin), item2(cin);
+       bool j = compare(item1, item2, compareIsbn);
+
+       return 0;
+}
diff --git a/16/compareSpec.cc b/16/compareSpec.cc
new file mode 100644 (file)
index 0000000..5f7cbd5
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <functional>
+using std::less;
+
+#include <cstring>
+using std::strcmp;
+
+// version of compare that will be correct even if used on pointers
+template <typename T> int compare(const T &v1, const T &v2)
+{
+       cout << "compare(T)" << "\t";
+
+       if (less<T>()(v1, v2)) return -1;
+       if (less<T>()(v2, v1)) return 1;
+
+       return 0;
+}
+
+template<unsigned N, unsigned M> 
+int compare(const char (&p1)[N], const char (&p2)[M])
+{
+       cout << "compare(const char arrays)" << "\t";
+
+       return strcmp(p1, p2);
+}
+
+template<> 
+int compare(const char *const& p1, const char *const& p2)
+{
+       cout << "compare(const char*const)" << "\t";
+
+       return strcmp(p1, p2);
+}
+
+int main()
+{
+       int *p1 = new int(45);
+       int *p2 = new int(42);
+
+       // because we're comparing pointer values, the result of
+    // this call may vary each time the program is run
+       cout << compare(*p1, *p2) << endl;
+       cout << compare(p1, p2) << endl;
+
+       cout << strcmp("hi", "mom") << endl;
+       cout << compare("hi", "mom") << endl;
+
+       const char *cp1 = "hi", *cp2= "mom";
+       cout << compare(cp1, cp2) << endl;
+       
+       return 0;
+}
diff --git a/16/data/Vecmain b/16/data/Vecmain
new file mode 100644 (file)
index 0000000..e440727
--- /dev/null
@@ -0,0 +1,7 @@
+Although most programmers are familiar with data structures
+such as |vector|s and |list|s, many have never used an
+associative data structure.
+Before we look at the details of how the library supports
+these types, it will be helpful to start with
+examples of how we can use
+these containers.
diff --git a/16/data/book_sales b/16/data/book_sales
new file mode 100644 (file)
index 0000000..4224fa4
--- /dev/null
@@ -0,0 +1,8 @@
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
+0-999-78345-X 3 20.00
+0-201-78345-X 2 25.00
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
+0-999-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/16/data/book_trans b/16/data/book_trans
new file mode 100644 (file)
index 0000000..5705960
--- /dev/null
@@ -0,0 +1,2 @@
+0-201-70353-X 4 24.99
+0-201-82470-1 4 45.39
diff --git a/16/debug.cc b/16/debug.cc
new file mode 100644 (file)
index 0000000..bc21220
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "debug_rep.h"
+
+#include <string>
+using std::string;
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       vector<int> v = {1,2,3,4,5,6,7,8,9};
+       string s("hi");
+       cout << debug_rep(v) << endl;
+       cout << debug_rep(s) << endl; 
+       cout << debug_rep("hi") << endl;
+       cout << debug_rep(&v[0]) << endl;
+       cout << debug_rep(&s) << endl;
+       const string *sp = &s;
+       cout << debug_rep(sp) << endl;
+
+       char carr[] = "bye";            // calls pointer version if no overloads
+       cout << debug_rep(carr) << endl;
+       vector<string> authors = {"Proust", "Shakespeare", "Barth"};
+       vector<const char*> authors2 = {"Proust", "Shakespeare", "Barth"};
+       cout << debug_rep(authors) << endl;
+       cout << debug_rep(authors2) << endl;
+       cout << debug_rep(s) << endl;
+       s += "more stuff";
+       cout << debug_rep(s) << endl;
+       s += "\\escape\"and quotes";
+       cout << debug_rep(s) << endl;
+
+       cout << debug_rep("hi world!") << endl; // calls debug_rep(T*)
+
+       s = "hi";
+       const char *cp = "bye";
+       char arr[] = "world";
+
+       cout << debug_rep(s) << endl;  // calls specialization debug_rep(const string&
+       cout << debug_rep(cp) << endl; // calls specialization debug_rep(const char*
+       cout << debug_rep(arr) << endl;// calls specialization debug_rep(char*
+       cout << debug_rep(&s) << endl; // calls template debug_rep(T*)
+
+       return 0;
+}
diff --git a/16/debug_rep.h b/16/debug_rep.h
new file mode 100644 (file)
index 0000000..757f409
--- /dev/null
@@ -0,0 +1,151 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef DEBUG_REP_H
+#define DEBUG_REP_H
+#include <iostream>
+#include <string>
+#include <vector>
+#include <sstream>
+
+/* this file uses two preprocessor variables to control whether
+ * we use nontemplate functions or specializations for string
+ * data and to control whether we define versions of debug_rep
+ * that handle character pointers.
+ *
+ * SPECIALIZED is defined, then we define specializations
+ *             for string, char*, and const char* and do not
+ *             define nontemplate functions taking these types
+ *
+ * OVERCHAR only matters if SPECIALIZED is not defined.  In this case
+ *          if OVERCHAR is defined, we define one nontemplate function
+ *          that takes a string; if OVERCHAR is not defined 
+ *          we also define nontemplate functions taking char* and
+ *          const char*
+*/
+#ifndef SPECIALIZED
+std::string debug_rep(const std::string &s);
+#ifndef OVERCHAR
+std::string debug_rep(char *p);
+std::string debug_rep(const char *cp);
+#endif
+#endif
+// overloaded, not specialized, function templates
+template <typename T> std::string debug_rep(const T &t);
+template <typename T> std::string debug_rep(T *p);
+template <typename T> std::string debug_rep(T b, T e);
+template <typename T> std::string debug_rep(const std::vector<T> &v);
+
+#ifdef SPECIALIZED
+// specialized versions to handle strings and character pointers
+// declarations for specializations should follow declarations for all overloaded templates
+template <> std::string debug_rep(const std::string&); 
+template <> std::string debug_rep(const char*);
+template <> std::string debug_rep(char*);
+#endif
+
+// print any type we don't otherwise handle
+template <typename T> std::string debug_rep(const T &t)
+{
+#ifdef DEBUG
+       std::cout << "const T&" << "\t";
+#endif
+       std::ostringstream ret; 
+       ret << t; // uses T's output operator to print a representation of t
+       return ret.str();  // return a copy of the string to which ret is bound
+}
+
+// print pointers as their pointer value 
+// followed by the object to which the pointer points
+// NB: this function will not work properly with char*
+template <typename T> std::string debug_rep(T *p)
+{
+#ifdef DEBUG
+       std::cout << "T*" << "\t";
+#endif
+       std::ostringstream ret;
+       ret << "pointer: " << p;         // print the pointer's own value
+       if (p)
+               ret << " " << debug_rep(*p); // print the value to which p points
+       else
+               ret << " null pointer";      // or indicate that the p is null
+       return ret.str(); // return a copy of the string to which ret is bound
+}
+#ifndef SPECIALIZED
+// print strings inside double quotes 
+std::string debug_rep(const std::string &s)
+#else
+template <> std::string debug_rep(const std::string &s)
+#endif
+{
+#ifdef DEBUG
+       std::cout << "const string &" << "\t";
+#endif
+       return '"' + s + '"';
+}
+
+
+#ifndef OVERCHAR
+// convert the character pointers to string and call the string version of debug_rep
+std::string debug_rep(char *p) 
+{
+       return debug_rep(std::string(p));
+}
+std::string debug_rep(const char *p) 
+{
+       return debug_rep(std::string(p));
+}
+#endif
+#ifdef SPECIALIZED
+template<> std::string debug_rep(char *p)
+       { return debug_rep(std::string(p)); }
+template <> std::string debug_rep(const char *cp)
+       { return debug_rep(std::string(cp)); }
+#endif
+
+template <typename T> std::string debug_rep(T b, T e)
+{
+       std::ostringstream ret;
+       for (T it = b; it != e; ++it) {
+               if (it != b)
+                       ret << ",";            // put comma before all but the first element
+               ret << debug_rep(*it);     // print the element
+       }
+       return ret.str();
+}
+
+template <typename T> std::string debug_rep(const std::vector<T> &v)
+{
+       std::ostringstream ret;
+       ret << "vector: [";
+       ret << debug_rep(v.begin(), v.end());
+       ret << "]";
+       return ret.str();
+}
+#endif
diff --git a/16/expansion.cc b/16/expansion.cc
new file mode 100644 (file)
index 0000000..0936a3f
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+template <typename T>
+T accum(const T &t)
+{
+       return t;
+}
+
+template <typename T, typename... Args>
+T accum(const T &t, Args... args)
+{
+       return t + accum(args...);
+}
+
+// h adds its variadic arguments to the starting value of 42
+int h()
+{
+       return 42; // starting point for the accumulation 
+}
+
+template <typename ...Args> int h(int t, Args ... args)
+{
+       return t + h(args...); // sum of all the values in args plus 42
+}
+
+// produces the sum of up to 4 integral values
+void f(int i, int j = 0, int k = 0, int l = 0)
+{
+       cout << i << " + " 
+            << j << " + " 
+            << k << " + " 
+            << l << " =  "  
+            << i + j + k + l << endl;
+}
+
+// expansion: applies the pattern to each member of the pack
+//            using a separator appropriate to the context
+template<typename ... Args> void g(Args ... args) {
+       cout << sizeof...(Args) << endl;  // number of type parameters
+       cout << sizeof...(args) << endl;  // number of function parameters
+       // call f passing it the arguments from args
+       f(args...);                // f(a1, a2, a3, ..., an)
+
+       // call h passing it the arguments from args
+       cout << h(args...) << endl; // h(a1, a2, a3, ..., an)
+
+       // the pattern is h(x), 
+       // the expansion calls h on each argument in args
+       f(h(args) ...);            // f(h(a1), h(a2), h(a3), ..., h(an))
+
+       // args is expanded in the call to h 
+       f(h(args ...));            // f(h(a1, a2, a3, ..., an2)
+
+       // pattern adds the argument value to result from calling h(5,6,7,8) 
+       f(h(5,6,7,8 ) + args ...); // f(h(5,6,7,8) + a1, h(5,6,7,8) + a2,
+                                  //   h(5,6,7,8) + a3, ..., h(5,6,7,8) + an)
+}
+
+int main()
+{
+       cout << accum(1,2,3,4) << endl;
+
+       g(1,2,3,4);
+
+       return 0;
+}
diff --git a/16/flip.cc b/16/flip.cc
new file mode 100644 (file)
index 0000000..ccef118
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <utility>
+#include <iostream>
+using std::cout; using std::endl;
+
+// template that takes a callable and two parameters 
+// and calls the given callable with the parameters ``flipped'' 
+template <typename F, typename T1, typename T2>
+void flip(F f, T1 &&t1, T2 &&t2)
+{
+       f(std::forward<T2>(t2), std::forward<T1>(t1));
+}
+
+void f(int v1, int &v2)  // note v2 is a reference
+{
+       cout << v1 << " " << ++v2 << endl;
+}
+
+void g(int &&i, int& j)
+{
+       cout << i << " " << j << endl;
+}
+
+// flip1 is an incomplete implementation: top-level const and references are lost
+template <typename F, typename T1, typename T2>
+void flip1(F f, T1 t1, T2 t2)
+{
+       f(t2, t1);
+} 
+
+template <typename F, typename T1, typename T2>
+void flip2(F f, T1 &&t1, T2 &&t2)
+{
+       f(t2, t1);
+}
+
+int main()
+{
+       int i = 0, j = 0, k = 0, l = 0;
+       cout << i << " " << j << " " << k << " " << l << endl;
+
+       f(42, i);        // f changes its argument i
+       flip1(f, j, 42); // f called through flip1 leaves j unchanged
+       flip2(f, k, 42); // ok: k is changed
+       g(1, i);
+       flip(g, i, 42);  // ok: rvalue-ness of the third argument is preserved
+       cout << i << " " << j << " " << k << " " << l << endl;
+
+       return 0;
+}
diff --git a/16/makefile b/16/makefile
new file mode 100644 (file)
index 0000000..53b05b5
--- /dev/null
@@ -0,0 +1,50 @@
+# executable files for this directory
+OBJECTS = Array.exe array-parms4.exe \
+                 compare.exe compareDef.exe compareSpec.exe \
+                 debug.exe expansion.exe flip.exe multiset.exe \
+                 overloaded.exe overSpec.exe qm.exe \
+                 SP.exe special.exe specialized.exe \
+                 trail-ret.exe typealias.exe \
+                 useBlob.exe useChcking.exe \
+                 variadic.exe variadic3.exe Vecmain.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = 
+
+SP.o: DebugDelete.h
+
+compareDef.o multiset.o variadic.o variadic3.o Sales_data.o: Sales_data.h
+
+Vecmain.o: Vec.h
+
+compareDef.exe: compareDef.o Sales_data.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) compareDef.o Sales_data.o \
+                       -o compareDef.exe
+
+variadic.exe: variadic.o Sales_data.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) variadic.o Sales_data.o \
+                       -o variadic.exe
+
+variadic3.exe: variadic3.o Sales_data.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) variadic3.o Sales_data.o \
+                       -o variadic3.exe
+
+multiset.exe: multiset.o Sales_data.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) multiset.o Sales_data.o \
+                       -o multiset.exe
+
+qm.exe: qm.o build.o 
+       $(CC) $(CCFLAGS) $(LOCFLAGS) qm.o build.o -o qm.exe
+
+specialized.exe: debug.cc
+       $(CC) -D SPECIALIZED $(CCFLAGS) $(LOCFLAGS) debug.cc -o specialized.exe
+
+overloaded.exe: debug.o
+       $(CC) -D OVERCHAR $(CCFLAGS) $(LOCFLAGS) debug.cc -o overloaded.exe
+
+debug.exe: debug.o
+       $(CC) -D DEBUG $(CCFLAGS) $(LOCFLAGS) debug.cc -o debug.exe
+
diff --git a/16/multiset.cc b/16/multiset.cc
new file mode 100644 (file)
index 0000000..41c8683
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <unordered_set>
+using std::unordered_multiset;
+
+#include <functional>
+
+#include "Sales_data.h"
+
+using std::hash;
+
+int main()
+{
+       // uses hash<Sales_data> and Sales_data operator== 
+       unordered_multiset<Sales_data> SDset;   
+       Sales_data item;
+       while (cin >> item) {
+               SDset.insert(item);
+       }
+       cout << SDset.size() << endl;
+
+       return 0;
+}
diff --git a/16/overSpec.cc b/16/overSpec.cc
new file mode 100644 (file)
index 0000000..2e2c431
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstring>
+using std::strcmp;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+// first version; can compare any two types
+template <typename T> int compare(const T&, const T&);
+
+// second version to handle string literals 
+template<size_t N, size_t M>
+int compare(const char (&)[N], const char (&)[M]);
+
+// specialized verson of the first template, handles character arrays
+template <>
+int compare(const char* const &, const char* const &);
+
+template <typename T> int compare(const T& v1, const T& v2)
+{
+cout << "base template" << endl;
+    if (v1 < v2) return -1;
+    if (v2 < v1) return 1;
+    return 0;
+}
+
+template<size_t N, size_t M>
+int compare(const char (&p1)[N], const char (&p2)[M])
+{
+cout << "array template" << endl;
+       return strcmp(p1, p2);
+}
+
+
+// special version of compare to handle pointers to character arrays
+template <>
+int compare(const char* const &p1, const char* const &p2)
+{
+cout << "specialized template" << endl;
+    return strcmp(p1, p2);
+}
+
+int main()
+{
+       const char *p1 = "hi", *p2 = "mom";
+       compare(p1, p2);      // calls the first template
+       compare("hi", "mom"); // calls the template with two nontype parameters
+}
diff --git a/16/qm.cc b/16/qm.cc
new file mode 100644 (file)
index 0000000..165e8e0
--- /dev/null
+++ b/16/qm.cc
@@ -0,0 +1,54 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "compare.h"
+#include "Blob.h"
+
+#include <string>
+using std::string;
+
+// Application.cc
+// these template types must be instantiated elsewhere in the program
+// instantion declaration and definition
+extern template class Blob<string>;  
+extern template int compare(const int&, const int&);
+
+int main() {
+
+       Blob<string> sa1, sa2; // instantiation will appear elsewhere
+
+       // Blob<int> and its initializer_list constructor 
+       // are instantiated in this file
+       Blob<int> a1 = {0,1,2,3,4,5,6,7,8,9}; 
+       Blob<int> a2(a1);  // copy constructor instantiated in this file
+
+       int i = compare(a1[0], a2[0]); // instantiation will appear elsewhere
+
+       return 0;
+}
diff --git a/16/runpgms b/16/runpgms
new file mode 100644 (file)
index 0000000..86c6019
--- /dev/null
@@ -0,0 +1,12 @@
+echo "compareDef: " && ./compareDef.exe < data/book_sales
+echo "multiset: " && ./multiset.exe < data/book_trans
+echo "Vecmain: " && ./Vecmain.exe < data/Vecmain
+
+../noinput Array.exe array-parms4.exe \
+                  compare.exe compareSpec.exe \
+                  debug.exe flip.exe expansion.exe \
+                  overloaded.exe overSpec.exe qm.exe \
+                  SP.exe special.exe specialized.exe \
+                  trail-ret.exe typealias.exe \
+                  useBlob.exe useChcking.exe \
+                  variadic.exe variadic3.exe 
diff --git a/16/special.cc b/16/special.cc
new file mode 100644 (file)
index 0000000..82c8ff5
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+// declarations before definitions
+template <typename T> void f(T);
+template <typename T> void f(const T*);
+
+template <typename T> void g(T);
+template <typename T> void g(T*);
+
+// definitions
+template <typename T> void f(T) { cout << "f(T)" << endl; }
+template <typename T> void f(const T*) 
+{ cout << "f(const T*)" << endl; }
+
+template <typename T> void g(T) { cout << "g(T)" << endl; }
+template <typename T> void g(T*) { cout << "g(T*)" << endl; }
+
+int main()
+{
+       int i = 42;
+       f(i);  // calls f(T), f(const T*) isn't viable
+       
+       f(&i); // calls f(T), which is an exact match, 
+              // f(const T*) requires a conversion 
+       
+       g(i);  // calls g(T), g(T*) isn't viable
+       g(&i); // calls g(T*), both templates are viable, 
+              // but g(T*) is more specialized 
+       
+       int *p = &i;
+       const int ci = 0, *p2 = &ci;
+       g(42);   g(p);   g(ci);   g(p2);
+       f(42);   f(p);   f(ci);   f(p2);
+
+       return 0;
+}
diff --git a/16/trail-ret.cc b/16/trail-ret.cc
new file mode 100644 (file)
index 0000000..c90f946
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <type_traits>
+using std::remove_reference;
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+#include "Blob.h"
+
+// auto as function return type indicates we're using a trailing return type
+// function that returns a reference to an element in the range
+template <typename It>
+auto reffcn(It beg, It end) -> decltype(*beg)
+{
+    // process the range
+    return *beg;  // return a copy of an element from the range
+}
+
+// function that returns an element in the range by value
+// must use typename to use a type member of a template parameter
+template <typename It>
+auto valfcn(It beg, It end) -> 
+       typename remove_reference<decltype(*beg)>::type
+{
+    // process the range
+    return *beg;  // return a copy of an element from the range
+}
+
+int main()
+{
+       vector<int> vi = {1,2,3,4,5};
+       Blob<string> ca = { "hi", "bye" };
+
+       auto &i = reffcn(vi.begin(), vi.end()); // reffcn should return int&
+       auto &s = reffcn(ca.begin(), ca.end()); // reffcn should return string&
+
+       vi = {1,2,3,4};
+       for (auto i : vi) cout << i << " "; cout << endl;
+       auto &ref = reffcn(vi.begin(), vi.end());  // ref is int&
+       ref = 5; // changes the value of *beg to which ref refers
+       for (auto i : vi) cout << i << " "; cout << endl;
+
+       auto val = valfcn(vi.begin(), vi.end()); // val is int
+       cout << val << endl;  // will print 5
+       
+       return 0;
+}
+
diff --git a/16/typealias.cc b/16/typealias.cc
new file mode 100644 (file)
index 0000000..8f396fc
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Blob.h"
+
+#include <utility>
+using std::pair;
+
+#include <string>
+using std::string;
+
+typedef Blob<string> StrBlob; 
+
+
+template<typename T> using twin = pair<T, T>;
+
+template <typename T> using partNo = pair<T, unsigned>;
+
+int main()
+{
+       // authors is a pair<string, string>
+       twin<string> author("Mark", "Twain");
+       
+       twin<int> win_loss(2, 45);  // win_loss is a pair<int, int>
+       
+       typedef string Vehicle;
+       partNo<Vehicle> car("volvo", 242);  // car is a pair<Vehicle, unsigned>
+
+       return 0;
+}
diff --git a/16/useBlob.cc b/16/useBlob.cc
new file mode 100644 (file)
index 0000000..76b7e69
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include "Blob.h"
+
+int main()
+{
+       Blob<string> b1; // empty Blob
+       cout << b1.size() << endl;
+       {  // new scope
+               Blob<string> b2 = {"a", "an", "the"};
+               b1 = b2;  // b1 and b2 share the same elements
+               b2.push_back("about");
+               cout << b1.size() << " " << b2.size() << endl;
+       } // b2 is destroyed, but the elements it points to must not be destroyed
+       cout << b1.size() << endl;
+       for(auto p = b1.begin(); p != b1.end(); ++p)
+               cout << *p << endl;
+       
+       return 0;
+}
diff --git a/16/useChcking.cc b/16/useChcking.cc
new file mode 100644 (file)
index 0000000..ce4b55d
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Blob.h"
+#include <string>
+using std::string;
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       vector<int> v1(3, 43), v2(10);
+       Blob<int> a1(v1.begin(), v1.end()),
+                 a2 = {0,1,2,3,4,5,6,7,8,9},
+                 a3(v2.begin(), v2.end());
+       
+       cout << a1 << "\n\n" << a2 << "\n\n" << a3 << endl;
+       
+       cout << "\ncopy" << "\n\n";
+       Blob<int> a5(a1); 
+       cout << a5 << endl;
+       
+       cout << "\nassignment" << "\n\n";
+       
+       a1 = a3;
+       cout << a1 << "\n\n" << a2 << "\n\n" << a3 << endl;
+       
+       cout << "\nelement assignment" << "\n\n";
+       a1[0] = 42;
+       a1[a1.size() - 1] = 15;
+       cout << a1 << "\n\n" << a3 << endl;
+       
+       Blob<string> s1 = {"hi", "bye", "now"};
+       BlobPtr<string> p(s1);    // p points to the vector inside s1
+       *p = "okay";                 // assigns to the first element in s1
+       cout << p->size() << endl;   // prints 4, the size of the first element in s1
+       cout << (*p).size() << endl; // equivalent to p->size()
+       
+       Blob<string> s2{"one", "two", "three"};
+       // run the string empty function in the first element in s2
+       if (s2[0].empty())   
+           s2[0] = "empty"; // assign a new value to the first string in s2
+       
+       cout << a1 << endl;
+       cout << a2 << endl;
+       a2.swap(a1);
+       cout << a1 << endl;
+       cout << a2 << endl;
+
+       return 0;
+}
+       
diff --git a/16/variadic.cc b/16/variadic.cc
new file mode 100644 (file)
index 0000000..84edca1
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cerr;
+using std::ostream; using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <map>
+using std::map;
+
+#include <cstddef>
+using std::size_t;
+
+#include "Sales_data.h"
+#include "debug_rep.h"
+
+// function to end the recursion and print the last element
+template<typename T>
+ostream &print(ostream &os, const T &t)
+{
+    return os << t; // no separator after the last element in the pack
+}
+
+template <typename T, typename... Args>                 
+ostream &
+print(ostream &os, const T &t, const Args&... rest)//expand Args
+{
+    os << t << ", ";
+    return print(os, rest...);                     //expand rest
+}
+
+// call debug_rep on each argument in the call to print
+template <typename... Args> 
+ostream &errorMsg(ostream &os, const Args&... rest)
+{
+       // print(os, debug_rep(a1), debug_rep(a2), ..., debug_rep(an)
+       return print(os, debug_rep(rest)...); 
+}
+
+
+struct ErrorCode {
+       ErrorCode(size_t n = 0): e(n) { }
+       size_t e;
+       size_t num() const { return e; }
+       string lookup() const { return errors[e]; }
+       static map<size_t, string> errors;
+};
+
+map<size_t, string>
+ErrorCode::errors = { {42, "some error"}, { 1024, "another error"} };
+
+int main()
+{
+       Sales_data item("978-0590353403", 25, 15.99);
+       string fcnName("itemProcess");
+       ErrorCode code(42);
+       string otherData("invalid ISBN");
+
+       errorMsg(cerr, fcnName, code.num(), otherData, "other", item);
+       cerr << endl;
+
+       print(cerr, debug_rep(fcnName), debug_rep(code.num()), 
+                   debug_rep(otherData), debug_rep("otherData"), 
+                   debug_rep(item));
+       cerr << endl;
+       
+       return 0;
+}
+
diff --git a/16/variadic3.cc b/16/variadic3.cc
new file mode 100644 (file)
index 0000000..471d568
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <initializer_list>
+using std::initializer_list;
+
+#include <iostream>
+using std::ostream; using std::cout; using std::endl;
+
+#include <algorithm>
+using std::min;
+
+#include <string>
+using std::string;
+
+#include "Sales_data.h"
+
+// function to end the recursion and print the last element
+// this function must be declared before the variadic version of print is defined
+template<typename T> 
+ostream &print(ostream &os, const T &t)
+{
+       return os << t; // no separator after the last element in the pack
+}
+
+// this version of print will be called for all but the last element in the pack
+
+template <typename T, typename... Args> 
+ostream &print(ostream &os, const T &t, const Args&... rest)
+{
+       os << t << ", ";           // print the first argument
+       return print(os, rest...); // recursive call; print the other arguments
+}
+
+template <typename T> bool bigger(const T &t, initializer_list<T> il)
+{
+       if (il.size() == 0)
+               return false;
+       auto b = il.begin();
+       while (b != il.end())
+               if (t < *b)
+                       return true;
+               else
+                       ++b;
+       return false;
+}
+
+// NB: elements in the list must all have the same type
+template <typename T> T min_elem(initializer_list<T> il)
+{
+       if (il.size() == 0)
+               throw "empty";
+       auto b = il.begin();      // we know there's at least one element in the list
+       T ret(*b++);              // start off with the first element as the ``smallest'' 
+       while (b != il.end())     // till we run out of elements
+               ret = min(ret, *b++); // update ret if we find a smaller value
+       return ret;               // return the value we found
+}
+
+// using variadic templates we can allow for conversions among the elements
+// function to end the recursion, called when we have only one element left from the original pack
+template<typename T> T min_elem(const T &v)
+{
+       return v;
+}
+
+template <typename T, typename... Back>
+T min_elem(T val, Back... back)
+{
+       // recursive call, "pops" the first element from back, which will be val in this call
+       T val2 = min_elem(back...); 
+
+       // requires that the types of val2 and val are comparable using <
+       return val < val2 ? val : val2;   
+}
+
+int main()
+{
+       // no braces, so calls variadic version of min_elem
+       cout << min_elem(1.0,2,3,4.5,0.0,5,6,7,8,9) << endl;
+       // calls min_element that takes a single argument 
+       // of type initializer_list<int>
+       cout << min_elem({1,2,3,4,0,5,6,7,8,9}) << endl;
+
+       int i = 5;
+       cout << bigger(i, {1,2,3,4,0,5,6,7,8,9}) << endl;
+
+       string s = "how now brown cow";
+       print(cout, i, s, 42);  // two parameters in the pack
+       print(cout, i, s);      // one parameter in the pack
+       print(cout, i);         // no parameters in the pack
+       cout << endl;
+
+       return 0;
+}
+
diff --git a/17/README b/17/README
new file mode 100644 (file)
index 0000000..dc6a088
--- /dev/null
+++ b/17/README
@@ -0,0 +1,18 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   bits             bits      
+   game             game
+   no-skipws        no-skipws
+   skipws           getc
+   skipws           badgetc  
+   skipws           getput
+   skipws           skipws
+   store1,store2,   
+   store3,store4    findbook
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/17/REerrs.cc b/17/REerrs.cc
new file mode 100644 (file)
index 0000000..358348a
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Version_test.h"
+
+// if the regular expression library isn't support, do nothing
+#ifdef REGEX
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <regex>
+using std::regex; using std::sregex_iterator; using std::smatch;
+using std::regex_error;
+
+int main()
+{
+       try {
+               // missing close bracket after alnum; the constructor will throw 
+               regex r("[[:alnum:]+\\.(cpp|cxx|cc)$", regex::icase); 
+       } catch (regex_error e) 
+            { cout << e.what() << "\ncode: " << e.code() << endl; }
+
+       return 0;
+}
+#else
+
+// do nothing
+int main() { return 0; }
+
+#endif
+
diff --git a/17/badgetc.cc b/17/badgetc.cc
new file mode 100644 (file)
index 0000000..65cebba
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstdio> // for EOF
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       char ch;    // using a char here invites disaster!
+
+       // the return from cin.get is converted to char 
+       // and then compared to an int
+       while ((ch = cin.get()) != EOF)
+               cout.put(ch);
+       cout << endl;
+
+       return 0;
+}
diff --git a/17/bits.cc b/17/bits.cc
new file mode 100644 (file)
index 0000000..17378af
--- /dev/null
@@ -0,0 +1,181 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+#include <iostream>
+#include <string>
+using std::cout; using std::cin;
+using std::endl;
+using std::string;
+using std::size_t;
+
+#include <bitset>
+using std::bitset;
+int main() 
+{
+       bitset<32> bitvec(1U); // 32 bits; low-order bit is 1, 
+                              // remaining bits are 0
+       bool is_set = bitvec.any();      // true, one bit is set
+       bool is_not_set = bitvec.none(); // false, one bit is set
+       bool all_set = bitvec.all();     // false, only one bit is set
+       size_t onBits = bitvec.count();  // returns 1
+       size_t sz = bitvec.size();       // returns 32
+       
+       bitvec.flip();     // reverses the value of all the bits in bitvec
+       bitvec.reset();    // sets all the bits to 0
+       bitvec.set();      // sets all the bits to 1
+       
+       cout << "bitvec: " << bitvec << endl;
+       
+       sz = bitvec.size();  // returns 32
+       
+       onBits = bitvec.count();  // returns 1, 
+                              // i.e., the number of bits that are on
+       
+       // assign 1 to even numbered bits
+       for (int index = 0; index != 32; index += 2)
+               bitvec[index] = 1; 
+
+       // equivalent loop using set operation
+       for (int index = 0; index != 32; index += 2)
+               bitvec.set(index); 
+       
+       // bitvec is unchanged
+       auto b2 = ~bitvec;  // b2 is a copy of bitvec with every bit flipped
+
+       // assign value of last bit in bitvec to the first bit in b2
+       b2[0] = bitvec[bitvec.size() - 1]; 
+       bitvec[0] = 0;          // turn off the bit at position 0
+       bitvec[31] = bitvec[0]; // give last bit the same value as the first
+       bitvec[0].flip();       // flip the value of the bit at position 0
+       ~bitvec[0];             // equivalent; flips the bit at position 0
+       bool b = bitvec[0];     // convert the value of bitvec[0] to bool
+       b2[0] = ~bitvec[0];     // first bit in b2 has the opposite value 
+                            // of the first bit in bitvec
+       
+       unsigned i = 0;
+       if (bitvec.test(i))
+            // bitvec[i] is on
+               ;
+       
+       //equivalent test using subscript
+       if (bitvec[i])
+            // bitvec[i] is on
+               ;
+       
+       cout << "bitvec: positions turned on:\n\t";
+       for (int index = 0; index != 32; ++index)
+               if (bitvec[index])
+               cout << index << " ";
+       cout << endl;
+       
+       // equivalent; turn off first bit
+       bitvec.flip(0);   // reverses the value of the first bit
+       bitvec.set(bitvec.size() - 1);  // turns on the last bit
+       bitvec.set(0, 0); // turns off the first bit
+       bitvec.reset(i);  // turns off the ith bit 
+       bitvec.test(0);   // returns false because the first bit is off
+       bitvec[0] = 0;
+       
+       bitvec.flip(0);   // reverses value of first bit
+       bitvec[0].flip(); // also reverses the first bit
+       
+       cout << "new inits" <<endl;
+       // bits13 is smaller than the initializer; 
+       // high-order bits from the initializer are discarded
+       bitset<13> bits13(0xbeef);  // bits are 1111011101111 
+
+       // bits20 is larger than the initializer; 
+       // high-order bits in bits20 are set to zero
+       bitset<20> bits20(0xbeef);  // bits are 00001011111011101111
+
+       // on machines with 64-bit long long 0ULL is 64 bits of 0, 
+       // so ~0ULL is 64 ones, so 0 ... 63 are one, and 64 ... 127 are zero
+    // if long long has 32 bits, 0 ... 32 are one, 33 ... 127 are zero
+       bitset<128> bits128(~0ULL); 
+       cout << "bits13: " << bits13 << endl;
+       cout << "bits20: " << bits20 << endl;
+       cout << "bits128: " << bits128 << endl;
+       cout << "bits20[0] " << bits20[0] << endl;
+       cout << "bits20[19] " << bits20[19] << endl;
+       
+       // bitvec1 is smaller than the initializer
+       bitset<32> bitvec1(0xffff);  // bits 0 ... 15 are set to 1; 
+                                    // 16 ... 31 are 0
+       
+       // bitvec2 same size as initializer
+       bitset<64> bitvec2(0xffff);  // bits 0 ... 15 are set to 1; 
+                                    // 16 ... 63 are 0
+       
+       // assuming 64-bit long long, bits 0 to 63 initialized from 0xffff
+       bitset<128> bitvec3(0xffff); // bits 32 through 127 are zero
+       
+       cout << "bitvec1: " << bitvec1 << endl;
+       cout << "bitvec2: " << bitvec2 << endl;
+       cout << "bitvec2[0] " << bitvec2[0] << endl;
+       cout << "bitvec2[31] " << bitvec2[31] << endl;
+       
+       cout << "bitvec3: " << bitvec3 << endl;
+       
+       
+       bitset<32> bitvec4("1100"); // bits 2 and 3 are 1, all others are 0
+       
+       cout << "strval: " << "1100" << endl;
+       cout << "bitvec4: " << bitvec4 << endl;
+       
+       string str("1111111000000011001101");
+       bitset<32> bitvec5(str, 5, 4); // four bits starting at str[5], 1100
+       bitset<32> bitvec6(str, str.size()-4); // use last four characters 
+       
+       cout << "str: " << str << endl;
+       cout << "bitvec5: " << bitvec5 << endl;
+       
+       cout << "str: " << str << endl;
+       cout << "bitvec6: " << bitvec6 << endl;
+       
+       unsigned long ulong = bitvec3.to_ulong();
+       cout << "ulong = " << ulong << endl;
+       
+       bitset<32> bitvec7 = bitvec1 & bitvec4;
+       
+       cout << "bitvec7: " << bitvec7 << endl;
+       
+       bitset<32> bitvec8 = bitvec1 | bitvec4;
+       
+       cout << "bitvec8: " << bitvec8 << endl;
+       
+       bitset<16> chk("111100110011001100000");
+       cout << "chk: " << chk << endl;
+       
+       bitset<16> bits;
+       cin >> bits;  // read up to 16 1 or 0 characters from cin
+       cout << "bits: " << bits << endl; // print what we just read
+
+       return 0;
+}
diff --git a/17/ccpgms.cc b/17/ccpgms.cc
new file mode 100644 (file)
index 0000000..4fced61
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Version_test.h"
+
+// if the regular expression library isn't support, do nothing
+#ifdef REGEX
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <regex>
+using std::regex; using std::sregex_iterator; using std::smatch;
+using std::regex_error;
+
+int main()
+{
+       try {
+               // one or more alphanumeric characters followed by a '.' 
+               // followed by "cpp" or "cxx" or "cc"
+               regex r("[[:alnum:]]+\\.(cpp|cxx|cc)$", regex::icase);
+
+               smatch results; // object to hold details about the match
+               string filename;
+               while (cin >> filename) 
+                       if (regex_search(filename, results, r))
+                               cout << results.str() << endl;  // print the match
+       } catch (regex_error e) 
+               { cout << e.what() << " " << e.code() << endl; }
+
+       return 0;
+}
+
+#else
+
+// do nothing
+int main() { return 0; }
+#endif
diff --git a/17/data/badgetc b/17/data/badgetc
new file mode 100644 (file)
index 0000000..e4dd415
--- /dev/null
@@ -0,0 +1,3 @@
+abc
+ÿ
+def
diff --git a/17/data/bits b/17/data/bits
new file mode 100644 (file)
index 0000000..9db0de3
--- /dev/null
@@ -0,0 +1 @@
+11110001
diff --git a/17/data/copyOut b/17/data/copyOut
new file mode 100644 (file)
index 0000000..15a1117
--- /dev/null
@@ -0,0 +1,9 @@
+abcd
+efg
+hi
+j
+5 9 12 14
+5 8 10 11 20
+5 7 8 18 30
+5 6 8 15 26 37
+5 9 12 14 24 37 49 64\r
diff --git a/17/data/files b/17/data/files
new file mode 100644 (file)
index 0000000..81410da
--- /dev/null
@@ -0,0 +1,35 @@
+badgetc.cpp
+bits.cpp
+ccpgms.cpp
+findbook.cpp
+findDups.cpp
+findDupsV2.cpp
+findgerunds.cpp
+game.cpp
+getc.cpp
+getput.cpp
+manips.cpp
+manips11.cpp
+normalInts.cpp
+no-skipws.cpp
+phones.cpp
+phones2.cpp
+phonesFinal.cpp
+play.cpp
+point.cpp
+prec.cpp
+rand1.cpp
+rand2.cpp
+rand4.cpp
+rand6.cpp
+REerrs.cpp
+regex1.cpp
+sci_dec.cpp
+seed.cpp
+seek.cpp
+setw.cpp
+skipws.cpp
+subexpr.cpp
+tuple.cpp
+validatePhones.cpp
+zips.cpp
diff --git a/17/data/findDups b/17/data/findDups
new file mode 100644 (file)
index 0000000..cd3e880
--- /dev/null
@@ -0,0 +1 @@
+Some some repeated words words
diff --git a/17/data/findDupsV2 b/17/data/findDupsV2
new file mode 100644 (file)
index 0000000..15827af
--- /dev/null
@@ -0,0 +1 @@
+some some input that has has some dups
diff --git a/17/data/findgerunds b/17/data/findgerunds
new file mode 100644 (file)
index 0000000..9e0510f
--- /dev/null
@@ -0,0 +1,4 @@
+when i was going to saint ives
+when going to saint ives
+i was going when i went
+i was when going
diff --git a/17/data/game b/17/data/game
new file mode 100644 (file)
index 0000000..837293b
--- /dev/null
@@ -0,0 +1 @@
+y y y y y y y y y y y y y q
diff --git a/17/data/no-skipws b/17/data/no-skipws
new file mode 100644 (file)
index 0000000..8c9edd3
--- /dev/null
@@ -0,0 +1,2 @@
+a b       c
+d
diff --git a/17/data/phones b/17/data/phones
new file mode 100644 (file)
index 0000000..9f124fb
--- /dev/null
@@ -0,0 +1,3 @@
+morgan (201) 555-0168 862-555-0123 
+drew (973)555.0130 
+lee (609) 555-0132 2015550175 800.555-0110
diff --git a/17/data/phones2 b/17/data/phones2
new file mode 100644 (file)
index 0000000..1292977
--- /dev/null
@@ -0,0 +1,3 @@
+morgan (201) 555-0168 (862) 555-0123 
+drew (973) 555-0130 
+lee (609) 555-0132 (201) 555-0175 (800) 555-0110
diff --git a/17/data/phonesFinal b/17/data/phonesFinal
new file mode 100644 (file)
index 0000000..cdb3da8
--- /dev/null
@@ -0,0 +1,11 @@
+(908)555.0136 
+(908.555.0136 
+908).555.0136
+908 555.0136
+908-555.0136
+908 555 0136
+908.555.0136
+908-555-0136
+morgan (201) 555-0168 862-555-0123 
+drew (973)555.0130 
+lee (609) 555-0132 2015550175 800.555-0110
diff --git a/17/data/skipws b/17/data/skipws
new file mode 100644 (file)
index 0000000..8c9edd3
--- /dev/null
@@ -0,0 +1,2 @@
+a b       c
+d
diff --git a/17/data/store1 b/17/data/store1
new file mode 100644 (file)
index 0000000..0d14afe
--- /dev/null
@@ -0,0 +1,10 @@
+0-201-70353-X 4 24.99
+0-201-82470-1 4 45.39
+0-201-88954-4 2 15.00 
+0-201-88954-4 5 12.00 
+0-201-88954-4 7 12.00 
+0-201-88954-4 2 12.00 
+0-399-82477-1 2 45.39
+0-399-82477-1 3 45.39
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/17/data/store2 b/17/data/store2
new file mode 100644 (file)
index 0000000..4224fa4
--- /dev/null
@@ -0,0 +1,8 @@
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
+0-999-78345-X 3 20.00
+0-201-78345-X 2 25.00
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
+0-999-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/17/data/store3 b/17/data/store3
new file mode 100644 (file)
index 0000000..0d14afe
--- /dev/null
@@ -0,0 +1,10 @@
+0-201-70353-X 4 24.99
+0-201-82470-1 4 45.39
+0-201-88954-4 2 15.00 
+0-201-88954-4 5 12.00 
+0-201-88954-4 7 12.00 
+0-201-88954-4 2 12.00 
+0-399-82477-1 2 45.39
+0-399-82477-1 3 45.39
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/17/data/store4 b/17/data/store4
new file mode 100644 (file)
index 0000000..0d14afe
--- /dev/null
@@ -0,0 +1,10 @@
+0-201-70353-X 4 24.99
+0-201-82470-1 4 45.39
+0-201-88954-4 2 15.00 
+0-201-88954-4 5 12.00 
+0-201-88954-4 7 12.00 
+0-201-88954-4 2 12.00 
+0-399-82477-1 2 45.39
+0-399-82477-1 3 45.39
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/17/data/validatePhones b/17/data/validatePhones
new file mode 100644 (file)
index 0000000..0545ae7
--- /dev/null
@@ -0,0 +1,8 @@
+(908)555.0106 
+(908.555.0106 
+908).555.0106
+908 555.0106
+908-555.0106
+908 555 0106
+908.555.0106
+908-555-0106
diff --git a/17/findbook.cc b/17/findbook.cc
new file mode 100644 (file)
index 0000000..84dd13c
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cassert>
+#include <utility>
+using std::pair;
+
+#include <string>
+using std::string; 
+
+#include <tuple>
+using std::tuple; using std::get; 
+using std::make_tuple;
+
+#include <vector>
+using std::vector;
+
+#include <numeric>
+using std::accumulate; 
+
+#include <algorithm>
+using std::equal_range; 
+
+#include <exception>
+#include <stdexcept>
+using std::domain_error;
+
+#include <iostream>
+using std::ostream; using std::istream;
+using std::cout; using std::endl; 
+
+#include <fstream>
+using std::ifstream; 
+
+#include "Sales_data.h"
+
+bool lt(const Sales_data &lhs, const Sales_data &rhs)
+{
+       return lhs.isbn() < rhs.isbn();
+}
+
+// need to leave this for as a traditional for loop because we
+// use the iterator to compute an index
+// matches has three members: an index of a store and iterators into that store's vector
+typedef tuple<vector<Sales_data>::size_type,
+              vector<Sales_data>::const_iterator,
+              vector<Sales_data>::const_iterator> matches;
+
+// files holds the transactions for every store 
+// findBook returns a vector with an entry for each store that sold the given book
+vector<matches>
+findBook(const vector<vector<Sales_data>> &files, 
+         const string &book)
+{
+       vector<matches> ret;  // initially empty
+       // for each store find the range of matching books, if any
+       for (auto it = files.cbegin(); it != files.cend(); ++it) {
+               // find the range of Sales_data that have the same ISBN
+               auto found = equal_range(it->cbegin(), it->cend(), 
+                                        book, compareIsbn);
+               if (found.first != found.second)  // this store had sales
+                       // remember the index of this store and the matching range 
+                       ret.push_back(make_tuple(it - files.cbegin(), 
+                                  found.first, found.second));
+       }
+       return ret; // empty if no matches found
+}
+
+vector<Sales_data> build_store(const string &s)
+{
+       Sales_data item;
+       vector<Sales_data> ret;
+       ifstream is(s);
+       while (read(is, item))
+               ret.push_back(item);
+       sort (ret.begin(), ret.end(), lt);  // need sort for equal_range to work
+       return ret;
+}
+
+void reportResults(istream &in, ostream &os,
+                   const vector<vector<Sales_data>> &files)
+{
+       string s;   // book to look for
+       while (in >> s) {
+               auto trans = findBook(files, s);  // stores that sold this book
+               if (trans.empty()) {
+                       cout << s << " not found in any stores" << endl;
+                       continue;  // get the next book to look for
+               }
+               for (const auto &store : trans)   // for every store with a sale
+                       // get<n> returns the specified member from the tuple in store
+                       os << "store " << get<0>(store) << " sales: "
+                          << accumulate(get<1>(store), get<2>(store), 
+                                        Sales_data(s))
+                          << endl;
+       }
+}
+
+int main(int argc, char **argv)
+{
+       assert(argc > 1);
+       // each element in files holds the transactions for a particular store
+       vector<vector<Sales_data>> files;
+       for (int cnt = 1; cnt != argc; ++cnt)
+               files.push_back(build_store(argv[cnt]));
+
+       ifstream in("../data/findbook.in");  // ISBNs to search for
+       reportResults(in, cout, files);
+}
diff --git a/17/game.cc b/17/game.cc
new file mode 100644 (file)
index 0000000..1b8a156
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <random>
+using std::default_random_engine;
+using std::bernoulli_distribution;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+bool play(bool player) { return player; }
+int main()
+{
+       string resp;
+       // distributions and engines have state,
+    // they must be defined outside the loop!
+       default_random_engine e;  
+
+       // 50/50 odds by default
+       bernoulli_distribution b(0.55); // give the house a slight edge
+
+       do {
+               bool first = b(e);    // if true, the program will go first
+               cout << (first ? "We go first" 
+                          : "You get to go first") << endl;
+               // play the game passing the indicator of who goes first
+               cout << ((play(first)) ? "sorry, you lost" 
+                                  : "congrats, you won") << endl;
+               cout << "play again? Enter 'yes' or 'no'" << endl;
+       } while (cin >> resp && resp[0] == 'y');
+
+       return 0;
+}
diff --git a/17/getc.cc b/17/getc.cc
new file mode 100644 (file)
index 0000000..a22108f
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <cstdio> // for EOF
+
+int main()
+{
+       int ch;    // use an int, not a char to hold the return from get()
+
+       // loop to read and write all the data in the input
+       while ((ch = cin.get()) != EOF)
+               cout.put(ch);
+       cout << endl;
+
+       return 0;
+}
diff --git a/17/getput.cc b/17/getput.cc
new file mode 100644 (file)
index 0000000..c8a3de8
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       char ch;
+       while (cin.get(ch))
+        cout.put(ch);
+       cout << endl;
+
+       return 0;
+}
diff --git a/17/makefile b/17/makefile
new file mode 100644 (file)
index 0000000..265168e
--- /dev/null
@@ -0,0 +1,24 @@
+# executable files for this directory
+OBJECTS = badgetc.exe bits.exe findbook.exe \
+                 game.exe getc.exe getput.exe manips.exe \
+                 normalInts.exe no-skipws.exe point.exe \
+                 prec.exe rand1.exe rand2.exe rand4.exe \
+                 rand6.exe seed.exe seek.exe setw.exe \
+                 skipws.exe tuple.exe manips11.exe sci_dec.exe \
+                 phones.exe ccpgms.exe validatePhones.exe \
+                 phonesFinal.exe subexpr.exe \
+                 regex1.exe zips.exe REerrs.exe 
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../14 
+
+phones.o ccpgms.o validatePhones.o \
+phonesFinal.o subexpr.o regex1.o \
+REerrs.o sci_dec.o manips11.o zips.o: ../Version_test.h
+
+findbook.exe: findbook.o ../14/Sales_data.o
+       $(CC) $(CCFLAGS) -o findbook findbook.o ../14/Sales_data.o
+
diff --git a/17/manips.cc b/17/manips.cc
new file mode 100644 (file)
index 0000000..33129c3
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+using std::boolalpha; using std::noboolalpha;
+using std::oct; using std::dec; using std::hex;
+using std::showbase; using std::noshowbase;
+using std::uppercase; using std::nouppercase;
+
+bool get_status()
+{
+       return false;
+}
+
+int main()
+{
+       bool b;
+       cout << "default bool values: " << true << " " << false
+            << "\nalpha bool values: " << boolalpha 
+                << true << " " << false << endl;
+       
+       bool bool_val = get_status();
+       cout << boolalpha    // sets the internal state of cout
+            << bool_val
+            << noboolalpha; // resets the internal state to default formatting
+       cout << endl;
+       
+       const int ival = 15, jval = 1024;  // const, so values never change
+       
+       cout << "default: " << 20 << " " << 1024 << endl;
+       cout << "octal: " << oct << 20 << " " << 1024 << endl;
+       cout << "hex: " << hex << 20 << " " << 1024 << endl;
+       cout << "decimal: " << dec << 20 << " " << 1024 << endl;
+       
+       
+       cout << showbase;    // show the base when printing integral values
+       cout << "default: " << 20 << " " << 1024 << endl;
+       cout << "in octal: " << oct  << 20 << " " << 1024 << endl;
+       cout << "in hex: " << hex  << 20 << " " << 1024 << endl;
+       cout << "in decimal: " << dec << 20 << " " << 1024 << endl;
+       cout << noshowbase;  // reset the state of the stream
+       
+       cout << uppercase << showbase << hex
+            << "printed in hexadecimal: " << 20 << " " << 1024 
+            << nouppercase << noshowbase << dec << endl;
+
+       return 0;
+}
diff --git a/17/manips11.cc b/17/manips11.cc
new file mode 100644 (file)
index 0000000..e303270
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include "Version_test.h"
+#ifdef HEX_MANIPS
+using std::hexfloat; using std::defaultfloat;
+#endif
+
+int main()
+{
+       double pi = 3.14;
+       cout << pi << " " 
+#ifdef HEX_MANIPS
+            << hexfloat << pi    // no workaround for this missing manipulator
+#endif
+            << defaultfloat << " " << pi << endl;
+       
+       return 0;
+}
diff --git a/17/no-skipws.cc b/17/no-skipws.cc
new file mode 100644 (file)
index 0000000..bddd868
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout;
+using std::noskipws; using std::skipws;
+
+int main()
+{
+       char ch;
+       cin >> noskipws;  // set cin so that it reads whitespace
+       while (cin >> ch)
+               cout << ch;
+       cin >> skipws;    // reset cin to the default state 
+                         // so that it ignores whitespace
+
+       return 0;
+}
diff --git a/17/normalInts.cc b/17/normalInts.cc
new file mode 100644 (file)
index 0000000..fd44116
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <ctime>
+using std::time;
+
+#include <cstdlib>
+using std::rand;
+
+#include <random>
+using std::default_random_engine;
+using std::normal_distribution;
+
+#include <iostream>
+using std::cerr; using std::cout; using std::endl;
+
+#include <vector>
+using std::vector; 
+
+#include <algorithm>
+using std::max_element; 
+
+#include <numeric>
+using std::accumulate;
+
+#include <cmath>
+using std::lround;
+
+#include <cstddef>
+using std::size_t;
+
+int main()
+{
+       default_random_engine e;        // generates random integers
+       normal_distribution<> n(4,1.5); // mean 4, standard deviation 1.5
+       vector<unsigned> vals(9);       // nine elements each 0
+
+       for (size_t i = 0; i != 200; ++i) {
+               unsigned v = lround(n(e));  // round to the nearest integer
+               if (v < vals.size())        // if this result is in range 
+                       ++vals[v];              // count how often each number appears
+       }
+
+       int sum = accumulate(vals.begin(), vals.end(), 0);
+
+       if (sum != 200)
+               cout << "discarded " << 200 - sum << " results" << endl;
+
+       for (size_t j = 0; j != vals.size(); ++j)
+               cout << j << ": " << string(vals[j], '*') << endl;
+
+       return 0;
+}
diff --git a/17/phones.cc b/17/phones.cc
new file mode 100644 (file)
index 0000000..ab76ea8
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Version_test.h"
+
+// if the regular expression library isn't support, do nothing
+#ifdef REGEX
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <regex>
+using std::regex; using std::sregex_iterator; using std::smatch;
+
+void checkPattern(const regex &r, const string &s)
+{
+       smatch results;
+       if (regex_search(s, results, r))
+               cout << results.str() << endl;
+       else 
+               cout << "no match for " << s << endl;
+}
+
+int main()
+{
+       // phone has 10 digits, optional parentheses around the area code
+       // components are separated by an optional space, ',' or '-'
+       string pattern = "\\(?\\d{3}\\)?[-. ]?\\d{3}[-. ]?\\d{4}";
+       regex r(pattern);  // a regex to match our pattern
+
+       // some numbers to try to match
+       string mtch1 = "(908) 555-0181";
+       string mtch2 = "(908)555-0182";
+       string mtch3 = "908 555-0183";
+       string mtch4 = "908.555-0184";
+       string mtch5 = "9085550185";
+
+       smatch results;
+       checkPattern(r, mtch1);
+       checkPattern(r, mtch2);
+       checkPattern(r, mtch3);
+       checkPattern(r, mtch4);
+       checkPattern(r, mtch5);
+
+       string s;
+       while (getline(cin, s)) 
+       {
+               checkPattern(r, s);
+       }
+       
+       return 0;
+}
+
+#else
+
+// do nothing
+int main() { return 0; }
+
+#endif
+
diff --git a/17/phonesFinal.cc b/17/phonesFinal.cc
new file mode 100644 (file)
index 0000000..7e79ca7
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Version_test.h"
+
+// if the regular expression library isn't support, do nothing
+#ifdef REGEX
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <regex>
+using std::regex; using std::sregex_iterator; using std::smatch;
+using std::regex_error;
+using std::regex_constants::format_no_copy;
+
+int main()
+{
+       // phone has 10 digits, optional parentheses around the area code
+       // components are separated by an optional space, ',' or '-'
+       string phone = "\\(?\\d{3}\\)?[-. ]?\\d{3}[-. ]?\\d{4}";
+
+       // parentheses indicate subexpressions;
+       // our overall expression has 7 subexpressions: 
+       //     ( ddd ) space ddd - dddd
+       // the subexpressions 1, 3, 4, and 6 are optional
+       // the subexpressions 2, 5, and 7 hold the digits of the number
+       phone = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ])?(\\d{4})";
+       regex r(phone);  // a regex to find our pattern
+       smatch m;        // a match object for the results
+       string s;
+
+       // generate just the phone numbers:  use a new format string
+       string fmt = "$2.$5.$7"; // reformat numbers to ddd.ddd.dddd
+       string fmt2 = "$2.$5.$7 "; // adds space at the end as a separator
+
+       // read each record from the input file
+       while (getline(cin, s))
+       {
+               cout << regex_replace(s, r, fmt) << endl;
+
+               // tell regex_replace to copy only the text that it replaces
+               cout << regex_replace(s, r, fmt2, format_no_copy) << endl;
+       }
+
+       return 0;
+}
+#else
+
+// do nothing
+int main() { return 0; }
+
+#endif
+
diff --git a/17/point.cc b/17/point.cc
new file mode 100644 (file)
index 0000000..d5bfece
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+using std::showpoint; using std::noshowpoint;
+
+int main()
+{
+       cout << 10.0 << endl;         // prints 10
+       cout << showpoint << 10.0     // prints 10.0000
+            << noshowpoint << endl;  // revert to default format 
+       
+       cout << 10.0 << endl;  // prints 10
+
+       return 0;
+}
diff --git a/17/prec.cc b/17/prec.cc
new file mode 100644 (file)
index 0000000..f18b652
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl; 
+
+#include <iomanip>
+using std::setprecision;
+
+#include <cmath>
+using std::sqrt;
+
+int main()
+{
+       // cout.precision reports the current precision value
+       cout  << "Precision: " << cout.precision()  
+             << ", Value: "   << sqrt(2.0) << endl;
+       
+       // cout.precision(12) asks that 12 digits of precision be printed
+       cout.precision(12);
+       cout << "Precision: " << cout.precision() 
+            << ", Value: "   << sqrt(2.0) << endl;
+       
+       // alternative way to set precision using the setprecision manipulator
+       cout << setprecision(3);
+       cout << "Precision: " << cout.precision() 
+            << ", Value: "   << sqrt(2.0) << endl;
+       
+       return 0;
+}
diff --git a/17/rand1.cc b/17/rand1.cc
new file mode 100644 (file)
index 0000000..bc53196
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <ctime>
+using std::time;
+
+#include <cstdlib>
+using std::rand;
+
+#include <random>
+using std::default_random_engine;
+using std::uniform_int_distribution;
+using std::normal_distribution;
+
+#include <iostream>
+using std::cerr; using std::cout; using std::endl;
+
+#include <vector>
+using std::vector; 
+
+#include <algorithm>
+using std::max_element;
+
+#include <cstddef>
+using std::size_t;
+
+int main()
+{
+       default_random_engine e;  // generates random unsigned integers
+       for (size_t i = 0; i < 10; ++i)
+               // e() "calls" the object to produce the next random number
+               cout << e() << " ";
+       cout << endl;
+
+       // uniformly distributed from 0 to 9 inclusive
+       uniform_int_distribution<unsigned> u(0,9);
+       default_random_engine e2;  
+       for (size_t i = 0; i < 10; ++i)
+               // u uses e as a source of numbers
+               // each call returns a uniformly distributed value 
+               // in the specified range
+       cout << u(e2) << " ";
+       cout << endl;
+
+       default_random_engine e3;  
+       // generates normally distributed doubles 
+       // with mean 100, standard deviation 15
+       normal_distribution<> n(100,15);  
+       for (size_t i = 0; i < 10; ++i)
+       cout << n(e3) << " ";
+       cout << endl;
+
+       // bad, but common way to generate random numbers in a given range
+       for (size_t i = 0; i < 10; ++i)
+               // r will be a between 0 and 9, but randomness is compromised
+               cout << rand()%10 << " ";
+       cout << endl;
+
+       default_random_engine e4;
+       // uniformly distributed from 0 to 9 inclusive
+       uniform_int_distribution<unsigned> u2(0,9);
+       for (size_t i = 0; i < 10; ++i)
+       cout << u2(e4) << " ";
+       cout << endl;
+
+       // seeding the engine causes it to generate different numbers 
+       // on different executions
+       default_random_engine e5(time(0));  
+       for (size_t i = 0; i < 10; ++i)
+       cout << u2(e5) << " ";
+       cout << endl;
+
+    // assuming shorts have 16 bits
+    uniform_int_distribution<short> us1;     // values from 0 to 32767
+    uniform_int_distribution<short> us2(42); // values from 42 to 32767
+    cout << "min: " << us1.min() << " max: " << us1.max() << endl;
+    cout << "min: " << us2.min() << " max: " << us2.max() << endl;
+
+    default_random_engine e6;
+    cout << "min: " << e6.min() << " max: " << e6.max() << endl;
+
+       return 0;
+}
diff --git a/17/rand2.cc b/17/rand2.cc
new file mode 100644 (file)
index 0000000..59046ac
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <ctime>
+using std::time;
+
+#include <cstdlib>
+using std::rand;
+
+#include <random>
+using std::default_random_engine;
+using std::uniform_int_distribution;
+using std::normal_distribution;
+
+#include <iostream>
+using std::cerr; using std::cout; using std::endl;
+
+#include <vector>
+using std::vector; 
+
+#include <cstddef>
+using std::size_t;
+
+#include <algorithm>
+using std::max_element; 
+
+#include <numeric>
+using std::accumulate;
+
+#include <cmath>
+using std::lround;
+
+int main()
+{
+       vector<int> vals(32);     // preallocate so each element is 0 
+       default_random_engine e;  // generates numbers
+       uniform_int_distribution<unsigned> u(0,31); // inclusive range 
+       for (size_t i = 0; i != 100; ++i)
+               ++vals[u(e)];         // count how often each number appears
+       int m = *max_element(vals.begin(), vals.end());
+       for (int i = m; i != 0; --i) {
+               for (size_t j = 0; j != vals.size(); ++j)
+                       if (vals[j] > i-1) cout << "* ";
+                       else               cout << "  ";
+               cout << endl;
+       }
+
+       vector<int> vals2(32);     // preallocate so each element 0 value
+       default_random_engine e2;  // restart the sequence
+       normal_distribution<> n(15,5); // mean 15, standard deviation 5
+       for (size_t i = 0; i != 100; ++i) {
+               size_t v = lround(n(e));
+               if (v < vals.size())
+                       ++vals[v];         // count how often each number appears
+               else
+                       cout << "discarding: " << v << " ";
+       }
+       cout << endl;
+
+       cout << std::accumulate(vals.begin(), vals.end(), 0) << endl;
+       m = *max_element(vals.begin(), vals.end());
+       for (int i = m; i != 0; --i) {
+               for (size_t j = 0; j != vals.size(); ++j)
+                       if (vals[j] > i-1) cout << "* ";
+                       else               cout << "  ";
+               cout << endl;
+       }
+
+       return 0;
+}
diff --git a/17/rand4.cc b/17/rand4.cc
new file mode 100644 (file)
index 0000000..3070753
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <ctime>
+using std::time;
+
+#include <cstdlib>
+using std::rand;
+
+#include <random>
+using std::default_random_engine;
+using std::uniform_int_distribution;
+using std::uniform_real_distribution;
+
+#include <iostream>
+using std::cerr; using std::cout; using std::endl;
+
+#include <vector>
+using std::vector; 
+
+#include <cstddef>
+using std::size_t;
+
+#include <algorithm>
+using std::max_element;
+
+int main()
+{
+       default_random_engine e;  // generates unsigned random integers
+       // uniformly distributed from 0 to 1 inclusive
+       uniform_real_distribution<double> u(0,1);
+       for (size_t i = 0; i < 10; ++i)
+       cout << u(e) << " ";
+       cout << endl;
+
+       // empty <> signify we want to use the default result type
+       uniform_real_distribution<> u2(0,1); // generates double by default
+       default_random_engine e2;
+       for (size_t i = 0; i < 10; ++i)
+       cout << u2(e2) << " ";  // should generate the same sequence 
+       cout << endl;
+
+       default_random_engine e3;
+       // uniformly distributed from 0 to 9 inclusive
+       uniform_int_distribution<unsigned> u3(0,9);
+       for (size_t i = 0; i < 100; ++i)
+       cout << u3(e3) << ((i != 99)? " ": "");
+       cout << endl;
+
+       return 0;
+}
diff --git a/17/rand6.cc b/17/rand6.cc
new file mode 100644 (file)
index 0000000..b351a79
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <ctime>
+using std::time;
+
+#include <cstdlib>
+using std::rand;
+
+#include <random>
+using std::default_random_engine;
+using std::uniform_int_distribution;
+
+#include <iostream>
+using std::cerr; using std::cout; using std::endl;
+
+#include <vector>
+using std::vector;
+
+// almost surely the wrong way to generate a vector of random integers
+// output from this function will be the same 100 numbers on every call!
+vector<unsigned> bad_randVec()
+{
+       default_random_engine e;    
+       uniform_int_distribution<unsigned> u(0,9);
+       vector<unsigned> ret;
+       for (size_t i = 0; i < 100; ++i)
+       ret.push_back(u(e));
+       return ret;
+}
+
+// returns a vector of 100 uniformly distributed random numbers
+vector<unsigned> good_randVec()
+{
+       // because engines and distributions retain state, they usually should be
+       // defined as static so that new numbers are generated on each call
+       static default_random_engine e;    
+       static uniform_int_distribution<unsigned> u(0,9);
+       vector<unsigned> ret;
+       for (size_t i = 0; i < 100; ++i)
+       ret.push_back(u(e));
+       return ret;
+}
+
+int main()
+{
+       vector<unsigned> v1(bad_randVec());
+       vector<unsigned> v2(bad_randVec());
+
+       // will print equal
+       cout << ((v1 == v2) ? "equal" : "not equal") << endl;
+
+       // very unlikely to print equal
+       if (good_randVec() == good_randVec())
+               cout << "equal!" << endl;
+       else
+               cout << "not equal" << endl;
+}
diff --git a/17/regex1.cc b/17/regex1.cc
new file mode 100644 (file)
index 0000000..ef14ef4
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Version_test.h"
+
+// if the regular expression library isn't support, do nothing
+#ifdef REGEX
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <regex>
+using std::regex; using std::sregex_iterator; using std::smatch;
+
+int main()
+{
+       // find the characters ei that follow a character other than c
+       string pattern("[^c]ei");  
+       // we want the whole word in which our pattern appears
+       pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*"; 
+
+       regex r(pattern); // construct a regex to find pattern
+       smatch results;   // define an object to hold the results of a search
+
+       // define a string that has text that does and doesn't match pattern
+       string test_str = "receipt freind theif receive";  
+
+       // use r to find a match to pattern in test_str
+       if (regex_search(test_str, results, r)) // if there is a match
+               cout << results.str() << endl;      // print the matching word
+
+       sregex_iterator it(test_str.begin(), test_str.end(), r);
+       sregex_iterator end_it;   // end iterator
+       for ( ; it != end_it; ++it)
+               cout << it->str() << endl;     // print current match
+
+       // alternative way to obtain all the matches in a given string
+       auto it2 = test_str.cbegin();
+       while (it2 != test_str.cend() && 
+              regex_search(it2, test_str.cend(), results, r)) {
+               cout << results.str() << endl;
+               // reposition the iterator past any nonmatched part
+               // of the string plus the size of this match
+               it2 += results.prefix().length() + results.length();
+       
+       }
+
+       return 0;
+}
+#else
+
+// do nothing
+int main() { return 0; }
+
+#endif
+
diff --git a/17/runpgms b/17/runpgms
new file mode 100644 (file)
index 0000000..e03172b
--- /dev/null
@@ -0,0 +1,19 @@
+echo "badgetc: " && ./badgetc.exe < data/skipws
+echo "bits: " && ./bits.exe < data/bits
+echo "ccpgms: " && ./ccpgms.exe <data/files
+echo "findbook: " && ./findbook.exe data/store*
+echo "game: " && ./game.exe < data/game
+echo "getc: " && ./getc.exe < data/skipws
+echo "getput: " && ./getput.exe < data/skipws
+echo "no-skipws: " && ./no-skipws.exe < data/no-skipws
+echo "phones:" && ./phones.exe < data/phones
+echo "phonesFinal:" && ./phonesFinal.exe < data/phonesFinal
+echo "skipws: " && ./skipws.exe < data/skipws
+echo "subexpr:" &&  ./subexpr.exe < data/files
+echo "validatePhones:" && ./validatePhones.exe  < data/validatePhones
+
+
+../noinput manips.exe manips11.exe normalInts.exe point.exe \
+                  prec.exe rand1.exe rand2.exe rand4.exe \
+                  rand6.exe  regex1.exe REerrs.exe sci_dec.exe \
+                  seed.exe seek.exe setw.exe tuple.exe zips.exe
diff --git a/17/sci_dec.cc b/17/sci_dec.cc
new file mode 100644 (file)
index 0000000..242f393
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cmath>
+using std::sqrt;
+
+#include <iostream>
+using std::cout; using std::endl;
+using std::fixed; using std::scientific; 
+using std::uppercase; using std::nouppercase;
+
+#include "Version_test.h"
+#ifdef HEX_MANIPS
+using std::hexfloat; using std::defaultfloat;
+#endif
+
+int main() 
+{
+       cout << "default format: " << 100 * sqrt(2.0) << '\n'
+                << "scientific: " << scientific << 100 * sqrt(2.0) << '\n'
+            << "fixed decimal: " << fixed << 100 * sqrt(2.0) << '\n'
+#ifdef HEX_MANIPS     // no workaround for this missing manipulator
+                << "hexadecimal: " << hexfloat << 100 * sqrt(2.0) << '\n'
+#endif
+                << "use defaults: " << defaultfloat << 100 * sqrt(2.0) 
+                << "\n\n";
+       
+       cout << uppercase
+            << "scientific: " << scientific << sqrt(2.0) << '\n'
+            << "fixed decimal: " << fixed << sqrt(2.0) << '\n'
+#ifdef HEX_MANIPS     // no workaround for this missing manipulator
+                << "hexadecimal: " << hexfloat << sqrt(2.0) << "\n\n"
+#endif
+                << nouppercase;
+       
+       return 0;
+}
diff --git a/17/seed.cc b/17/seed.cc
new file mode 100644 (file)
index 0000000..95a0ecc
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <ctime>
+using std::time;
+
+#include <cstdlib>
+using std::rand;
+
+#include <random>
+using std::default_random_engine;
+using std::uniform_int_distribution;
+
+#include <iostream>
+using std::cerr; using std::cout; using std::endl;
+
+#include <vector>
+using std::vector;
+
+int main()
+{
+       default_random_engine e1;             // uses the default seed
+       default_random_engine e2(2147483646); // use the given seed value
+
+       // e3 and e4 will generate the same sequence 
+       // because they use the same seed
+       default_random_engine e3;        // uses the default seed value
+       e3.seed(32767);                  // call seed to set a new seed value
+       default_random_engine e4(32767); // set the seed value to 32767
+       for (size_t i = 0; i != 100; ++i) {
+               if (e1() == e2())
+                       cout << "unseeded match at iteration: " << i << endl;
+               if (e3() != e4())
+                       cout << "seeded differs at iteration: " << i << endl;
+       }
+
+       return 0;
+}
diff --git a/17/seek.cc b/17/seek.cc
new file mode 100644 (file)
index 0000000..941e304
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cerr; using std::endl;
+
+#include <fstream>
+using std::fstream;
+
+#include <string>
+using std::string;
+
+#include <cstdlib> // for EXIT_FAILURE
+
+int main()
+{
+    // open for input and output and preposition file pointers to end-of-file
+       // file mode argument 
+    fstream inOut("data/copyOut", 
+                   fstream::ate | fstream::in | fstream::out);
+    if (!inOut) {
+        cerr << "Unable to open file!" << endl;
+        return EXIT_FAILURE; // EXIT_FAILURE 
+    } 
+
+    // inOut is opened in ate mode, so it starts out positioned at the end
+    auto end_mark = inOut.tellg();// remember original end-of-file position
+    inOut.seekg(0, fstream::beg); // reposition to the start of the file
+    size_t cnt = 0;               // accumulator for the byte count
+    string line;                  // hold each line of input
+
+    // while we haven't hit an error and are still reading the original data 
+    while (inOut && inOut.tellg() != end_mark
+           && getline(inOut, line)) { // and can get another line of input
+        cnt += line.size() + 1;       // add 1 to account for the newline
+               auto mark = inOut.tellg();    // remember the read position
+        inOut.seekp(0, fstream::end); // set the write marker to the end
+        inOut << cnt;                 // write the accumulated length
+        // print a separator if this is not the last line
+        if (mark != end_mark) inOut << " "; 
+        inOut.seekg(mark);            // restore the read position
+    }
+    inOut.seekp(0, fstream::end);     // seek to the end
+    inOut << "\n";                    // write a newline at end-of-file
+
+    return 0;
+}
diff --git a/17/setw.cc b/17/setw.cc
new file mode 100644 (file)
index 0000000..e810e16
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl; 
+using std::left; using std::right; using std::internal;
+
+#include <iomanip>
+using std::setw; using std::setfill;
+
+int main()
+{
+       int i = -16;
+       double d = 3.14159;
+       
+       // pad the first column to use a minimum of 12 positions in the output
+       cout << "i: " << setw(12) << i << "next col" << '\n'
+            << "d: " << setw(12) << d << "next col" << '\n';
+       
+       // pad the first column and left-justify all columns
+       cout << left 
+            << "i: " << setw(12) << i << "next col" << '\n'
+            << "d: " << setw(12) << d << "next col" << '\n'
+            << right;           // restore normal justification
+       
+       // pad the first column and right-justify all columns
+       cout << right
+            << "i: " << setw(12) << i << "next col" << '\n'
+            << "d: " << setw(12) << d << "next col" << '\n';
+       
+       // pad the first column but put the padding internal to the field
+       cout << internal
+            << "i: " << setw(12) << i << "next col" << '\n'
+            << "d: " << setw(12) << d << "next col" << '\n';
+       
+       // pad the first column, using # as the pad character
+       cout << setfill('#')
+            << "i: " << setw(12) << i << "next col" << '\n'
+            << "d: " << setw(12) << d << "next col" << '\n'
+            << setfill(' ');   // restore the normal pad character
+
+       return 0;
+}
diff --git a/17/skipws.cc b/17/skipws.cc
new file mode 100644 (file)
index 0000000..0d2b5dc
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       char ch;
+       while (cin >> ch)
+               cout << ch;
+       cout << endl;
+
+       return 0;
+}
diff --git a/17/subexpr.cc b/17/subexpr.cc
new file mode 100644 (file)
index 0000000..5efbdb4
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Version_test.h"
+
+// if the regular expression library isn't support, do nothing
+#ifdef REGEX
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <regex>
+using std::regex; using std::sregex_iterator; using std::smatch;
+using std::regex_error;
+
+int main()
+{
+       try {
+               // r has two subexpressions: 
+               // the first is the part of the file name before the period
+               // the second is the file extension
+               regex r("([[:alnum:]]+)\\.(cpp|cxx|cc)$", regex::icase);
+               smatch results;
+               string filename;
+               while (cin >> filename) 
+                       if (regex_search(filename, results, r))
+                               cout << results.str(1) << endl;  
+                               // print the first subexpression
+       } catch (regex_error e) 
+               { cout << e.what() << " " << e.code() << endl; }
+
+       return 0;
+}
+#else
+
+// do nothing
+int main() { return 0; }
+
+#endif
+
diff --git a/17/tuple.cc b/17/tuple.cc
new file mode 100644 (file)
index 0000000..1ec6aab
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <list>
+using std::list;
+
+#include <vector>
+using std::vector;
+
+#include <tuple>
+using std::tuple; using std::get; 
+using std::tuple_size; using std::tuple_element;
+using std::make_tuple;
+
+#include <string>
+using std::string; 
+
+#include <iostream>
+using std::cout; using std::endl; 
+
+int main()
+{
+       // tuple that represents a bookstore transaction: 
+       // ISBN, count, price per book
+       auto item = make_tuple("0-999-78345-X", 3, 20.00);
+       auto book = get<0>(item);      // returns the first member of item
+       auto cnt = get<1>(item);       // returns the second member of item
+       auto price = get<2>(item)/cnt; // returns the last member of item
+       get<2>(item) *= 0.8;           // apply 20% discount
+       
+       cout << book << " " << cnt << " " << price << endl;
+
+       typedef decltype(item) trans; // trans is the type of item 
+       
+       // returns the number of members in object's of type trans
+       size_t sz = tuple_size<trans>::value;  // returns 3
+
+       // cnt has the same type as the second member in item
+       tuple_element<1, trans>::type cnt2 = get<1>(item); // cnt is an int
+       tuple_element<0, trans>::type book2 = get<0>(item);
+       tuple_element<2, trans>::type price2 = get<2>(item);
+       cout << tuple_size<trans>::value << endl;
+       
+       cout << book2 << " " << cnt2 << " " << price2 << endl;
+       
+       tuple<size_t, size_t, size_t> threeD;  // all three members set to 0
+       tuple<string, vector<double>, int, list<int>>
+           someVal("constants", {3.14, 2.718}, 42, {0,1,2,3,4,5});
+       
+       return 0;
+}
diff --git a/17/validatePhones.cc b/17/validatePhones.cc
new file mode 100644 (file)
index 0000000..92ed930
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Version_test.h"
+
+// if the regular expression library isn't support, do nothing
+#ifdef REGEX
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <regex>
+using std::regex; using std::sregex_iterator; using std::smatch;
+using std::regex_error;
+
+// the area code doesn't have a separator;
+// the remaining separators must be equal
+// OR the area code parens are correct and
+// the next separator is blank or missing
+bool valid(const smatch& m)
+{
+       // if there is an open parenthesis before the area code
+       if(m[1].matched)
+               // the area code must be followed by a close parenthesis
+               // and followed immediately by the rest of the number or a space 
+           return m[3].matched 
+                      && (m[4].matched == 0 || m[4].str() == " ");
+       else 
+               // then there can't be a close after the area code 
+               // the delimiters between the other two components must match
+               return !m[3].matched
+                      && m[4].str() == m[6].str();
+}
+int main()
+{
+       // phone has 10 digits, optional parentheses around the area code
+       // components are separated by an optional space, ',' or '-'
+       string phone = "\\(?\\d{3}\\)?[-. ]?\\d{3}[-. ]?\\d{4}";
+
+       // our overall expression has seven subexpressions: 
+       //    ( ddd ) separator ddd separator dddd
+       // subexpressions 1, 3, 4, and 6 are optional; 
+       // subexpressions 2, 5, and 7 hold the number
+       phone = "(\\()?(\\d{3})(\\))?([-. ])?(\\d{3})([-. ]?)(\\d{4})";
+       regex r(phone);  // a regex to find our pattern
+       smatch m;        // a match object to hold the results
+       string s;        // a string to search
+
+       // read each record from the input file
+       while (getline(cin, s)) {
+               // for each matching phone number
+               for (sregex_iterator it(s.begin(), s.end(), r), end_it;
+                              it != end_it; ++it)
+                       // check whether the number's formatting is valid
+                       if (valid(*it))
+                               cout << "valid: " << it->str() << endl;
+                       else
+                               cout << "not valid: " << it->str() << endl;
+       }
+
+       return 0;
+}
+#else
+
+// do nothing
+int main() { return 0; }
+
+#endif
+
diff --git a/17/zips.cc b/17/zips.cc
new file mode 100644 (file)
index 0000000..7173aa6
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Version_test.h"
+
+// if the regular expression library isn't support, do nothing
+#ifdef REGEX
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <regex>
+using std::regex; using std::sregex_iterator; using std::smatch;
+
+int main()
+{
+       string zip = "\\d{5}-\\d{4}|\\d{5}";
+
+       string test_str = "908.647.4306 164 gates, 07933 07933-1257";
+       regex r(zip);  // a regex to find the parts in our pattern
+       smatch results;
+
+       if (regex_search(test_str, results, r))
+               cout << results.str() << endl;
+       
+       sregex_iterator it(test_str.begin(), test_str.end(), r);
+       sregex_iterator end_it;         // end iterator
+       while (it != end_it) {
+               cout << it->str() << endl;  // print current match
+               ++it;                       // advance iterator for next search
+       }
+
+       return 0;
+}
+#else
+
+// do nothing
+int main() { return 0; }
+
+#endif
+
diff --git a/18/Animal.cc b/18/Animal.cc
new file mode 100644 (file)
index 0000000..3a6b1a6
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Animal.h"
+#include <iostream>
+using std::ostream; using std::cout; using std::endl;
+
+// operations that take references to base classes of type Panda
+void print(const Bear&) 
+{
+       cout << "print(const Bear&)" << std::endl;
+
+}
+void highlight(const Endangered&)
+{
+       cout << "highlight(const Endangered&)" << std::endl;
+}
+
+ostream& operator<<(ostream &os, const ZooAnimal&)
+{
+       return os << "ZooAnimal output operator" << endl;
+}
+
+int main() {
+       Panda ying_yang("ying_yang");
+       
+       print(ying_yang);     // passes Panda to a reference to Bear
+       highlight(ying_yang); // passes Panda to a reference to Endangered
+       cout << ying_yang << endl; // passes Panda to a reference to ZooAnimal
+       Panda ling_ling = ying_yang;    // uses the copy constructor 
+
+       return 0;
+}
diff --git a/18/Animal.h b/18/Animal.h
new file mode 100644 (file)
index 0000000..0d57fda
--- /dev/null
@@ -0,0 +1,138 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+#include <iostream>
+#include <algorithm>
+
+class Endangered {
+public:
+    virtual ~Endangered() 
+               { std::cout << "Endangered dtor" << std::endl; }
+    virtual std::ostream& print() const
+               { return std::cout << "Endangered::print" << std::endl; }
+    virtual void highlight() const
+               { std::cout << "Endangered::highlight" << std::endl; }
+       virtual double max_weight() const
+               { std::cout << "Endangered::max_weight" << std::endl; return 0; }
+    // . . .
+};
+
+class ZooAnimal;
+extern std::ostream&
+operator<<(std::ostream&, const ZooAnimal&);
+
+class ZooAnimal {
+public:
+    ZooAnimal() = default;
+    ZooAnimal(std::string animal, bool exhibit,
+              std::string family): nm(animal), 
+                                   exhibit_stat(exhibit), 
+                                   fam_name(family) { } 
+    virtual ~ZooAnimal()
+               { std::cout << "Animal dtor" << std::endl; }
+
+    virtual std::ostream& print() const
+               { return std::cout << "Animal::print" << std::endl; }
+    virtual int population() const
+               { std::cout << "Animal::population" << std::endl; return 0;}
+       virtual double max_weight() const
+               { std::cout << "Animal::max_weight" << std::endl; return 0;}
+
+    // accessors
+    std::string name() const { return nm; }
+    std::string family_name() const { return fam_name; }
+    bool onExhibit() const { return exhibit_stat; }
+    // . . .
+protected:
+    std::string nm;
+    bool        exhibit_stat = false;
+    std::string fam_name;
+    // . . .
+private:
+};
+
+using DanceType = unsigned;
+constexpr DanceType two_left_feet = 0;
+constexpr DanceType Astaire = 1;
+constexpr DanceType Rogers = 42;
+
+class Bear : public ZooAnimal {
+public:
+    Bear() = default;
+    Bear(std::string name, bool onExhibit=true, 
+         std::string family = "Bear"):
+                         ZooAnimal(name, onExhibit, family),
+                         dancetype(two_left_feet) { }
+
+    virtual std::ostream &print() const
+               { return std::cout << "Bear::print" << std::endl; }
+    virtual int toes() const
+               { std::cout << "Bear::toes" << std::endl; return 0; }
+    int mumble(int)
+               { std::cout << "Bear::mumble" << std::endl; return 0; }
+    void dance(DanceType) const 
+               { std::cout << "Bear::dance" << std::endl; }
+
+    virtual ~Bear()
+               { std::cout << "Bear dtor" << std::endl; }
+private:
+    DanceType   dancetype = Rogers;
+};
+
+class Panda : public Bear, public Endangered {
+public:
+    Panda() = default;
+    Panda(std::string name, bool onExhibit=true);
+    virtual ~Panda()
+               { std::cout << "Panda dtor" << std::endl; }
+    virtual std::ostream& print() const
+               { return std::cout << "Panda::print" << std::endl; }
+    void highlight()
+               { std::cout << "Panda::highlight" << std::endl; }
+    virtual int toes()
+               { std::cout << "Panda::toes" << std::endl; return 0; }
+    virtual void cuddle()
+               { std::cout << "Panda::cuddle" << std::endl; }
+       virtual double max_weight() const;
+// . . .
+};
+
+inline
+Panda::Panda(std::string name, bool onExhibit)
+      : Bear(name, onExhibit, "Panda") { }
+
+inline
+double Panda::max_weight() const
+{
+    return std::max(ZooAnimal::max_weight(), 
+                       Endangered::max_weight());
+}
+
+class PolarBear : public Bear { /* . . . */ };
diff --git a/18/Animal_virtual_baseVers.h b/18/Animal_virtual_baseVers.h
new file mode 100644 (file)
index 0000000..48b1900
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+#include <iostream>
+
+class Endangered {
+public:
+    enum Status { critical, environment, improving };
+    Endangered(Status stat = improving): animal_status(stat) { }
+    virtual ~Endangered() 
+               {std::cout << "Endangered::~Endangered" << std::endl; }
+    virtual std::ostream& print(std::ostream&) const
+               { return std::cout << "Endangered::print" << std::endl; }
+    virtual void highlight() const
+               {std::cout << "Endangered::highlighted" << std::endl; }
+    // . . .
+private:
+    Status animal_status;
+};
+
+class ZooAnimal;
+extern std::ostream&
+operator<<(std::ostream&, const ZooAnimal&);
+
+class ZooAnimal {
+public:
+    ZooAnimal() = default;
+    ZooAnimal(std::string animal, bool exhibit,
+              std::string family): nm(animal), 
+                                   exhibit_stat(exhibit), 
+                                   fam_name(family) { } 
+    virtual ~ZooAnimal()
+               {std::cout << "ZooAnimal::~ZooAnimal" << std::endl; }
+
+    virtual std::ostream& print(std::ostream&) const
+               { return std::cout << "ZooAnimal::print" << std::endl; }
+    virtual int population() const
+               {std::cout << "ZooAnimal::population" << std::endl;  return 0;}
+
+    // accessors
+    std::string name() const { return nm; }
+    std::string family_name() const { return fam_name; }
+    bool onExhibit() const { return exhibit_stat; }
+    // . . .
+protected:
+    std::string nm;
+    bool exhibit_stat = false;
+    std::string fam_name;
+    // . . .
+private:
+};
+
+// the order of the keywords public and virtual is not significant
+class Raccoon : public virtual ZooAnimal {
+public:
+    Raccoon() = default;
+    Raccoon(std::string name, bool onExhibit=true);
+
+    virtual std::ostream& print(std::ostream&) const
+               { return  std::cout << "Raccoon::print" << std::endl; }
+
+    bool pettable() const {return pettable_flag;  }
+    void pettable(bool petval) {pettable_flag = petval;}
+    // . . .
+
+protected:
+    bool pettable_flag = false;
+    // . . .
+};
+
+class Bear : virtual public ZooAnimal {
+public:
+    // when the most derived class
+    Bear(std::string name, bool onExhibit=true);
+protected:
+    // when an intermediate derived class
+    Bear() : dance_flag(two_left_feet) { }
+
+public:
+    enum DanceType { two_left_feet, macarena, fandango };
+
+    virtual std::ostream &print(std::ostream&) const
+               { return  std::cout << "Bear::print" << std::endl; }
+    virtual std::string isA() const
+               { std::cout << "Bear::isA" << std::endl; return "Bear"; }
+    int mumble(int)
+               { std::cout << "Bear::mumble" << std::endl; return 0; }
+    void dance(DanceType) const
+               { std::cout << "Bear::dance" << std::endl; }
+
+       virtual ~Bear() { std::cout << "Bear::~Bear" << std::endl; }
+private:
+    std::string name;
+    DanceType dance_flag;
+};
+
+class Panda : public Bear,
+              public Raccoon, public Endangered {
+public:
+    Panda() = default;
+    Panda(std::string name, bool onExhibit=true);
+    virtual std::ostream& print(std::ostream&) const
+               { return  std::cout << "Panda::print" << std::endl; }
+
+    bool sleeping() const {return sleeping_flag;}
+    void sleeping(bool newval) {sleeping_flag = newval;}
+    // . . .
+
+protected:
+    bool sleeping_flag = false;
+    // . . .
+};
+
+
+Bear::Bear(std::string name, bool onExhibit):
+         ZooAnimal(name, onExhibit, "Bear") { }
+Raccoon::Raccoon(std::string name, bool onExhibit)
+       : ZooAnimal(name, onExhibit, "Raccoon") { }
+
+Panda::Panda(std::string name, bool onExhibit)
+      : ZooAnimal(name, onExhibit, "Panda"),
+        Bear(name, onExhibit),
+        Raccoon(name, onExhibit),
+        Endangered(Endangered::critical),
+        sleeping_flag(false)   { }
+
diff --git a/18/README b/18/README
new file mode 100644 (file)
index 0000000..42c3ace
--- /dev/null
+++ b/18/README
@@ -0,0 +1,10 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   book_sales       book      
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/18/Sales_data.cc b/18/Sales_data.cc
new file mode 100644 (file)
index 0000000..00821a3
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::istream; using std::ostream;
+
+#include "Sales_data.h"
+#include "bookexcept.h"
+
+// this version of the compound assignment operator
+// throws an exception if the objects refer to different books
+Sales_data& Sales_data::operator+=(const Sales_data &rhs)
+{
+    if (isbn() != rhs.isbn())
+        throw isbn_mismatch("wrong isbns", isbn(), rhs.isbn());
+    units_sold += rhs.units_sold;
+    revenue += rhs.revenue;
+    return *this;
+}
+
+// operator+ code is unchanged, but because it uses +=
+// this version of the function also throws when called
+// for books whose isbns differ
+Sales_data 
+operator+(const Sales_data &lhs, const Sales_data &rhs)
+{
+       Sales_data sum = lhs;  // copy data members from lhs into sum
+       sum += rhs;            // add rhs into sum
+       return sum;
+}
+
+// remaning functions unchanged from chapter 16
+// define the hash interface for Sales_data
+namespace std {
+size_t
+hash<Sales_data>::operator()(const Sales_data& s) const
+{
+    return hash<string>()(s.bookNo) ^
+           hash<unsigned>()(s.units_sold) ^
+           hash<double>()(s.revenue);
+}
+}  // close the std namespace; note: no semicolon after the close curly
+
+// remaining members unchanged from chapter 14
+Sales_data::Sales_data(istream &is) 
+{
+       is >> *this; // read a transaction from is into this object
+}
+
+double Sales_data::avg_price() const
+{
+       if (units_sold)
+               return revenue/units_sold;
+       else
+               return 0;
+}
+
+istream &operator>>(istream &is, Sales_data &item)
+{
+       double price;  // no need to initialize; we'll read into price before we use it
+       is >> item.bookNo >> item.units_sold >> price;
+       if (is)        // check that the inputs succeeded
+       item.revenue = item.units_sold * price;
+       else
+       item = Sales_data(); // input failed: give the object the default state
+       return is;
+}
+
+ostream &operator<<(ostream &os, const Sales_data &item)
+{
+       os << item.isbn() << " " << item.units_sold << " " 
+          << item.revenue << " " << item.avg_price();
+       return os;
+}
+
+// operators replace these original named functions
+istream &read(istream &is, Sales_data &item)
+{
+       double price = 0;
+       is >> item.bookNo >> item.units_sold >> price;
+       item.revenue = price * item.units_sold;
+       return is;
+}
+ostream &print(ostream &os, const Sales_data &item)
+{
+       os << item.isbn() << " " << item.units_sold << " " 
+          << item.revenue << " " << item.avg_price();
+       return os;
+}
+       
+Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
+{
+       Sales_data sum = lhs;  // copy data members from lhs into sum
+       sum += rhs;            // add rhs into sum
+       return sum;
+}
+
diff --git a/18/book.cc b/18/book.cc
new file mode 100644 (file)
index 0000000..61b6c98
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Sales_data.h"
+#include "bookexcept.h"
+
+#include <iostream>
+using std::cin; using std::cerr; using std::endl;
+
+int main()
+{
+    // use the hypothetical bookstore exceptions 
+    Sales_data item1, item2, sum;
+    while (cin >> item1 >> item2) {  // read two transactions
+        try {
+            sum = item1 + item2;     // calculate their sum
+            // use sum
+        } catch (const isbn_mismatch &e) {
+          cerr << e.what() << ": left isbn(" << e.left
+               << ") right isbn(" << e.right << ")" << endl;
+        }
+    }
+     return 0;
+}
diff --git a/18/bookexcept.h b/18/bookexcept.h
new file mode 100644 (file)
index 0000000..8a50270
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef BOOKEXCEPT_H
+#define BOOKEXCEPT_H
+#include <stdexcept>
+#include <string>
+
+// hypothetical exception classes for a bookstore application
+class out_of_stock: public std::runtime_error {
+public:
+    explicit out_of_stock(const std::string &s):
+                       std::runtime_error(s) { }
+};
+
+class isbn_mismatch: public std::logic_error {
+public:
+    explicit isbn_mismatch(const std::string &s): 
+                          std::logic_error(s) { }
+    isbn_mismatch(const std::string &s,
+        const std::string &lhs, const std::string &rhs):
+        std::logic_error(s), left(lhs), right(rhs) { }
+    const std::string left, right;
+};
+
+#endif
diff --git a/18/class.cc b/18/class.cc
new file mode 100644 (file)
index 0000000..2c683cd
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+ */ 
+
+#include <iostream>
+#include <string>
+
+int main()
+{
+    std::string s;
+
+    // ok: calls std::getline(std::istream&, const std::string&)
+    getline(std::cin, s);
+
+       std::cout << s << std::endl;
+
+    return 0;
+}
+
diff --git a/18/data/book_sales b/18/data/book_sales
new file mode 100644 (file)
index 0000000..0d14afe
--- /dev/null
@@ -0,0 +1,10 @@
+0-201-70353-X 4 24.99
+0-201-82470-1 4 45.39
+0-201-88954-4 2 15.00 
+0-201-88954-4 5 12.00 
+0-201-88954-4 7 12.00 
+0-201-88954-4 2 12.00 
+0-399-82477-1 2 45.39
+0-399-82477-1 3 45.39
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/18/ex-over.cc b/18/ex-over.cc
new file mode 100644 (file)
index 0000000..84a6350
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+namespace primerLib {
+    void compute() { cout << "primerLib::compute()" << endl; }
+    void compute(const void *) 
+          { cout << "primerLib::compute(const void *)" << endl; }
+}
+
+// brings comput() and compute(const void*) into scope
+using primerLib::compute;
+
+void compute(int) { cout << "compute(int)" << endl; }
+void compute(double, double = 3.4)
+          { cout << "compute(double, double)" << endl; }
+void compute(char*, char* = 0)
+         { cout << "compute(char*, char*)" << endl; }
+
+int main()
+{
+       int i = 42;
+       char c = 'a';
+       compute(i);  // compute(int)
+       compute(c);  // compute(int)
+       compute(&i); // primerLib::compute(const void*)
+       compute(&c); // compute(char*, char*)
+       compute(3.4);// compute(double, double)
+       
+       return 0;
+}
diff --git a/18/inherit.cc b/18/inherit.cc
new file mode 100644 (file)
index 0000000..beb766d
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+
+namespace NS {
+    class Quote { 
+       public:
+               Quote() { std::cout << "Quote::Quote" << std::endl; } 
+       };
+    void display(const Quote&) 
+               { std::cout << "display(const Quote&)" << std::endl; }
+}
+
+// Bulk_item's base class is declared in namespace NS
+class Bulk_item : public NS::Quote { 
+public:
+       Bulk_item() { std::cout << "Bulk_item::Bulk_item" << std::endl; }
+};
+
+int main() {
+    Bulk_item book1;
+
+    display(book1); // calls Quote::display
+
+    return 0;
+}
+
diff --git a/18/makefile b/18/makefile
new file mode 100644 (file)
index 0000000..80f04f1
--- /dev/null
@@ -0,0 +1,18 @@
+# executable files for this directory
+OBJECTS = Animal.exe book.exe class.exe \
+                 ex-over.exe inherit.exe mi-scope.exe\
+                 over.exe over2.exe spec.exe \
+                 usingDirective.exe virt-inherit.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../16
+
+Animal.o: Animal.h
+book.o: ../16/Sales_data.h bookexcept.h
+virt-inherit.o: Animal_virtual_baseVers.h
+
+book.exe: book.o bookexcept.h Sales_data.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) book.o Sales_data.o -o book.exe
diff --git a/18/mi-scope.cc b/18/mi-scope.cc
new file mode 100644 (file)
index 0000000..8204c28
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+#include <vector>
+#include <iostream>
+
+struct Base1 {
+    void print(int) const     // public by default
+               { std::cout << "Base1::print(int) " << ival << std::endl; }
+protected:
+    int ival = 1;
+};
+
+struct Base2 {
+    void print(double) const        // public by default
+               { std::cout << "Base2::print(double) " << ival << std::endl; }
+protected:
+    int ival = 2;
+};
+
+struct Derived : public Base1 {
+    void print(std::string) const   // public by default
+               { std::cout << "Derived::print(string) " << ival << std::endl; }
+protected:
+       int ival = 3;
+};
+
+struct MI : public Derived, public Base2 {
+    void print(std::vector<double>) // public by default
+               { std::cout << "MI::print(int(vector<double>) " 
+                               << ival << std::endl; }
+protected:
+    int ival = 4;
+};
+
+int main()
+{
+       MI obj;
+       obj.Base1::print(0);
+       obj.Base2::print(3.14);
+       obj.Derived::print("hi");
+       obj.print(std::vector<double>());
+       
+       return 0;
+}
diff --git a/18/over.cc b/18/over.cc
new file mode 100644 (file)
index 0000000..22cb5cf
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+
+namespace libs_R_us {
+    extern void print(int) 
+               { std::cout << "libs_R_us::print(int)" << std::endl; }
+    extern void print(double) 
+               { std::cout << "libs_R_us::print(double)" << std::endl; }
+}
+
+// ordinary declaration
+void print(const std::string &)
+{
+       std::cout << "print(const std::string &)" << std::endl;
+}
+
+// this using directive adds names to the candidate set for calls to print:
+using namespace libs_R_us;  
+
+// the candidates for calls to print at this point in the program are:
+//     print(int) from libs_R_us
+//     print(double) from libs_R_us
+//     print(const std::string &) declared explicitly
+
+int main()
+{
+       int ival = 42;
+    print("Value: "); // calls global print(const string &)
+    print(ival);      // calls libs_R_us::print(int)
+
+       return 0;
+}
+
diff --git a/18/over2.cc b/18/over2.cc
new file mode 100644 (file)
index 0000000..0216291
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+
+namespace AW {
+    int print(int i) 
+               { std::cout << "AW::print(int)" << std::endl; return i; }
+}
+namespace Primer {
+    double print(double d) 
+               { std::cout << "Primer::print(double)" << std::endl; return d; }
+}
+
+// using directives create an overload set of functions from different namespaces
+using namespace AW;
+using namespace Primer;
+
+long double print(long double);
+
+int main() {
+    print(1);   // calls AW::print(int)
+    print(3.1); // calls Primer::print(double)
+
+    return 0;
+}
+
diff --git a/18/runpgms b/18/runpgms
new file mode 100644 (file)
index 0000000..0a92c00
--- /dev/null
@@ -0,0 +1,6 @@
+echo "book: " && ./book.exe < data/book_sales
+
+../noinput Animal.exe ex-over.exe inherit.exe mi-scope.exe\
+            over.exe over2.exe spec.exe \
+            usingDirective.exe virt-inherit.exe
+
diff --git a/18/spec.cc b/18/spec.cc
new file mode 100644 (file)
index 0000000..bb53ff7
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <exception>
+using std::exception;
+
+// this function will compile, even though it clearly violates its exception specification
+void f() noexcept       // promises not to throw any exception
+{
+    throw exception();  // violates the exception specification
+}
+
+void g() { }
+void h() noexcept(noexcept(f())) { f(); }
+void i() noexcept(noexcept(g())) { g(); }
+int main()
+{
+    try {
+               cout << "f: " << std::boolalpha << noexcept(f()) << endl;
+               cout << "g: " << std::boolalpha << noexcept(g()) << endl;
+               cout << "h: " << std::boolalpha << noexcept(h()) << endl;
+               cout << "i: " << std::boolalpha << noexcept(i()) << endl;
+        f();
+    } catch (exception &e) {
+        cout << "caught " << e.what() << endl;
+    }
+       
+       return 0;
+}
diff --git a/18/usingDirective.cc b/18/usingDirective.cc
new file mode 100644 (file)
index 0000000..cd5f2c8
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+// namespace A and function f are defined at global scope
+namespace A {
+    int i = 0, j = 42;
+}
+
+void f() 
+{
+    using namespace A; // injects the names from A into the global scope
+
+       // uses i and j from namespace A
+    cout << "i: " << i << " j: " << j << endl;
+}
+
+namespace blip {
+    int i = 16, j = 15, k = 23;
+       void f() 
+               { cout << "i: " << i << " j: " << j << " k: " << k << endl; }
+}
+
+int j = 0;  // ok: j inside blip is hidden inside a namespace
+
+int main() 
+{
+    // using directive; 
+       // the names in blip are ``added'' to the global scope
+    using namespace blip; // clash between ::j and blip::j
+                          // detected only if j is used
+
+    ++i;        // sets blip::i to 17
+    ++::j;      // ok: sets global j to 1
+    ++blip::j;  // ok: sets blip::j to 16
+
+    int k = 97; // local k hides blip::k
+    ++k;        // sets local k to 98
+
+       ::f();        
+       blip::f();
+       cout << "j: " << ::j << " k: " << k << endl; 
+
+       return 0;
+}
diff --git a/18/virt-inherit.cc b/18/virt-inherit.cc
new file mode 100644 (file)
index 0000000..0499b7b
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Animal_virtual_baseVers.h"
+#include <iostream>
+using std::cout; using std::endl; using std::ostream;
+
+void dance(const Bear&) 
+       { cout << "dance(const Bear&)" << endl; }
+
+void rummage(const Raccoon&) 
+       { cout << "rummage(const Raccoon&)" << endl; }
+
+ostream& operator<<(ostream&, const ZooAnimal&) 
+       { return cout << "ZooAnimal output operator" << endl; }
+
+int main ()
+{
+       Panda ying_yang;
+       dance(ying_yang);   // ok: passes Panda object as a Bear 
+       rummage(ying_yang); // ok: passes Panda object as a Raccoon 
+
+       cout << ying_yang;  // ok: passes Panda object as a ZooAnimal
+
+       return 0;
+}
diff --git a/19/.useScreen.cc.swp b/19/.useScreen.cc.swp
new file mode 100644 (file)
index 0000000..a22cc1f
Binary files /dev/null and b/19/.useScreen.cc.swp differ
diff --git a/19/README b/19/README
new file mode 100644 (file)
index 0000000..5325bc4
--- /dev/null
+++ b/19/README
@@ -0,0 +1,2 @@
+None of the programs in this chapter read input.
+
diff --git a/19/Screen.cc b/19/Screen.cc
new file mode 100644 (file)
index 0000000..fd3925e
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced. Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/
+
+#include "Screen.h"
+
+Screen::Action Screen::Menu[] = { &Screen::home,
+                                  &Screen::forward,
+                                  &Screen::back,
+                                  &Screen::up,
+                                  &Screen::down,
+                                };
+
diff --git a/19/Screen.h b/19/Screen.h
new file mode 100644 (file)
index 0000000..ea1bf15
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef SCREEN_H
+#define SCREEN_H
+
+#include <string>
+
+class Screen {
+public:
+    typedef std::string::size_type pos;
+
+       // Action is a type that can point to a member function of Screen
+    // that returns a reference to a Screen and takes no arguments
+       using Action = Screen&(Screen::*)();
+
+    // constructor: build screen of given size containing all blanks
+    Screen(pos ht = 0, pos wd = 0): contents(ht * wd, ' '), cursor(0), 
+                                    height(ht), width(wd) { }
+       friend int main();
+       // data is a static member that returns a pointer to member
+       static const std::string Screen::*data() 
+           { return &Screen::contents; }
+
+       char get_cursor() const { return contents[cursor]; }
+    char get() const { return contents[cursor]; }
+    inline char get(pos ht, pos wd) const;
+private:
+    std::string contents;
+    pos cursor;
+    pos height, width;
+
+public:
+       // cursor movement functions 
+       // beware: these functions don't check whether the operation is valid
+    Screen& home() { cursor = 0; return *this; }
+    Screen& forward() { ++cursor; return *this; }
+    Screen& back() { --cursor; return *this; }
+    Screen& up() { cursor += height; return *this; }
+    Screen& down() {cursor -= height; return *this; }
+
+    // specify which direction to move; enum see XREF(enums)
+    enum Directions { HOME, FORWARD, BACK, UP, DOWN };
+    Screen& move(Directions);
+private:
+    static Action Menu[];      // function table
+};
+
+char Screen::get(pos r, pos c) const // declared as inline in the class
+{
+    pos row = r * width;      // compute row location
+    return contents[row + c]; // return character at the given column
+}
+
+inline
+Screen& Screen::move(Directions cm)
+{
+    // run the element indexed by cm on this object
+    return (this->*Menu[cm])(); // Menu[cm] points to a member function
+}
+
+#endif
+
diff --git a/19/TextQuery.h b/19/TextQuery.h
new file mode 100644 (file)
index 0000000..ed6250b
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef TEXTQUERY_H
+#define TEXTQUERY_H
+#include <memory>
+#include <string>
+#include <vector>
+#include <map>
+#include <set>
+#include <iostream>
+#include <fstream>
+#include <cctype>
+#include <cstring>
+#include <cassert>
+#include <cstdlib>
+#include <utility>
+
+class TextQuery {
+public:
+       class QueryResult;  // nested class to be defined later
+       // other members as in XREF(TextQueryClass)
+       typedef std::vector<std::string>::size_type line_no;
+       TextQuery(std::ifstream&);
+    QueryResult query(const std::string&) const; 
+    void display_map();        // debugging aid: print the map
+private:
+    std::shared_ptr<std::vector<std::string>> file; // input file
+    // map of each word to the set of the lines in which that word appears
+    std::map<std::string, 
+                std::shared_ptr<std::set<line_no>>> wm;  
+    // characters that constitute whitespace
+    static std::string whitespace_chars;     
+       // canonicalizes text: removes punctuation and makes everything lower case
+    static std::string cleanup_str(const std::string&);
+};
+
+//we're defining the QueryResult class that is a member of class TextQuery
+class TextQuery::QueryResult {
+       // in class scope, we don't have to qualify the name of the QueryResult parameters
+       friend std::ostream&
+              print(std::ostream&, const QueryResult&);
+public:
+       // no need to define QueryResult::line_no; a nested class can use a member 
+       // of its enclosing class without needing to qualify the member's name
+       QueryResult(std::string, 
+                   std::shared_ptr<std::set<line_no>>,  
+                   std::shared_ptr<std::vector<std::string>>);
+    // other members as in XREF(TextQueryClass)
+       typedef std::set<line_no>::const_iterator line_it;
+       std::set<line_no>::size_type size() const  { return lines->size(); }
+       line_it begin() const { return lines->cbegin(); }
+       line_it end() const   { return lines->cend(); }
+       std::shared_ptr<std::vector<std::string>> get_file() { return file; }
+private:
+       std::string sought;  // word this query represents
+       std::shared_ptr<std::set<line_no>> lines; // lines it's on
+       std::shared_ptr<std::vector<std::string>> file;  //input file
+       static int static_mem;
+};
+std::ostream &print(std::ostream&, const TextQuery::QueryResult&);
+
+using std::shared_ptr; using std::string; using std::set;
+using std::vector;
+
+inline
+// defining the member named QueryResult for the class named QueryResult
+// that is nested inside the class TextQuery
+TextQuery::QueryResult::QueryResult(string s, 
+                   shared_ptr<set<line_no>> p,  
+                   shared_ptr<vector<string>> f):
+               sought(s), lines(p), file(f) { }
+#endif
diff --git a/19/Token.h b/19/Token.h
new file mode 100644 (file)
index 0000000..06fd0bf
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef TOKEN_H
+#define TOKEN_H
+
+#include <string>
+using std::string;
+
+#include <iostream>
+
+#include "Token.h"
+
+class Token {
+friend std::ostream &operator<<(std::ostream&, const Token&);
+public:
+       // copy control needed because our class has a union with a string member
+       // defining the move constructor and move-assignment operator is left as an exercise
+       Token(): tok(INT), ival{0} { }
+       Token(const Token &t): tok(t.tok) { copyUnion(t); }
+       Token &operator=(const Token&);
+       // if the  union holds a string, we must destroy it; see XREF(expldtor)
+       ~Token() { if (tok == STR) sval.~string(); }
+       // assignment operators to set the differing members of the union
+       Token &operator=(const std::string&);
+       Token &operator=(char);
+       Token &operator=(int);
+       Token &operator=(double);
+private:
+    enum {INT, CHAR, DBL, STR} tok; // discriminant
+    union {                         // anonymous union 
+        char   cval;
+        int    ival;
+        double dval;
+               std::string sval;
+    }; // each Token object has an unnamed member of this unnamed union type
+       // check the discriminant and copy the union member as appropriate
+       void copyUnion(const Token&);  
+};
+
+inline
+void Token::copyUnion(const Token &t)
+{
+       switch (t.tok) {
+               case Token::INT: ival = t.ival; break;
+               case Token::CHAR: cval = t.cval; break;
+               case Token::DBL: dval = t.dval; break;
+               // to copy a string, construct it using placement new; see (XREF(placenew)) 
+               case Token::STR: new(&sval) std::string(t.sval); break;
+       }
+}
+
+inline
+std::ostream &operator<<(std::ostream &os, const Token &t)
+{
+       switch (t.tok) {
+               case Token::INT: return os << t.ival;
+               case Token::CHAR: return os << t.cval;
+               case Token::DBL: return os << t.dval;
+               case Token::STR: return os << t.sval;
+       }
+}
+
+inline
+Token &Token::operator=(double d)
+{
+       if (tok == STR) sval.~string();  // if we have a string, free it
+       dval = d;
+       tok = DBL;
+       return *this;
+}
+
+inline
+Token &Token::operator=(char c)
+{
+       if (tok == STR) sval.~string();  // if we have a string, free it
+       cval = c;
+       tok = CHAR;
+       return *this;
+}
+
+inline
+Token &Token::operator=(int i)
+{
+       if (tok == STR) sval.~string();  // if we have a string, free it
+       ival = i;                        // assign to the appropriate member
+       tok = INT;                       // update the discriminant
+       return *this;
+}
+
+inline
+Token &Token::operator=(const std::string &s)
+{
+       if (tok == STR)  // if we already hold a string, just do an assignment
+               sval = s;
+       else
+               new(&sval) std::string(s);  // otherwise construct a string
+       tok = STR;                 // update the discriminant
+       return *this;
+}
+
+inline
+Token &Token::operator=(const Token &t)
+{
+       // if this object holds a string and t doesn't, we have to free the old string
+       if (tok == STR && t.tok != STR) sval.~string();
+       if (tok == STR && t.tok == STR)
+               sval = t.sval;  // no need to construct a new string
+       else
+               copyUnion(t);   // will construct a string if t.tok is STR
+       tok = t.tok;
+       return *this;
+}
+#endif
diff --git a/19/bitfields.cc b/19/bitfields.cc
new file mode 100644 (file)
index 0000000..f3c96f4
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced. Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/
+#include <iostream>
+using std::cout; using std::endl;
+
+typedef unsigned int Bit;
+
+class File {
+    Bit mode: 2;       // mode has 2 bits
+    Bit modified: 1;   // modified has 1 bit
+    Bit prot_owner: 3; // prot_owner has 3 bits
+    Bit prot_group: 3; // prot_group has 3 bits
+    Bit prot_world: 3; // prot_world has 3 bits
+    // operations and data members of File
+public:
+       // file modes specified as octal literals; see XREF(intLiterals)
+       enum modes { READ = 01, WRITE = 02, EXECUTE = 03 };
+       File &open(modes);
+       void close();
+       void write();
+       bool isRead() const;
+       void setWrite();
+       void execute();
+       bool isExecute() const;
+};
+
+void File::write()
+{
+    modified = 1;
+    // . . .
+}
+
+void File::close()
+{
+    if (modified)
+        // . . . save contents
+       ;
+}
+
+inline bool File::isRead() const { return mode & READ; }
+inline void File::setWrite() { mode |= WRITE; }
+
+
+File &File::open(File::modes m)
+{
+       mode |= READ;    // set the READ bit by default
+       // other processing
+       if (m & WRITE) // if opening READ and WRITE 
+       // processing to open the file in read/write mode
+       cout << "myFile.mode READ is set" << endl;
+       return *this;
+}
+
+int main()
+{
+       File myFile;
+       
+       myFile.open(File::READ);
+       if (myFile.isRead()) 
+               cout << "reading" << endl;
+       
+       return 0;
+}
+
diff --git a/19/calc.cc b/19/calc.cc
new file mode 100644 (file)
index 0000000..b687b2f
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl; 
+using std::ostream;
+
+#include <map>
+using std::map;
+
+#include <string>
+using std::string;
+
+#include <functional>
+using std::bind; using std::function;
+using namespace std::placeholders;
+
+// this version of the desk calculator adds a class that
+// represents both the left and right shift operators.
+// This calss illustrates using pointers to member
+// in the desk calculator
+struct ShiftOps {
+       ShiftOps(std::ostream &o) : os(o) { }
+       unsigned Lshift(unsigned op1, unsigned op2) 
+       { os << "Lshift: " << op1 << " " << op2; return op1 << op2; }
+       unsigned Rshift(unsigned op1, unsigned op2) 
+       { os << "Rshift: " << op1 << " " << op2; return op1 >> op2; }
+private:
+       std::ostream &os;
+};
+
+// ordinary function
+int add(int i, int j) { return i + j; }
+
+// lambda, which generates an unnamed function-object class
+auto mod = [](int i, int j) { return i % j; };
+
+// function-object class
+// Note, in the first printing of The Primer this struct was named div
+struct divide {
+    int operator()(int denominator, int divisor) {
+        return denominator / divisor;
+    }
+};
+
+int main()
+{
+       function<int(int, int)> f1 = add;      // function pointer
+       function<int(int, int)> f2 = divide(); // callable class type
+       function<int(int, int)> f3 = [](int i, int j) // lambda
+                                    { return i * j; };
+       cout << f1(4,2) << endl; // prints 6
+       cout << f2(4,2) << endl; // prints 2
+       cout << f3(4,2) << endl; // prints 8
+
+       // table of callable objects corresponding to each binary operator
+       // all the callables must take two ints and return an int
+       // an element can be a function pointer, function object, or lambda
+       map<string, function<int(int, int)>> binops = {
+               {"+", add},                  // function pointer
+               {"-", std::minus<int>()},    // library function object
+               {"/",  divide()},            // user-defined function object
+               {"*", [](int i, int j) { return i * j; }}, // unnamed lambda
+               {"%", mod} };                // named lambda object
+
+       cout << binops["+"](10, 5) << endl; // calls add(10, 5)
+       cout << binops["-"](10, 5) << endl; // uses the call operator of the minus<int> object
+       cout << binops["/"](10, 5) << endl; // uses the call operator of the divide object
+       cout << binops["*"](10, 5) << endl; // calls the lambda function object
+       cout << binops["%"](10, 5) << endl; // calls the lambda function object
+
+       // memp can point to either shift operation in ShiftOps
+       // memp points to the Lshift member
+       function<int (ShiftOps*, int, int)> memp = &ShiftOps::Lshift;  
+
+       ShiftOps shift(cout);      // declare an object to which to bind the member pointer
+       binops.insert({"<<", bind(memp, &shift, _1, _2)}); 
+       cout << binops["<<"](10, 5) << endl; // calls member function
+}
diff --git a/19/dyn_cast.cc b/19/dyn_cast.cc
new file mode 100644 (file)
index 0000000..0727b6b
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <typeinfo>
+using std::bad_cast;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+struct A { virtual ~A() { } };
+struct B : virtual public A { /* . . . */ };
+struct C : public B { /* . . . */ };
+struct D : public B, virtual public A { /* . . . */ };
+
+void exercises() {
+       A *pa = new C;
+    if (B *pb = dynamic_cast< B* >(pa))
+         cout << "cast from C to B* ok" << endl;
+    else
+         cout << "cast from C to B* not ok" << endl;
+       B *pb = new B;
+    if (C *pc = dynamic_cast< C* >(pb))
+         cout << "cast from B to C* ok" << endl;
+    else
+         cout << "cast from B to C* not ok" << endl;
+
+       A *pc = new C;
+    if (B *pb = dynamic_cast< B* >(pc))
+         cout << "cast C to B* ok" << endl;
+    else
+         cout << "cast C to B* not ok" << endl;
+
+       A *pd = new D;
+    if (B *pb = dynamic_cast< B* >(pd))
+         cout << "cast D to B* ok" << endl;
+    else
+         cout << "cast D to B* not ok" << endl;
+}
+
+struct Base {
+   virtual ~Base() {};
+};
+
+struct Derived: public Base { };
+
+void cast_to_ref(const Base &b)
+{
+    try {
+        const Derived &d = dynamic_cast<const Derived&>(b);
+       // use the Derived object to which b referred
+    } catch (bad_cast) {
+        cout << "called f with an object that is not a Derived" << endl;
+    }
+}
+
+int main()
+{
+       Base *bp; 
+       bp = new Derived;  // bp actually points to a Derived object
+       if (Derived *dp = dynamic_cast<Derived*>(bp)) 
+       {
+           // use the Derived object to which dp points
+       } else {  // bp points at a Base object
+       // use the Base object to which bp points
+       }
+
+       exercises();
+       
+       cast_to_ref(*bp);
+       cast_to_ref(Base());
+}
+
diff --git a/19/enum-match.cc b/19/enum-match.cc
new file mode 100644 (file)
index 0000000..0c62a6e
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+// unscoped enumeration; the underlying type is machine dependent
+enum Tokens {INLINE = 128, VIRTUAL = 129};
+
+void ff(Tokens) 
+{
+       cout << "ff(Tokens)" << endl; 
+}
+void ff(int) 
+{
+       cout << "ff(int)" << endl;
+}
+void newf(int) 
+{
+       cout << "newf(int)" << endl; 
+}
+void newf(unsigned char) 
+{
+       cout << "newf(unsigned char)" << endl; 
+}
+
+int main() {
+    Tokens curTok = INLINE;
+    ff(128);    // exactly matches ff(int)
+    ff(INLINE); // exactly matches ff(Tokens)
+    ff(curTok); // exactly matches ff(Tokens)
+
+       void newf(unsigned char);
+       void newf(int);
+       unsigned char uc = VIRTUAL;
+       
+       newf(VIRTUAL);  // calls newf(int)
+       newf(uc);       // calls newf(unsigned char)
+       
+       newf(129);      // calls newf(int)
+       
+    return 0;
+}
diff --git a/19/enum.cc b/19/enum.cc
new file mode 100644 (file)
index 0000000..c2f1576
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <cstddef>
+using std::size_t;
+
+// forward declaration of unscoped enum named intValues 
+enum intValues : unsigned long long; // unscoped, must specify a type
+enum class open_modes;  // scoped enums can use int by default
+
+enum class open_modes {input, output, append};
+
+// input is an enumerator of open_modes
+open_modes om = open_modes::input; 
+
+enum class intTypes {
+       charTyp = 8, shortTyp = 16, intTyp = 16, 
+       longTyp = 32, long_longTyp = 64
+};
+
+constexpr intTypes charbits = intTypes::charTyp;
+
+enum intValues : unsigned long long {
+       charTyp = 255, shortTyp = 65535, intTyp = 65535, 
+       longTyp = 4294967295UL, 
+       long_longTyp = 18446744073709551615ULL
+};
+
+int main() 
+{
+       enum color {red, yellow, green};      // unscoped enumeration 
+       enum class peppers {red, yellow, green}; // enumerators are hidden 
+       
+       // unnamed, unscoped enum
+       enum {floatPrec = 6, doublePrec = 10, double_doublePrec = 10};
+       
+       // enumerators are in scope for an unscoped enumeration
+       color eyes = green; 
+       
+       color hair = color::red;   // we can explicitly access the enumerators
+       peppers p2 = peppers::red; // using red from peppers 
+       
+       int i = color::red;   // unscoped enumerator implicitly converted to int
+       
+       // point2d is 2, point2w is 3, point3d is 3, point3w is 4
+       enum class Points { point2d = 2, point2w,
+                           point3d = 3, point3w };
+
+       return 0;
+}
diff --git a/19/makefile b/19/makefile
new file mode 100644 (file)
index 0000000..89fd1e9
--- /dev/null
@@ -0,0 +1,15 @@
+# executable files for this directory
+OBJECTS = bitfields.exe calc.exe dyn_cast.exe enum.exe enum-match.exe \
+                 memFN-bind.exe tok2.exe typeid.exe useScreen.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../14
+
+Screen.o useScreen.o: Screen.h
+
+useScreen.exe: useScreen.o Screen.o 
+       $(CC) $(CCFLAGS) useScreen.o Screen.o -o useScreen.exe
+
diff --git a/19/memFN-bind.cc b/19/memFN-bind.cc
new file mode 100644 (file)
index 0000000..5313215
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string; 
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cout; using std::endl; 
+
+#include <algorithm>
+using std::find_if;
+
+#include <functional>
+using std::bind;
+using std::placeholders::_1; 
+
+int main()
+{
+    vector<string> svec = {"hi", "bye"};
+    // bind each string in the range to the implicit first argument to empty
+    auto it = find_if(svec.begin(), svec.end(), 
+                         bind(&string::empty, _1));
+    if (it == svec.end()) 
+               cout << "worked" << endl; 
+       else 
+               cout << "failed"  << endl;
+       auto f =  bind(&string::empty, _1);
+       f(*svec.begin()); // ok: argument is a string f will use .* to call empty
+       f(&svec[0]); // ok: argument is a pointer to string f will use .-> to call empty
+}
+
+
diff --git a/19/out2 b/19/out2
new file mode 100644 (file)
index 0000000..3ed2920
--- /dev/null
+++ b/19/out2
@@ -0,0 +1,36 @@
+bitfields.exe
+reading
+calc.exe
+6
+2
+8
+15
+5
+2
+50
+0
+Lshift: 10 5320
+dyn_cast.exe
+cast from C to B* ok
+cast from B to C* not ok
+cast C to B* ok
+cast D to B* ok
+called f with an object that is not a Derived
+enum.exe
+enum-match.exe
+ff(int)
+ff(Tokens)
+ff(Tokens)
+newf(int)
+newf(unsigned char)
+newf(int)
+memFN-bind.exe
+worked
+tok2.exe
+0 0
+boo
+good bye
+42
+typeid.exe
+i, A10_i, 10Sales_data, Ss, P4Base, 7Derived
+useScreen.exe
diff --git a/19/runpgms b/19/runpgms
new file mode 100644 (file)
index 0000000..ab6330b
--- /dev/null
@@ -0,0 +1,4 @@
+../noinput bitfields.exe calc.exe dyn_cast.exe enum.exe \
+                  enum-match.exe memFN-bind.exe tok2.exe typeid.exe \
+                  useScreen.exe 
+
diff --git a/19/tok2.cc b/19/tok2.cc
new file mode 100644 (file)
index 0000000..65f7f9a
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include "Token.h"
+
+int main()
+{
+       Token token;
+       Token t2;
+       Token t3;
+       cout << t2 << " " << t3 << endl;
+       t2 = string("hi mom!");
+       t3 = "good bye";
+       token = t2;
+       token = "boo";
+       cout << token << endl;
+       t2 = t3;
+       cout << t2 << endl;
+
+       token = 42;
+       cout << token << endl;
+}
diff --git a/19/typeid.cc b/19/typeid.cc
new file mode 100644 (file)
index 0000000..494bd38
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Sales_data.h"
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <typeinfo>
+
+#include <string>
+using std::string;
+
+struct Base {
+    virtual ~Base() { }
+};
+
+struct Derived : Base { };
+
+int main()
+{
+       int arr[10];
+       Derived d;
+       Base *p = &d;
+       
+       cout << typeid(42).name() << ", "
+            << typeid(arr).name() << ", "
+            << typeid(Sales_data).name() << ", "
+            << typeid(std::string).name() << ", "
+            << typeid(p).name() << ", "
+            << typeid(*p).name() << endl;
+       
+       return 0;
+}
diff --git a/19/useScreen.cc b/19/useScreen.cc
new file mode 100644 (file)
index 0000000..badd83c
--- /dev/null
@@ -0,0 +1,115 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced. Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/
+
+#include "Screen.h"
+
+#include <functional>
+using std::function; 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+struct X {
+       int foo(int i) { cout << "foo(" << i << ")" << endl; return i; }
+};
+
+void xfcn()
+{
+       function<int (X*, int)> f;
+       f = &X::foo;            // pointer to member
+
+       X x;
+       int v = f(&x, 5);
+       cout << "v = " << v << endl;
+}
+
+int main ()
+{
+       // pdata can point to a string member of a const (or nonconst) Screen
+       const string Screen::*pdata;  // uninitialized
+       pdata = &Screen::contents;    // points to the contents member
+       auto pdata2 = &Screen::contents; // equivalent declaration
+
+       // data() returns a pointer to the contents member of class Screen
+       const string Screen::*pdata3 = Screen::data();
+       
+       // Screen objects
+       Screen myScreen, *pScreen = &myScreen;
+       const Screen cScreen, *pcScreen = &cScreen;
+
+       // .* dereferences pdata to fetch the contents member 
+       // from the object myScreen
+       auto str = myScreen.*pdata;  // s is a string
+       auto cstr = cScreen.*pdata;  // c is a const string
+       
+       // ->* dereferences pdata to fetch contents 
+       // from the object to which pScreen points
+       str = pScreen->*pdata;  
+       
+       // pmf is a pointer that can point to a Screen member function 
+       // that takes no arguments, returns a char, and is const
+       // that returns a char and takes no arguments
+       auto pmf = &Screen::get_cursor;
+       char (Screen::*pmf2)() const = &Screen::get; // same type as pmf
+       
+       pmf = &Screen::get; // which version of get deduced from type of pmf
+       pmf2 = &Screen::get_cursor;
+       
+       Screen s;
+       char c1 = s.get_cursor(); // gets character at the cursor directly
+       char c2 = (s.*pmf2)();    // calls get_cursor indirectly through pmf2
+       
+       // call the function to which pmf points 
+       // on the object to which pScreen points
+       c1 = (pScreen->*pmf)();  
+       
+       // pmf3 points to the two-parameter version of get
+       char (Screen::*pmf3)(Screen::pos, Screen::pos) const = &Screen::get;
+       c1 = myScreen.get(0,0);     // call two-parameter version of get 
+       c2 = (myScreen.*pmf3)(0,0); // equivalent call to get
+       
+       // Op is a type that can point to a member function of Screen 
+       // that returns a char and takes two pos arguments
+       using Op = char (Screen::*)(Screen::pos, Screen::pos) const;
+       // equivalent declaration of Op using a typedef
+       typedef char (Screen::*Op)(Screen::pos, Screen::pos) const;
+
+       Op get = &Screen::get; // get points to the get member of Screen
+       
+       myScreen.move(Screen::HOME);  // invokes myScreen.home
+       myScreen.move(Screen::DOWN);  // invokes myScreen.down
+
+       // bind an object of type function to a pointer to member
+       function<char (const Screen*)> f = &Screen::get_cursor;
+
+       return 0;
+}
diff --git a/2/README b/2/README
new file mode 100644 (file)
index 0000000..834d139
--- /dev/null
+++ b/2/README
@@ -0,0 +1,10 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   add_item         Sales_data
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/2/Sales_data.cc b/2/Sales_data.cc
new file mode 100644 (file)
index 0000000..fae73f2
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+#include <string>
+#include "Sales_data.h"
+
+int main()
+{
+       Sales_data data1, data2;
+
+       // code to read into data1 and data2
+       double price = 0;  // price per book, used to calculate total revenue
+
+       // read the first transactions: ISBN, number of books sold, price per book
+       std::cin >> data1.bookNo >> data1.units_sold >> price;
+       // calculate total revenue from price and units_sold
+       data1.revenue = data1.units_sold * price;
+
+       // read the second transaction
+       std::cin >> data2.bookNo >> data2.units_sold >> price;
+       data2.revenue = data2.units_sold * price;
+
+       // code to check whether data1 and data2 have the same ISBN
+       //        and if so print the sum of data1 and data2
+       if (data1.bookNo == data2.bookNo) {
+               unsigned totalCnt = data1.units_sold + data2.units_sold;
+               double totalRevenue = data1.revenue + data2.revenue;
+
+               // print: ISBN, total sold, total revenue, average price per book
+               std::cout << data1.bookNo << " " << totalCnt 
+                         << " " << totalRevenue << " ";
+               if (totalCnt != 0)
+                       std::cout << totalRevenue/totalCnt << std::endl;
+               else
+                       std::cout  << "(no sales)" << std::endl;
+
+               return 0;  // indicate success
+       } else {  // transactions weren't for the same ISBN
+               std::cerr << "Data must refer to the same ISBN" 
+                         << std::endl;
+               return -1; // indicate failure
+       }
+}
diff --git a/2/Sales_data.h b/2/Sales_data.h
new file mode 100644 (file)
index 0000000..5968332
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef SALES_DATA_H
+#define SALES_DATA_H
+
+#include <string>
+
+struct Sales_data {
+       std::string bookNo;
+       unsigned units_sold = 0;
+       double revenue = 0.0;
+};
+#endif
diff --git a/2/compounddecls.cc b/2/compounddecls.cc
new file mode 100644 (file)
index 0000000..e6026eb
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+int main()
+{
+       // i is an int; p is a pointer to int; r is a reference to int
+       int i = 1024, *p = &i, &r = i;
+       
+       // three ways to print the value of i
+       std::cout << i << " " <<  *p <<  " " << r << std::endl;
+
+       int j = 42, *p2 = &j;
+       int *&pref = p2;  // pref is a reference to the pointer p2
+
+       // prints the value of j, which is the int to which p2 points
+       std::cout << *pref << std::endl;
+       
+       // pref refers to a pointer; assigning &i to pref makes p point to i
+       pref = &i; 
+       std::cout << *pref << std::endl; // prints the value of i
+
+       // dereferencing pref yields i, the int to which p2 points; 
+       *pref = 0;  // changes i to 0
+       
+       std::cout << i << " " << *pref << std::endl;
+
+       return 0;
+}
+       
diff --git a/2/convs.cc b/2/convs.cc
new file mode 100644 (file)
index 0000000..fbc0c35
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+int main()
+{
+       int i = 42;
+       std::cout << i << std::endl; // prints 42
+       if (i) // condition will evaluate as true
+               i = 0;
+       std::cout << i << std::endl; // prints 0
+
+       bool b = 42;            // b is true
+       std::cout << b << std::endl; // prints 1
+
+       int j = b;              // j has value 1
+       std::cout << j << std::endl; // prints 1
+
+       double pi = 3.14;       // pi has value 3.14
+       std::cout << pi << std::endl; // prints 3.14
+
+       j = pi;                 // j has value 3
+       std::cout << j << std::endl; // prints 3
+
+       unsigned char c = -1;   // assuming 8-bit chars, c has value 255
+       i = c;  // the character with value 255 is an unprintable character
+               // assigns value of c (i.e., 255) to an int
+       std::cout << i << std::endl; // prints 255
+       
+       return 0;
+}
diff --git a/2/data/add_item b/2/data/add_item
new file mode 100644 (file)
index 0000000..92fd61c
--- /dev/null
@@ -0,0 +1,2 @@
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/2/data/data/add_item b/2/data/data/add_item
new file mode 100644 (file)
index 0000000..92fd61c
--- /dev/null
@@ -0,0 +1,2 @@
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/2/dbl_ptr.cc b/2/dbl_ptr.cc
new file mode 100644 (file)
index 0000000..530ea91
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout;
+using std::endl;
+
+int main()
+{
+    int ival = 1024;
+    int *pi = &ival;   // pi points to an int
+    int **ppi = &pi;   // ppi points to a pointer to an int
+    cout << "The value of ival\n"
+         << "direct value: " << ival << "\n"
+         << "indirect value: " << *pi << "\n"
+         << "doubly indirect value: " << **ppi
+         << endl;
+
+       int i = 2; 
+       int *p1 = &i;     // p1 points to i
+       *p1 = *p1 * *p1;  // equivalent to i = i * i
+       cout << "i  = " << i << endl;
+
+       *p1 *= *p1;       // equivalent to i *= i
+       cout << "i  = " << i << endl;
+
+       return 0;
+}
diff --git a/2/decl-ex.cc b/2/decl-ex.cc
new file mode 100644 (file)
index 0000000..2e2662f
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       int a = 0;
+       decltype(a) c = a;   // c is an int
+       decltype((a)) d = a; // d is a reference to a
+       ++c;                 // increments c, a (and d) unchanged
+       cout << "a: " << a << " c: " << c << " d: " << d << endl;
+       ++d;                 // increments a through the reference d
+       cout << "a: " << a << " c: " << c << " d: " << d << endl;
+       
+       int A = 0, B = 0;
+       decltype((A)) C = A;   // C is a reference to A
+       decltype(A = B) D = A; // D is also a reference to A
+       ++C;
+       cout << "A: " << A << " C: " << C << " D: " << D << endl;
+       ++D;
+       cout << "A: " << A << " C: " << C << " D: " << D << endl;
+       
+       return 0;
+}
diff --git a/2/escape.cc b/2/escape.cc
new file mode 100644 (file)
index 0000000..f206e0b
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+
+int main() {
+       std::cout << '\n';       // prints a newline
+       std::cout << "\tHi!\n";  // prints a tab followd by "Hi!" and a newline
+       std::cout << "Hi \x4dO\115!\n"; // prints Hi MOM! followed by a newline
+       std::cout << '\115' << '\n';    // prints M followed by a newline
+
+       return 0;
+}
diff --git a/2/inits.cc b/2/inits.cc
new file mode 100644 (file)
index 0000000..86c1ddc
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Sales_item.h"
+#include <iostream>
+#include <string>
+
+
+int main() {
+
+       int v1(1024);    // direct-initialization, functional form
+       int v2{1024};    // direct-initialization, list initializer form
+       int v3 = 1024;   // copy-initialization
+       int v4 = {1024}; // copy-initialization, list initializer form
+
+
+       // alternative ways to initialize string from a character string literal
+       std::string titleA = "C++ Primer, 5th Ed.";
+       std::string titleB("C++ Primer, 5th Ed.");
+       std::string all_nines(10, '9');  // all_nines = "9999999999"
+
+       return 0;
+}
diff --git a/2/literals.cc b/2/literals.cc
new file mode 100644 (file)
index 0000000..ed35542
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+int main() 
+{
+       std::cout << "Hello World!";  // simple character string literal
+       std::cout << "";              // empty character string literal
+       // literal using newlines and tabs
+       std::cout << "\nCC\toptions\tfile.[cC]\n";
+
+    // multiline string literal
+    std::cout << "a really, really long string literal "
+                    "that spans two lines" << std::endl;
+
+       // three ways to print a capital M
+       std::cout << 'M' << " " << '\115' << " " << '\x4d' << std::endl;
+
+    unsigned long long bigVal = -1ULL;
+    std::cout << bigVal << std::endl;
+
+    return 0;
+}
+
diff --git a/2/makefile b/2/makefile
new file mode 100644 (file)
index 0000000..6801cc4
--- /dev/null
@@ -0,0 +1,11 @@
+# executable files for this directory
+OBJECTS = compounddecls.exe convs.exe dbl_ptr.exe \
+                 decl-ex.exe escape.exe inits.exe \
+                 literals.exe ref-ex.exe Sales_data.exe \
+                 scope_levels.exe unsigned.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../1
diff --git a/2/ref-ex.cc b/2/ref-ex.cc
new file mode 100644 (file)
index 0000000..24c5d03
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+int main()
+{
+       int i = 0, &ri = i;  // ri is a reference to i
+       // ri is just another name for i; 
+       // this statement prints the value of i twice
+       std::cout << i << " " << ri << std::endl;
+
+       i = 5; // changing i is reflected through ri as well
+       std::cout << i << " " << ri << std::endl;
+
+       ri = 10; // assigning to ri actually assigns to i
+       std::cout << i << " " << ri << std::endl;
+
+       return 0;
+}
diff --git a/2/runpgms b/2/runpgms
new file mode 100644 (file)
index 0000000..66fd34b
--- /dev/null
+++ b/2/runpgms
@@ -0,0 +1,6 @@
+echo "Sales_data:" && ./Sales_data < data/add_item
+
+../noinput compounddecls.exe convs.exe dbl_ptr.exe \
+                  decl-ex.exe escape.exe inits.exe \
+                  literals.exe ref-ex.exe \
+                  scope_levels.exe unsigned.exe
diff --git a/2/runpgms.bat b/2/runpgms.bat
new file mode 100644 (file)
index 0000000..3d93c39
--- /dev/null
@@ -0,0 +1,7 @@
+echo on 
+Sales_data < data/add_item
+
+for %%i in (compounddecls.exe convs.exe dbl_ptr.exe 
+                  decl-ex.exe escape.exe inits.exe 
+                  literals.exe ref-ex.exe 
+                  scope_levels.exe unsigned.exe ) do %%i
diff --git a/2/scope_levels.cc b/2/scope_levels.cc
new file mode 100644 (file)
index 0000000..c622b04
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+
+// Program for illustration purposes only: It is bad style for a function
+// to use a global variable and also define a local variable with the same name
+
+int reused = 42;  // reused has global scope
+
+int main()
+{
+       int unique = 0; // unique has block scope
+
+       // output #1: uses global reused; prints 42 0
+       std::cout << reused << " " << unique << std::endl;   
+
+       int reused = 0; // new, local object named reused hides global reused
+
+       // output #2: uses local reused; prints 0 0
+       std::cout << reused << " " <<  unique << std::endl;  
+
+       // output #3: explicitly requests the global reused; prints 42 0
+       std::cout << ::reused << " " <<  unique << std::endl;  
+
+       return 0;
+}
diff --git a/2/unsigned.cc b/2/unsigned.cc
new file mode 100644 (file)
index 0000000..86bde70
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+
+int main()
+{
+       unsigned u = 10, u2 = 42;
+       std::cout << u2 - u << std::endl;  
+       std::cout << u - u2 << std::endl; 
+
+       int i = 10, i2 = 42;
+       std::cout << i2 - i << std::endl;
+       std::cout << i - i2 << std::endl;
+
+       u = 42;
+       i = 10;
+       std::cout << i - u << std::endl;
+       std::cout << u - i << std::endl;
+
+       u = 10;
+       i = -42;
+       std::cout << i + i << std::endl;  // prints -84
+       std::cout << u + i << std::endl;  // if 32-bit ints, prints 4294967264
+
+       i = 10;
+       std::cout << "good" << std::endl;
+       while (i >= 0) {
+               std::cout << i << std::endl;
+               --i;
+       }
+
+       for (int i = 10; i >= 0; --i)
+               std::cout << i << std::endl;
+
+       for (unsigned u = 0; u <= 10; ++u) 
+               std::cout << u << std::endl;  // prints 0 . . . 10
+
+/* NOTE: the condition in the following loop 
+         will run indefinitely
+       // WRONG: u can never be less than 0; the condition will always succeed
+       for (unsigned u = 10; u >= 0; --u)
+       std::cout << u << std::endl;
+*/
+       u = 11; // start the loop one past the first element we want to print
+       while (u > 0) {
+                --u;        // decrement first, so that the last iteration will print 0
+               std::cout << u << std::endl;  
+       }
+
+       // be wary of comparing ints and unsigned
+       u = 10;
+       i = -42;
+       if (i < u)               // false: i is converted to unsigned
+               std::cout << i << std::endl;
+       else
+               std::cout << u << std::endl;   // prints 10
+
+       u = 42; u2 = 10;
+       std::cout << u - u2 << std::endl; // ok: result is 32
+       std::cout << u2 - u << std::endl; // ok: but the result will wrap around
+}
+       
diff --git a/3/README b/3/README
new file mode 100644 (file)
index 0000000..0c93687
--- /dev/null
+++ b/3/README
@@ -0,0 +1,21 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   add               add_using
+   grades            arrayScores
+   grades            vecScores
+   hexify            hexify
+   ptr_traversal2    ptr_traversal2
+   string_io         string_io
+   string_io2        string_io
+   vecStrings2       vecStrings2
+   word_echo         getline
+   word_echo         string_size2
+   word_echo         string_size3
+   word_echo         word_echo
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/3/add_using.cc b/3/add_using.cc
new file mode 100644 (file)
index 0000000..356c179
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+
+// using declarations for names from the standard library
+using std::cin;
+using std::cout; using std::endl;
+
+int main()
+{
+       cout << "Enter two numbers:" << endl;
+
+       int v1, v2;
+       cin >> v1 >> v2;
+
+       cout << "The sum of " << v1 << " and " << v2
+            << " is " << v1 + v2 << endl;
+
+       return 0;
+}
diff --git a/3/arrayScores.cc b/3/arrayScores.cc
new file mode 100644 (file)
index 0000000..918d25f
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+
+int main ()
+{
+       vector<unsigned> grades;
+       // count the number of grades by clusters of ten: 
+       // 0--9, 10--19, . . . 90--99, 100
+       unsigned scores[11] = {};  // 11 buckets, all value initialized to 0
+       unsigned grade;
+       while (cin >> grade) {
+               if (grade <= 100)
+                       // increment the counter for the current cluster
+                       ++scores[grade/10]; 
+               grades.push_back(grade);
+       }
+       cout << "grades.size = " << grades.size() << endl;
+
+       for (auto g : grades)  // for every element in grades
+               cout << g << " " ;
+       cout << endl;
+
+       for (auto i : scores)       // for each counter in scores
+               cout << i << " ";       // print the value of that counter
+       cout << endl;
+}
diff --git a/3/cctype.cc b/3/cctype.cc
new file mode 100644 (file)
index 0000000..88a808a
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <cctype>
+using std::isupper; using std::toupper;
+using std::islower; using std::tolower;
+using std::isalpha; using std::isspace;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       string s("Hello World!!!");
+       // punct_cnt has the same type that s.size returns
+       decltype(s.size()) punct_cnt = 0; 
+       
+       // count the number of punctuation characters in s
+       for (auto c : s)         // for every char in s
+               if (ispunct(c))      // if the character is punctuation
+                       ++punct_cnt;     // increment the punctuation counter
+       
+       cout << punct_cnt 
+            << " punctuation characters in " << s << endl;
+       
+       // convert s to uppercase
+       string orig = s;
+       for (auto &c : s)   // for every char in s (note: c is a reference)
+               // c is a reference, so this assignment changes the char in s
+               c = toupper(c);
+       cout << s << endl;
+       
+       // convert first word in s to uppercase
+       s = orig;  // restore s to original case
+       decltype(s.size()) index = 0;
+
+       // process characters in s until we run out of characters 
+       // or we hit a whitespace
+       while (index != s.size() && !isspace(s[index])) {
+
+           // s[index] returns a reference so we can change 
+               // the underlying character
+               s[index] = toupper(s[index]);
+
+               // increment the index to look at the next character 
+               // on the next iteration
+               ++index; 
+       }
+       cout << s << endl;
+       
+       return 0;
+}
diff --git a/3/char_repl.cc b/3/char_repl.cc
new file mode 100644 (file)
index 0000000..aa97fa5
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       string str("some string"), orig = str;
+       if (!str.empty())          // make sure there's a character to print
+               cout << str[0] << endl;// print the first character in str
+
+       if (!str.empty())       // make sure there's a character in str[0]
+               // assign a new value to the first character in str
+               str[0] = toupper(str[0]);
+       cout << str << endl;
+
+       str = orig; // restore str to its original value
+
+       // equivalent code using iterators instead of subscripts
+       if (str.begin() != str.end()) { // make sure str is not empty
+               auto it = str.begin();  // it denotes the first character in str
+               *it = toupper(*it);     // make that character uppercase
+       }
+       cout << str << endl;
+
+       str = orig; // restore str to its original value
+
+       // four wasy to capitalize first word in str:
+       // 1. for loop with subscripts
+       for (decltype(str.size()) index = 0; 
+                index != str.size() && !isspace(str[index]); ++index) 
+               str[index] = toupper(str[index]); // capitalize the current character
+       cout << str << endl;
+
+       str = orig; // restore str to its original value
+
+       // 2. for loop with iterators instead of subscripts
+       for (auto it = str.begin(); it != str.end() && !isspace(*it); ++it)
+               *it = toupper(*it); // capitalize the current character
+       cout << str << endl;
+
+       str = orig; // restore str to its original value
+
+       // 3. while instead of a for with subscripts
+       decltype(str.size()) index = 0; // subscript to look at characters in str
+       while (index != str.size() && !isspace(str[index])) {
+               str[index] = toupper(str[index]);  // capitalize the current character
+               ++index;        // advance the index to get the next character
+       }
+       cout << str << endl;
+
+       // 4. while loop with iterators
+       auto beg = str.begin();
+       while (beg != str.end() && !isspace(*beg)) {
+               *beg = toupper(*beg);
+               ++beg;
+       }
+       cout << str << endl;
+
+       str = orig; // restore str to its original value
+
+       // range for loop to process every character
+       // first a loop to print the characters in str one character to a line
+       for (auto c : str)      // for every char in str
+               cout << c << endl;  // print the current character followed by a newline
+
+       // next change every character in str
+       for (auto &c : str)  // note: c is a reference 
+               c = '*';         // assign a new value to the underlying char in str
+       cout << str << endl;
+       
+       str = orig;  // restore str to its original value
+
+       // equivalent code using traditional for loops
+       // first print the characters in str
+       for (decltype(str.size()) ix = 0; ix != str.size(); ++ix)
+               cout << str[ix] << endl;  // print current character followd by a newline
+
+       // next change every character in str
+       for (decltype(str.size()) ix = 0; ix != str.size(); ++ix)
+               str[ix] = '*';  // assigns a new value to the character in str
+       cout << str << endl;
+
+       str = orig;  // restore str to its original value
+
+       // equivalent code using traditional for loops and iterators
+       // first print the characters in str
+       for (auto beg = str.begin(); beg != str.end(); ++beg)
+               cout << *beg << endl;  // print current character followd by a newline
+
+       // next change every character in str
+       for (auto beg = str.begin(); beg != str.end(); ++beg)
+               *beg = '*';  // assigns a new value to the character in str
+       cout << str << endl;
+       return 0;
+}
diff --git a/3/cstring_hdr.cc b/3/cstring_hdr.cc
new file mode 100644 (file)
index 0000000..8d0bf87
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <cstring>
+
+#include <cstddef>
+using std::size_t;
+
+int main() {
+       string s1 = "A string example";
+       string s2 = "A different string";
+       
+       if (s1 < s2)  // false: s2 is less than s1
+               cout << s1 << endl;
+       else
+               cout << s2 << endl;
+       const char ca1[] = "A string example";
+       const char ca2[] = "A different string";
+       
+       if (strcmp(ca1, ca2) < 0) // same effect as string comparison s1 < s2
+               cout << ca1 << endl;
+       else
+               cout << ca2 << endl;
+       
+       const char *cp1 = ca1, *cp2 = ca2;
+       cout << strcmp(cp1, cp2) << endl; // output is positive
+       cout << strcmp(cp2, cp1) << endl; // output is negative
+       cout << strcmp(cp1, cp1) << endl; // output is zero
+       
+       
+       cout << strlen(cp1) << endl; // prints 16; strlen ignores the null
+       
+       const unsigned sz = 16 + 18 + 2;
+       char largeStr[sz];      // will hold the result
+       // disastrous if we miscalculated the size of largeStr
+       strcpy(largeStr, ca1);  // copies ca1 into largeStr
+       strcat(largeStr, " ");  // adds a space at the end of largeStr
+       strcat(largeStr, ca2);  // concatenates ca2 onto largeStr
+       // prints A string example A different string
+       cout << largeStr << endl;       
+       
+       strncpy(largeStr, ca1, sz); // size to copy includes the null
+       if (strlen(ca1) > sz)
+               largeStr[sz-1] = '\0';
+       strncat(largeStr, " ", 2);  // pedantic, but a good habit
+       strncat(largeStr, ca2, sz - strlen(largeStr)); 
+       cout << largeStr << endl;       
+       
+       // initialize large_string as a concatenation of s1, a space, and s2
+       string large_string = s1 + " " + s2; 
+       cout << large_string << endl;       
+       
+       return 0;
+}
diff --git a/3/data/add b/3/data/add
new file mode 100644 (file)
index 0000000..a07a471
--- /dev/null
@@ -0,0 +1 @@
+3 7 
diff --git a/3/data/grades b/3/data/grades
new file mode 100644 (file)
index 0000000..de39b73
--- /dev/null
@@ -0,0 +1,14 @@
+45
+65
+99
+100
+34
+67
+95
+76
+88
+75
+83
+92
+76
+93
diff --git a/3/data/hexify b/3/data/hexify
new file mode 100644 (file)
index 0000000..616d361
--- /dev/null
@@ -0,0 +1 @@
+12 0 5 15 8 15 
diff --git a/3/data/iterarith b/3/data/iterarith
new file mode 100644 (file)
index 0000000..fcaa91d
--- /dev/null
@@ -0,0 +1 @@
+now is the time for all good boys to use their sense
diff --git a/3/data/ptr_traversal2 b/3/data/ptr_traversal2
new file mode 100644 (file)
index 0000000..09b0ff3
--- /dev/null
@@ -0,0 +1 @@
+345 42 5 34 8 -20 16 35 100 200
diff --git a/3/data/string_io b/3/data/string_io
new file mode 100644 (file)
index 0000000..a882a97
--- /dev/null
@@ -0,0 +1 @@
+    Hello World!
diff --git a/3/data/vecStrings2 b/3/data/vecStrings2
new file mode 100644 (file)
index 0000000..2be983a
--- /dev/null
@@ -0,0 +1,9 @@
+When we subscript a string, we get back a character.
+When we subscript a vector, we get an object of whatever type the vector contains.
+We can use the resulting (unnamed) object in exactly the same way we can use any other object of that type.
+In particular, if the type is a class type, such as a library string, we can call member functions or use operators defined by the element type.
+
+As an example, let's write a program to capitalize the first word of every sentence in the first paragraph of some text.
+We'll assume that our input contains a series of lines.
+Each line is either a complete sentence or it contains only a newline.
+Lines containing a only a newline separate paragraphs.
diff --git a/3/data/word_echo b/3/data/word_echo
new file mode 100644 (file)
index 0000000..e544f33
--- /dev/null
@@ -0,0 +1,2 @@
+Now is the time for all good boys
+to come to the aid of the party
diff --git a/3/getline.cc b/3/getline.cc
new file mode 100644 (file)
index 0000000..95764b1
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string> 
+using std::string; using std::getline;
+
+#include <iostream> 
+using std::cin; using std::cout; using std::endl;
+
+int main() 
+{
+       string line;
+
+       // read input a line at a time until end-of-file
+       while (getline(cin, line))
+               cout << line << endl;
+
+       return 0;
+}
diff --git a/3/hexify.cc b/3/hexify.cc
new file mode 100644 (file)
index 0000000..fa43071
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <cstddef>
+using std::size_t;
+
+int main()
+{
+       const string hexdigits = "0123456789ABCDEF";  // possible hex digits
+
+       cout << "Enter a series of numbers between 0 and 15"
+            << " separated by spaces.  Hit ENTER when finished: " 
+            << endl;
+       string result;        // will hold the resulting hexify'd string
+
+       string::size_type n;  // hold numbers from the input
+       while (cin >> n)
+               if (n < hexdigits.size())    // ignore invalid input
+                       result += hexdigits[n];  // fetch the indicated hex digit
+
+       cout << "Your hex number is: " << result << endl;
+
+       return 0;
+}
diff --git a/3/makefile b/3/makefile
new file mode 100644 (file)
index 0000000..7482f1b
--- /dev/null
@@ -0,0 +1,15 @@
+# executable files for this directory
+OBJECTS = add_using.exe arrayScores.exe cctype.exe \
+                 char_repl.exe cstring_hdr.exe getline.exe \
+                 hexify.exe multidim.exe \
+                 ptr_traversal2.exe square.exe strcompare.exe \
+                 string_add.exe string_io.exe string_io2.exe \
+                 string_size.exe string_size2.exe string_size3.exe \
+                 vec_decls.exe vecStrings2.exe vecScores.exe \
+                 vecSubs.exe word_echo.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../1
diff --git a/3/multidim.cc b/3/multidim.cc
new file mode 100644 (file)
index 0000000..579e833
--- /dev/null
@@ -0,0 +1,141 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iterator>
+using std::begin; using std::end;
+
+#include <vector>
+using std::vector;
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <cstddef>
+using std::size_t;
+
+int main()
+{
+       // array of size 3; each element is an array of 4 uninitailzed ints 
+       int ia1[3][4];
+       
+       // array of size 10; each element is a 20-element array 
+       // whose elements are arrays of 30 ints
+       int arr[10][20][30] = {0}; // initialize all elements to 0
+       
+       // assigns the first element of arr to the last element 
+       // in the last row of ia
+       ia1[2][3] = arr[0][0][0];
+       
+       // binds row to the second four-element array in ia 
+       int (&row)[4] = ia1[1];
+       
+       // three elements, each element is an array of size 4
+       int ia2[3][4] = { 
+           {0, 1, 2, 3},   // initializers for the row indexed by 0
+           {4, 5, 6, 7},   // initializers for the row indexed by 1
+           {8, 9, 10, 11}  // initializers for the row indexed by 2
+       };
+       
+       // equivalent initialization without the optional 
+       // nested braces for each row
+       int ia3[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
+       
+       // explicitly initialize only element 0 in each row
+       int ia4[3][4] = {{ 0 }, { 4 }, { 8 }};
+       
+       // explicitly initialize row 0; the remaining elements 
+       // are value initialized
+       int ix[3][4] = {0, 3, 6, 9};
+       
+       // prints 9 0 0 
+       cout << ix[0][3] << ' ' << ix[1][0] << ' ' << ix[2][0] << endl;
+       
+       constexpr size_t rowCnt = 3, colCnt = 4;
+       int ia[rowCnt][colCnt];   // 12 uninitialized elements 
+       
+    // for each row
+    for (size_t i = 0; i != rowCnt; ++i) {
+        // for each column within the row
+        for (size_t j = 0; j != colCnt; ++j) {
+            // assign the element's positional index as its value
+            ia[i][j] = i * colCnt + j;   
+               }
+       }
+
+       // four ways to print the contents of ia
+       // 1. using nested range for loops
+       for (const auto &row : ia) // for every element in the outer array
+               for (auto col : row)   // for every element in the inner array
+                       cout << col << endl; // print the element's value
+    cout << ia[0][0] << ' ' << ia[2][3] << endl; // prints 0 11
+
+
+       // 2. using pointers and a traditional for loop
+       //    with pointer arithmetic to calculate the end pointers 
+       for (auto p = ia; p != ia + rowCnt; ++p) {
+               // q points to the first element of an array of four ints; 
+               // that is, q points to an int
+           for (auto q = *p; q != *p + colCnt; ++q)
+                cout << *q << ' ';
+               cout << endl;
+       }
+       
+       // 3. using pointers and a traditional for loop
+       //    with the library begin and end functions to manage the pointers
+       for (auto p = begin(ia); p != end(ia); ++p) {
+               // q points to the first element in an inner array
+               for (auto q = begin(*p); q != end(*p); ++q)
+                       cout << *q << ' ';  // prints the int value to which q points
+               cout << endl;
+       }
+       
+       // 4. using a type alias to declare the loop control variable
+       using int_array = int[4]; // new style type alias declaration 
+
+       for (int_array *p = ia; p != ia + 3; ++p) {
+           for (int *q = *p; q != *p + 4; ++q)
+                cout << *q << ' ';
+               cout << endl;
+       }
+
+       // alternative way to assign positional index to elements 
+       // in a two-dimensional array
+       int alt_ia[rowCnt][colCnt]; // 12 uninitialized elements 
+       size_t cnt = 0;
+       for (auto &row : alt_ia)    // for every element in the outer array
+               for (auto &col : row) { // for every element in the inner array
+                       col = cnt;          // give this element the next value
+                       ++cnt;              // increment cnt
+               }
+       // now print the value of the array
+       for (const auto &row : alt_ia) // for every element in the outer array
+               for (auto col : row)     // for every element in the inner array
+                       cout << col << endl;
+
+       return 0;
+}
diff --git a/3/ptr_traversal2.cc b/3/ptr_traversal2.cc
new file mode 100644 (file)
index 0000000..3c8d419
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iterator>
+using std::begin; using std::end;
+
+#include <cstddef>
+using std::size_t; 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       int ia[] = {0,1,2,3,4,5,6,7,8,9};
+
+       int *p = ia; // p points to the first element in ia
+       ++p;           // p points to ia[1]
+
+       int *e = &ia[10]; // pointer just past the last element in ia
+       for (int *b = ia; b != e; ++b)
+               cout << *b << " "; // print the elements in ia
+       cout << endl;
+
+       const size_t sz = 10;
+       int arr[sz];  // array of 10 ints
+
+       for (auto &n : arr) // for each element in arr
+               cin >> n;  // read values from the standard input
+
+       for (auto i : arr)
+               cout << i << " ";
+       cout << endl;
+
+       // pbeg points to the first and 
+       // pend points just past the last element in arr
+       int *pbeg = begin(arr),  *pend = end(arr);
+
+       // find the first negative element, 
+       // stopping if we've seen all the elements
+       while (pbeg != pend && *pbeg >= 0)
+               ++pbeg;
+       if (pbeg == pend)
+               cout << "no negative elements in arr" << endl;
+       else
+               cout << "first negative number was " << *pbeg << endl;
+       return 0;
+}
+
diff --git a/3/runpgms b/3/runpgms
new file mode 100644 (file)
index 0000000..516b108
--- /dev/null
+++ b/3/runpgms
@@ -0,0 +1,17 @@
+echo "add:" && ./add_using < data/add
+echo "arrayScores:" && ./arrayScores < data/grades
+echo "hexify:" && ./hexify < data/hexify
+echo "ptr_traversal2:" && ./ptr_traversal2 < data/ptr_traversal2
+echo "string_io:" && ./string_io < data/string_io
+echo "string_io2:" && ./string_io2 < data/string_io
+echo "vecStrings2:" && ./vecStrings2 < data/vecStrings2
+echo "vecScores:" && ./vecScores < data/grades
+echo "getline:" && ./getline < data/word_echo
+echo "string_size2:" && ./string_size2 < data/word_echo
+echo "string_size3:" && ./string_size3 < data/word_echo
+echo "word_echo:" && ./word_echo < data/word_echo
+
+../noinput cctype.exe char_repl.exe cstring_hdr.exe \
+                  multidim.exe square.exe strcompare.exe \
+                  string_add.exe string_size.exe vec_decls.exe \
+                  vecSubs.exe
diff --git a/3/square.cc b/3/square.cc
new file mode 100644 (file)
index 0000000..0252115
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cout; using std::endl; 
+
+int main()
+{
+       vector<int> v{1,2,3,4,5,6,7,8,9};
+       for (auto &i : v) // for each element in v (note: i is a reference)
+               i *= i;           // square the element value
+       for (auto i : v)      // for each element in v
+               cout << i << " "; // print the element
+       cout << endl;
+
+       return 0;
+}
+
diff --git a/3/strcompare.cc b/3/strcompare.cc
new file mode 100644 (file)
index 0000000..07690e3
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+    string str = "Hello";
+    string phrase = "Hello World";
+    string slang  = "Hiya";
+
+    if (str < phrase) cout << "str is smaller" << endl;
+    if (slang > str) cout << "slang is greater" << endl;
+    if (slang > phrase) cout << "slang is greater" << endl;
+
+    return 0;
+}
diff --git a/3/string_add.cc b/3/string_add.cc
new file mode 100644 (file)
index 0000000..d78b1e9
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+int main() 
+{
+
+       string s1  = "hello, ", s2 = "world\n";
+       string s3 = s1 + s2;   // s3 is hello, world\n
+       cout << s1 << s2 << s3 << endl;
+
+       s1 += s2;   // equivalent to s1 = s1 + s2
+       cout << s1;
+       
+       string s4 = "hello", s5 = "world";  // no punctuation in s4 or s2
+       string s6 = s4 + ", " + s5 + '\n';
+       cout << s4 << s5 << "\n" << s6 << endl;
+       
+       return 0;
+}
diff --git a/3/string_io.cc b/3/string_io.cc
new file mode 100644 (file)
index 0000000..6be2dce
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cin;
+using std::cout;
+using std::endl;
+
+int main()
+{
+    string s;          // empty string
+    cin >> s;          // read a whitespace-separated string into s
+    cout << s << endl; // write s to the output
+    return 0;
+}
diff --git a/3/string_io2.cc b/3/string_io2.cc
new file mode 100644 (file)
index 0000000..28a3ecc
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       string s1, s2;
+
+       cin >> s1 >> s2; // read first input into s1, second into s2
+       cout << s1 << s2 << endl; // write both strings
+
+       return 0;
+}
diff --git a/3/string_size.cc b/3/string_size.cc
new file mode 100644 (file)
index 0000000..1ece68c
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+string st1;       // empty string
+string st2(st1);  // st2 is a copy of st1
+
+int main()
+{
+    string st("The expense of spirit\n");
+    cout << "The size of " << st << "is " << st.size()
+         << " characters, including the newline" << endl;
+    return 0;
+}
diff --git a/3/string_size2.cc b/3/string_size2.cc
new file mode 100644 (file)
index 0000000..1b2bd07
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <string> 
+using std::string; using std::getline;
+
+#include <iostream> 
+using std::cin; using std::cout; using std::endl;
+
+int main() 
+{
+       string line;
+
+       // read input a line at a time and print lines that are longer than 80 characters
+       while (getline(cin, line))
+               if (line.size() > 80)
+                       cout << line << endl;
+
+       return 0;
+}
diff --git a/3/string_size3.cc b/3/string_size3.cc
new file mode 100644 (file)
index 0000000..005c13c
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string> 
+using std::string; using std::getline;
+
+#include <iostream> 
+using std::cin; using std::cout; using std::endl;
+
+int main() 
+{
+       string line;
+
+       // read input a line at a time and discard blank lines
+       while (getline(cin, line))
+               if (!line.empty())
+                       cout << line << endl;
+
+       return 0;
+}
diff --git a/3/vecScores.cc b/3/vecScores.cc
new file mode 100644 (file)
index 0000000..835d982
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       // hold the grades we read from the standard input
+       vector<unsigned> grades;
+
+       // count the number of grades by clusters of ten: 
+       // 0--9, 10--19, . ..  90--99, 100
+       vector<unsigned> scores(11, 0); // 11 buckets, all initially 0
+       unsigned grade;
+       while (cin >> grade) {      // read the grades
+               if (grade <= 100)       // handle only valid grades
+                       grades.push_back(grade);
+                       ++scores[grade/10]; // increment the counter for the current cluster
+       }
+       cout << "grades.size = " << grades.size() << endl;
+       for (auto it : grades)
+               cout << it << " " ;
+       cout << endl;
+
+       cout << "scores.size = " << scores.size() << endl;
+       for (auto it : scores)
+               cout << it << " " ;
+       cout << endl;
+
+       // equivalent program using iterators instead of subscripts
+       vector<unsigned> alt_scores(11, 0);  // 11 buckets, all initially 0
+       // for each grade in the input
+       for (auto it = grades.begin(); it != grades.end(); ++it) {
+               unsigned i = *it;
+               // increment the counter for the current cluster
+               ++(*(alt_scores.begin() + i/10));
+       }
+
+       cout << "alt_scores.size = " << alt_scores.size() << endl;
+       for (auto it = alt_scores.begin(); it != alt_scores.end(); ++it) 
+               cout << *it << " " ;
+       cout << endl;
+
+}
+
diff --git a/3/vecStrings2.cc b/3/vecStrings2.cc
new file mode 100644 (file)
index 0000000..48bc9de
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <vector>
+using std::vector; 
+
+#include <string>
+using std::string;
+
+int main()
+{
+       vector<string> text;     // holds the input
+
+       string s;
+       while (getline(cin, s))  // read the entire input file
+               text.push_back(s);   // storing each line as an element in text
+       cout << "text.size: " << text.size() << endl;
+
+       // print each line in text up to the first blank line
+       for (auto it = text.cbegin(); 
+            it != text.cend() && !(*it).empty(); ++it) 
+               cout << *it << endl;
+
+       // equivalent loop using arrow to dereference it and call empty
+       for (auto it = text.cbegin(); 
+            it != text.cend() && !it->empty(); ++it) 
+               cout << *it << endl;
+}
+               
diff --git a/3/vecSubs.cc b/3/vecSubs.cc
new file mode 100644 (file)
index 0000000..191ff2e
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       vector<int> v = {0,1,2,3,4,5,6,7,8,9};
+       auto sz = v.size(); 
+       decltype(sz) i = 0;
+       // duplicate contents of v onto the back of v
+       while (i != sz) {
+               v.push_back(*v.begin() + i);
+               ++i;
+       }
+       // prints 0...9 0...9
+       for (auto it : v)
+               cout << it << " ";
+       cout << endl;
+       
+       // alternative way to stop when we get to the original last element
+       vector<int> alt_v = {0,1,2,3,4,5,6,7,8,9}; // vector with values 0...9
+       for (decltype(alt_v.size()) i = 0, sz = alt_v.size(); i != sz; ++i)
+               alt_v.push_back(alt_v[i]);
+
+       // prints 0...9 0...9
+       for (auto it : alt_v)
+               cout << it << " ";
+       cout << endl;
+
+       vector<int> v2 = {0,1,2,3,4,5,6,7,8,9}; // vector with values 0 ... 9
+       decltype(v2.size()) ix = 0;   // we'll use ix to index the vector
+
+       // set the elements with values less than 5 to 0
+       while (ix != v2.size() && v2[ix] < 5) {
+               v2[ix] = 0; // changes the value of the element in v
+               ++ix;       // increment the index so the next iteration fetches the next element
+       }
+
+       // print the elements using subscripts
+       for (unsigned i = 0; i != v2.size(); ++i)
+               cout << v2[i] << " ";
+       cout << endl;
+
+       // equivalent but using iterators
+       vector<int> alt_v2 = {0,1,2,3,4,5,6,7,8,9}; // vector with values 0...9
+       // set the elements to 0 up to the first one that is 5 or greater
+       auto it = alt_v2.begin();
+       while (it != alt_v2.end() && *it < 5) {
+               *it = 0;   // changes the value of the element in alt_v2
+               ++it;      // advance the iterator to denote the next element
+       }
+       for (auto it = alt_v2.begin(); // it denotes first element in alt_v2
+                 it != alt_v2.end();  // so long as it denotes an element
+                 ++it)          // increment the iterator to next element
+               cout << *it << " ";  // print element denoted by it from alt_v2
+       cout << endl;
+
+       return 0;
+}
diff --git a/3/vec_decls.cc b/3/vec_decls.cc
new file mode 100644 (file)
index 0000000..6b8a7a5
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <vector>
+using std::vector;
+
+#include "Sales_item.h"
+
+int main()
+{
+       // list initialization, articles has 3 elements
+       vector<string> articles = {"a", "an", "the"};
+
+       vector<string> svec; // default initialization; svec has no elements
+       vector<int> ivec;             // ivec holds objects of type int
+       vector<Sales_item> Sales_vec; // holds Sales_items
+       
+       vector<vector<string>> file;  // vector whose elements are vectors
+       vector<vector<int>> vecOfvec; // each element is itself a vector
+       
+       // all five vectors have size 0
+       cout << svec.size() << " " << ivec.size() << " "
+            << Sales_vec.size() << " "
+            << file.size() << " " << vecOfvec.size() << endl;
+       
+       vector<int> ivec2(10);     // ten elements, each initialized to 0
+       vector<int> ivec3(10, -1); // ten int elements, each initialized to -1
+       vector<string> svec2(10);  // ten elements, each an empty string
+       vector<string> svec3(10, "hi!"); // ten strings; each element is "hi!"
+       cout << ivec2.size() << " " << ivec3.size() << " "
+            << svec2.size() << " " << svec3.size() << endl;
+       
+       // 10 is not a string, so cannot be list initialization
+       vector<string> v1(10); // construct v1 with ten value-initialized elements
+       vector<string> v2{10}; // ten elements value-initialized elements
+       vector<string> v3(10, "hi");  // ten elements with value "hi"
+       // again list initialization is not viable, so ordinary construction
+       vector<string> v4{10, "hi"};  // ten elements with values "hi"
+       
+       // all four vectors have size ten
+       cout << v1.size() << " " << v2.size() 
+            << " " << v3.size() << " " << v4.size() << endl;
+       
+       vector<string> vs1{"hi"}; // list initialization: vs1 has 1 element 
+       vector<string> vs2{10};   // ten default-initialized elements
+       vector<string> vs3{10, "hi"}; // has ten elements with value "hi"
+       cout << vs1.size() << " " << vs2.size() << " " << vs3.size() << endl;
+       
+       vector<int> v5(10, 1);  // ten elements with value 1
+       vector<int> v6{10, 1};  // two elements with values 10 and 1
+       cout << v5.size() << " " << v6.size() << endl;
+       
+       // intention is clearer
+       vector<int> alt_v3 = {10};    // one element with value 10
+       vector<int> alt_v4 = {10, 1}; // two elements with values 10 and 1
+       cout << alt_v3.size() << " " << alt_v4.size() << endl;
+
+       return 0;
+}
diff --git a/3/word_echo.cc b/3/word_echo.cc
new file mode 100644 (file)
index 0000000..bea4369
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream> 
+using std::cin; using std::cout; using std::endl;
+
+#include <string> 
+using std::string;
+
+int main() 
+{
+       string word;
+       while (cin >> word)       // read until end-of-file 
+               cout << word << endl; // write each word followed by a new line
+       return 0;
+}
diff --git a/4/README b/4/README
new file mode 100644 (file)
index 0000000..9fe3dd8
--- /dev/null
+++ b/4/README
@@ -0,0 +1,10 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   vecSubs          cond
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/4/arith-ex.cc b/4/arith-ex.cc
new file mode 100644 (file)
index 0000000..41744e3
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       cout << -30 * 3 + 21 / 5 << endl;
+       
+       cout << -30 + 3 * 21 / 5 << endl;
+       
+       cout << 30 / 3 * 21 % 5 << endl;
+       
+       cout << 30 / 3 * 21 % 4 << endl;
+       
+       cout << -30 / 3 * 21 % 4 << endl;
+       
+       cout << 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2 << endl;
+       
+       return 0;
+}
diff --git a/4/arraysizeof.cc b/4/arraysizeof.cc
new file mode 100644 (file)
index 0000000..897c750
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <cstddef>
+using std::size_t;
+
+int ia[] = {0,1,2,3,4,5,6,7,8,9};
+
+int main()
+{
+   // sizeof(ia)/sizeof(*ia) returns the number of elements in ia
+   constexpr size_t sz = sizeof(ia)/sizeof(*ia);
+
+   int arr2[sz];  // ok sizeof returns a constant expression 
+
+   cout << "ia size: " << sz << endl;
+
+   return 0;
+}
diff --git a/4/cond.cc b/4/cond.cc
new file mode 100644 (file)
index 0000000..154a8a9
--- /dev/null
+++ b/4/cond.cc
@@ -0,0 +1,58 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <vector>
+using std::vector;
+
+vector<unsigned> grades;
+
+int main()
+{
+       unsigned i;
+       while (cin >> i)
+               grades.push_back(i);
+
+       for (auto grade : grades) {  // for each grade in grades
+               // set the initial grade as pass or fail
+               string finalgrade = (grade < 60) ?  "fail" : "pass";
+
+               finalgrade = (grade > 90) ? "high pass"
+                                                 : (grade < 60) ? "fail" : "pass";
+
+               cout << grade << " " + finalgrade << endl;
+       }
+
+       return 0;
+}
diff --git a/4/data/oofe b/4/data/oofe
new file mode 100644 (file)
index 0000000..c4e1776
--- /dev/null
@@ -0,0 +1 @@
+0 -100 1 -1 2 -3 3 -3 4 -4 5 -5 6 -6 7 -7 8 -8 9 -9
diff --git a/4/data/vecSubs b/4/data/vecSubs
new file mode 100644 (file)
index 0000000..cf37e78
--- /dev/null
@@ -0,0 +1,14 @@
+42
+65
+95
+100
+39
+67
+95
+76
+88
+76
+83
+92
+76
+93
diff --git a/4/incr.cc b/4/incr.cc
new file mode 100644 (file)
index 0000000..79786ec
--- /dev/null
+++ b/4/incr.cc
@@ -0,0 +1,43 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main() 
+{
+       int i = 0, j;
+       j = ++i; // j = 1, i = 1: prefix yields the incremented value
+       cout << i << " " << j << endl;
+
+       j = i++; // j = 1, i = 2: postfix yields the unincremented value
+       cout << i << " " << j << endl;
+
+       return 0;
+}
diff --git a/4/int-div.cc b/4/int-div.cc
new file mode 100644 (file)
index 0000000..03f798d
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main() 
+{
+       // ival1 is 3; result is truncated; remainder is discarded
+       int ival1 = 21/6;
+
+       // ival2 is 3; no remainder; result is an integral value
+       int ival2 = 21/7;
+
+       cout << ival1 << " " << ival2 << endl;
+
+       return 0;
+}
diff --git a/4/makefile b/4/makefile
new file mode 100644 (file)
index 0000000..893e4b4
--- /dev/null
@@ -0,0 +1,11 @@
+# executable files for this directory
+OBJECTS = arith-ex.exe arraysizeof.exe cond.exe \
+                 incr.exe int-div.exe oofe.exe overflow.exe \
+                 prec.exe shift-prec.exe sizeof_pgm.exe \
+                 unaryOps.exe vec_init.exe warn_dbltoint.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../2
diff --git a/4/oofe.cc b/4/oofe.cc
new file mode 100644 (file)
index 0000000..c4cff96
--- /dev/null
+++ b/4/oofe.cc
@@ -0,0 +1,82 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <cctype>
+using std::toupper;
+
+// chapter 6 will explain functions
+// tolower and toupper change the argument itself, not a local copy
+string &tolower(string &s)
+{
+       for (auto &i : s)
+               i = tolower(i);
+       return s;
+}
+
+string &toupper(string &s)
+{
+       for (auto &i : s)
+               i = toupper(i);
+       return s;
+}
+
+int main()
+{
+       int i = 0;
+       cout << i << " " << ++i << endl;  // undefined
+
+       string s("a string"), orig = s;
+       cout << toupper(s) << endl;  // changes s to  uppercase
+       cout << tolower(s) << endl;  // changes s to lowercase
+
+       s = orig;
+       // the calls to toupper and tolower change the value of s
+       // << doesn't guarantee order of evaluation, 
+       // so this expression is undefined
+       cout << toupper(s) << " " << tolower(s) << endl; 
+
+       string first = toupper(s);  // we control the order of evaluation
+       string second = tolower(s); // by storing the results in the order in which we want
+
+       cout << first << " " << second << endl;  // one possible evaluation
+       cout << second << " " << first << endl;  // equally legal evaluation!
+       cout << first << " " << first << endl;   // another legal evaluation!
+       cout << second << " " << second << endl; // and a fourth!
+
+       return 0;
+}
diff --git a/4/overflow.cc b/4/overflow.cc
new file mode 100644 (file)
index 0000000..8b54dd1
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main() 
+{
+       short short_value = 32767; // max value if shorts are 16 bits
+
+       short_value += 1; // this calculation overflows
+       cout << "short_value: " << short_value << endl;
+
+    return 0;
+}
diff --git a/4/prec.cc b/4/prec.cc
new file mode 100644 (file)
index 0000000..712dc7b
--- /dev/null
+++ b/4/prec.cc
@@ -0,0 +1,52 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main() 
+{
+       cout << 6 + 3 * 4 / 2 + 2 << endl;
+
+       // parentheses in this expression match default precedence and associativity
+       cout << ((6 + ((3 * 4) / 2)) + 2) << endl; // prints 14
+
+       int temp = 3 * 4;         // 12
+       int temp2 = temp / 2;     // 6
+       int temp3 = temp2 + 6;    // 12
+       int result = temp3 + 2;   // 14
+       cout << result << endl;
+
+       // parentheses result in alternative groupings
+       cout << (6 + 3) * (4 / 2 + 2) << endl;     // prints 36
+       cout << ((6 + 3) * 4) / 2 + 2 << endl;     // prints 20
+       cout << 6 + 3 * 4 / (2 + 2) << endl;       // prints 9
+
+    return 0;
+}
diff --git a/4/runpgms b/4/runpgms
new file mode 100644 (file)
index 0000000..b0b141f
--- /dev/null
+++ b/4/runpgms
@@ -0,0 +1,6 @@
+echo "cond:" && ./cond < data/vecSubs
+
+../noinput arith-ex.exe arraysizeof.exe \
+                  incr.exe int-div.exe oofe.exe overflow.exe \
+                  prec.exe shift-prec.exe sizeof_pgm.exe \
+                  unaryOps.exe vec_init.exe warn_dbltoint.exe
diff --git a/4/shift-prec.cc b/4/shift-prec.cc
new file mode 100644 (file)
index 0000000..420ef3c
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       int grade = 75;
+       cout << ((grade < 60) ?  "fail" : "pass"); // prints pass or fail
+       cout << endl;
+
+       cout << (grade < 60) ?  "fail" : "pass";   // prints 1 or 0!
+       cout << endl;
+
+       // previous expression is equivalent to the following
+       cout << (grade < 60);    // prints 1 or 0
+       cout ?  "fail" : "pass"; // test cout and then yield one of the two literals
+                                // depending on whether cout is true or false
+       cout << endl;
+       int i = 15, j = 20;
+       cout << (i < j ? i : j);  // ok: prints smaller of i and j
+       cout << endl;
+
+       cout << (i < j) ? i : j;  // ok: prints 1 or 0!
+       cout << endl;
+       
+       // previous expression is equivalent to the following
+       cout << (i < j); // prints 1 or 0
+       cout ? i : j;    // test cout and then evaluate i or j
+                        // depending on whether cout evaluates to true or false
+       cout << endl;
+
+       return 0;
+}
diff --git a/4/sizeof_pgm.cc b/4/sizeof_pgm.cc
new file mode 100644 (file)
index 0000000..ccaa273
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include "Sales_data.h"
+
+int main() 
+{
+       Sales_data data, *p;
+       sizeof(Sales_data); // size required to hold an object of type Sales_data 
+       sizeof data; // size of data's type, i.e., sizeof(Sales_data)
+       sizeof p;    // size of a pointer
+       sizeof *p;   // size of the type to which p points, i.e., sizeof(Sales_data)
+       sizeof data.revenue; // size of the type of Sales_data's revenue member 
+       sizeof Sales_data::revenue; // alternative way to get the size of revenue
+       
+       cout << "short: " << sizeof(short) << "\n"
+            << "short[3]: " << sizeof(short[3]) << "\n"
+            << "short*: " << sizeof(short*) << "\n"
+            << "short&: " << sizeof(short&) << endl;
+       
+       cout << endl; 
+       
+       cout << "int: " << sizeof(int) << "\n"
+            << "int[3]: " << sizeof(int[3]) << "\n"
+            << "int*: " << sizeof(int*) << "\n"
+            << "int&: " << sizeof(int&) << endl;
+       
+       cout << endl; 
+       
+       cout << "Sales_data: " << sizeof(Sales_data) << "\n"
+            << "Sales_data[3]: " << sizeof(Sales_data[3]) << "\n"
+            << "Sales_data*: " << sizeof(Sales_data*) << "\n"
+            << "Sales_data&: " << sizeof(Sales_data&) << endl;
+       
+       cout << "Sales_data::revenue: " << sizeof Sales_data::revenue << "\n"
+            << "data.revenue: " << sizeof data.revenue << endl;
+
+       int x[10];   
+       int *ip = x;
+
+       // number of elements in x
+       cout << sizeof(x)/sizeof(*x) << endl;
+
+       // divides sizeof a pointer by sizeof an int
+       cout << sizeof(ip)/sizeof(*ip) << endl;
+       
+       return 0;
+}
diff --git a/4/unaryOps.cc b/4/unaryOps.cc
new file mode 100644 (file)
index 0000000..f257999
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       int i = 1024;
+       int k = -i; // i is -1024
+       
+       bool b = true;
+       bool b2 = -b; // b2 is true!
+       
+       cout << b << " " << b2 << " " << endl;
+       
+       return 0;
+}
diff --git a/4/vec_init.cc b/4/vec_init.cc
new file mode 100644 (file)
index 0000000..c09edda
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+    
+    vector<int> ivec;                // empty vector
+    int cnt = 10;
+    // add elements 10 . . . 1 to ivec
+    while (cnt > 0)
+        ivec.push_back(cnt--);       // int postfix decrement
+
+    auto iter = ivec.begin();
+    // prints 10 9 8 . . . 1 
+    while (iter != ivec.end())
+        cout << *iter++ << endl; // iterator postfix increment
+
+       vector<int> vec2(10, 0);  // ten elements initially all 0
+    cnt = vec2.size();
+    // assign values from size . . . 1 to the elements in vec2
+    for(vector<int>::size_type ix = 0; 
+                    ix != vec2.size(); ++ix, --cnt)   
+        vec2[ix] = cnt;
+
+    iter = vec2.begin();
+    // prints 10 9 8 . . . 1 
+    while (iter != vec2.end())
+        cout << *iter++ << endl; // iterator postfix increment
+
+    return 0;
+}
diff --git a/4/warn_dbltoint.cc b/4/warn_dbltoint.cc
new file mode 100644 (file)
index 0000000..89fb24f
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       // the compiler might warn about loss of precision
+       int ival = 3.541 + 3; // the compiler might warn about loss of precision
+       cout << ival << endl;  // prints 6
+
+       return 0;
+}
diff --git a/5/README b/5/README
new file mode 100644 (file)
index 0000000..32881b5
--- /dev/null
+++ b/5/README
@@ -0,0 +1,15 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   add_item_data    add_item2
+   doWhile          doWhile
+   grades           ifgrades
+   guesses          guess
+   readme           othercnt
+   readme           vowels 
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/5/add_item2.cc b/5/add_item2.cc
new file mode 100644 (file)
index 0000000..e4266b6
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <stdexcept>
+using std::runtime_error;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include "Sales_item.h"
+
+int main() 
+{
+       Sales_item item1, item2;
+       
+       while (cin >> item1 >> item2) {
+           try {
+               // execute code that will add the two Sales_items
+               // if the addition fails, the code throws a runtime_error exception
+               // first check that the data are for the same item 
+               if (item1.isbn() != item2.isbn())
+                   throw runtime_error("Data must refer to same ISBN");
+       
+               // if we're still here, the ISBNs are the same
+               cout << item1 + item2 << endl;
+           } catch (runtime_error err) {
+               // remind the user that the ISBNs must match 
+                       // and prompt for another pair
+               cout << err.what() 
+                    << "\nTry Again?  Enter y or n" << endl;
+               char c;
+               cin >> c;
+               if (!cin || c == 'n')
+                   break;      // break out of the while loop
+           }  // ends the catch clause
+       }  // ends the while loop
+       
+       return 0;   // indicate success
+}
diff --git a/5/data/add_item_data b/5/data/add_item_data
new file mode 100644 (file)
index 0000000..f25d0a9
--- /dev/null
@@ -0,0 +1,10 @@
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
+0-999-78345-X 3 20.00
+0-201-78345-X 2 25.00
+y
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
+0-999-78345-X 3 20.00
+0-201-78345-X 2 25.00
+n
diff --git a/5/data/doWhile b/5/data/doWhile
new file mode 100644 (file)
index 0000000..8eb0402
--- /dev/null
@@ -0,0 +1,4 @@
+10 42
+y
+3 17
+n
diff --git a/5/data/grades b/5/data/grades
new file mode 100644 (file)
index 0000000..cf37e78
--- /dev/null
@@ -0,0 +1,14 @@
+42
+65
+95
+100
+39
+67
+95
+76
+88
+76
+83
+92
+76
+93
diff --git a/5/data/guesses b/5/data/guesses
new file mode 100644 (file)
index 0000000..cabc403
--- /dev/null
@@ -0,0 +1,3 @@
+5
+9
+7
diff --git a/5/data/readme b/5/data/readme
new file mode 100644 (file)
index 0000000..05a439d
--- /dev/null
@@ -0,0 +1,14 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   add_item_data    add_item2
+   vecSubs          ifgrades
+   readme           othercnt
+   readme           vowels 
+   doWhile          doWhile
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/5/doWhile.cc b/5/doWhile.cc
new file mode 100644 (file)
index 0000000..415d842
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+int main()
+{
+    // repeatedly ask the user for a pair of numbers to sum
+    string rsp;  // used in the condition; can't be defined inside the do
+    do {
+        cout << "please enter two values: ";
+        int val1 = 0, val2 = 0;
+        cin  >> val1 >> val2;
+        cout << "The sum of " << val1 << " and " << val2
+             << " = " << val1 + val2 << "\n\n"
+             << "More? Enter yes or no: ";
+        cin  >> rsp;
+    } while (!rsp.empty() && rsp[0] != 'n');
+
+       cout << endl;
+
+       return 0;
+}
diff --git a/5/guess.cc b/5/guess.cc
new file mode 100644 (file)
index 0000000..bcdce21
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       do {
+               cout << "Guess a number between 0 and 9" << endl;
+               unsigned i, mynum = 7;
+               cin >> i;
+
+               if (i == mynum) {
+                       cout << "Congrats you guessed right!" << endl;
+                       break;
+               } else
+                       if (i < mynum)
+                               cout << "too low" << endl;
+                       else
+                               cout << "too high" << endl;
+
+               if (i != mynum)
+                       if (i < mynum)
+                               cout << "too low" << endl;
+                       else
+                               cout << "too high" << endl;
+               else {
+                       cout << "Congrats you guessed right!" << endl;
+                       break;
+               }
+       } while (true);
+
+       return 0;
+}
diff --git a/5/ifgrades.cc b/5/ifgrades.cc
new file mode 100644 (file)
index 0000000..b211031
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::endl; using std::cin; using std::cout;
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+const vector<string> scores = {"F", "D", "C", "B", "A", "A++"};
+vector<unsigned> grades;
+
+// these functions demonstrate alternative ways to handle the if tests
+// function that takes an unsigned value and a string 
+// and returns a string
+string goodVers(string lettergrade, unsigned grade)
+{
+       // add a plus for grades the end in 8 or 9 and a minus for those ending in 0, 1, or 2
+       if (grade % 10 > 7)
+               lettergrade += '+';        // grades ending in 8 or 9 get a '+'
+       else 
+               if (grade % 10 < 3)
+                       lettergrade += '-';   // those ending in 0, 1, or 2 get a '-'
+       return lettergrade;
+}
+
+// incorrect version of the function to add a plus or minus to a grade
+string badVers(string lettergrade, unsigned grade)
+{
+       // add a plus for grades the end in 8 or 9 and a minus for those ending in 0, 1, or 2
+       // WRONG: execution does NOT match indentation; the else goes with the inner if
+       if (grade % 10 >= 3) 
+               if (grade % 10 > 7) 
+                       lettergrade += '+';  // grades ending in 8 or 9 get a +
+       else
+               lettergrade += '-'; // grades ending in 3, 4, 5, 6 will get a minus!
+       return lettergrade;
+}
+
+// corrected version using the same logic path as badVers
+string rightVers(string lettergrade, unsigned grade)
+{
+       // add a plus for grades that end in 8 or 9 and a minus for those ending in 0, 1, or 2
+       if (grade % 10 >= 3) {
+               if (grade % 10 > 7) 
+                       lettergrade += '+';  // grades ending in 8 or 9 get a +
+       } else                  // curlies force the else to go with the outer if
+               lettergrade += '-'; // grades ending in 0, 1, or 2 will get a minus
+       return lettergrade;
+}
+
+int main()
+{
+       // read a set of scores from the input
+       unsigned grade;
+       while (cin >> grade) 
+               grades.push_back(grade);
+
+       // now process those grades
+       for (auto it : grades) {   // for each grade we read
+               cout << it << " " ;    // print the grade
+               string lettergrade;    // hold coresponding letter grade
+               // if failing grade, no need to check for a plus or minus
+               if (it < 60)
+                       lettergrade = scores[0];
+               else {
+                       lettergrade = scores[(it - 50)/10];  // fetch the letter grade
+                       if (it != 100)  // add plus or minus only if not already an A++
+                               if (it % 10 > 7)
+                                       lettergrade += '+';   // grades ending in 8 or 9 get a +
+                               else if (it % 10 < 3)
+                                       lettergrade += '-';   // grades ending in 0, 1, or 2 get a -
+               }
+               cout << lettergrade << endl;
+               if (it > 59 && it !=100) {
+                       cout << "alternative versions: " << it << " ";
+                       // start over with just the basic grade, no + or -
+                       lettergrade = scores[(it - 50)/10];
+                       cout << goodVers(lettergrade, it) << " ";
+                       cout << badVers(lettergrade, it) << " ";
+                       cout << rightVers(lettergrade, it) << " ";
+                       cout << endl;
+               }
+       }
+
+       return 0;
+}
diff --git a/5/makefile b/5/makefile
new file mode 100644 (file)
index 0000000..48a61f0
--- /dev/null
@@ -0,0 +1,10 @@
+# executable files for this directory
+OBJECTS = add_item2.exe doWhile.exe guess.exe ifgrades.exe \
+                 othercnt.exe rangefor.exe vowels.exe 
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../1
+
diff --git a/5/othercnt.cc b/5/othercnt.cc
new file mode 100644 (file)
index 0000000..a0d2aff
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+    char ch;
+    // initialize counters for each vowel
+    unsigned vowelCnt = 0;
+    unsigned otherCnt = 0;  // count anything that isn't a vowel
+
+    while (cin >> ch) {
+        // if ch is a vowel, increment the appropriate counter
+        switch (ch) {
+                       case 'a': case 'e': case 'i': case 'o': case 'u':
+                               ++vowelCnt;
+                               break;
+            default:
+                ++otherCnt;
+                break;
+        }
+    }
+    // print results
+    cout << "Number of vowels: \t" << vowelCnt << '\n'
+         << "Total non-vowels : \t" << otherCnt << '\n';
+
+    return 0;
+}
diff --git a/5/rangefor.cc b/5/rangefor.cc
new file mode 100644 (file)
index 0000000..1d4752c
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+int main()
+{
+       vector<int> ivec;
+       vector<int> v = {0,1,2,3,4,5,6,7,8,9};
+
+       // range variable must be a reference so we can write to the elements
+       for (auto &r : v)   // for each element in v
+               r *= 2;         // double the value of each element in v
+       
+       // print every element in v
+       for (int r : v)
+               cout << r << " "; // print the elements in v
+       cout << endl;
+
+       return 0;
+}
diff --git a/5/runpgms b/5/runpgms
new file mode 100644 (file)
index 0000000..5f4a110
--- /dev/null
+++ b/5/runpgms
@@ -0,0 +1,8 @@
+echo "add_item2:" && ./add_item2 < data/add_item_data
+echo "guess:" && ./guess < data/guesses
+echo "ifgrades:" && ./ifgrades < data/grades
+echo "othercnt:" && ./othercnt < data/readme
+echo "vowels:" && ./vowels < data/readme
+echo "doWhile:" && ./doWhile < data/doWhile
+
+../noinput rangefor.exe
diff --git a/5/vowels.cc b/5/vowels.cc
new file mode 100644 (file)
index 0000000..712b260
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+    // initialize counters for each vowel
+    unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
+
+    char ch;
+    while (cin >> ch) {
+       // if ch is a vowel, increment the appropriate counter
+       switch (ch) {
+               case 'a':
+                       ++aCnt;
+                       break;
+               case 'e':
+                       ++eCnt;
+                       break;
+               case 'i':
+                       ++iCnt;
+                       break;
+               case 'o':
+                       ++oCnt;
+                       break;
+               case 'u':
+                       ++uCnt;
+                       break;
+       }
+    }
+    // print results
+    cout << "Number of vowel a: \t" << aCnt << '\n'
+         << "Number of vowel e: \t" << eCnt << '\n'
+         << "Number of vowel i: \t" << iCnt << '\n'
+         << "Number of vowel o: \t" << oCnt << '\n'
+         << "Number of vowel u: \t" << uCnt << endl;
+
+    return 0;
+}
diff --git a/6/LocalMath.cc b/6/LocalMath.cc
new file mode 100644 (file)
index 0000000..6eeca29
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "LocalMath.h"
+
+// return the greatest common divisor
+int gcd(int v1, int v2)
+{
+    while (v2) {
+        int temp = v2;
+        v2 = v1 % v2;
+        v1 = temp;
+    }
+    return v1;
+}
+
+
+// factorial of val is val * (val - 1) *  (val - 2) . . . * ((val -  (val - 1)) * 1)
+int fact(int val)
+{
+       int ret = 1; // local variable to hold the result as we calculate it
+       while (val > 1) 
+               ret *= val--;  // assign ret * val to ret and decrement val
+       return ret;        // return the result
+}
+
+// recursive version of factorial:
+// calculate val!, which is 1 * 2 * 3 . . . * val
+int factorial(int val)
+{
+    if (val > 1)
+        return factorial(val-1) * val;
+    return 1;
+}
+
diff --git a/6/LocalMath.h b/6/LocalMath.h
new file mode 100644 (file)
index 0000000..cb678a4
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+// not included in book text, but used by programs in this directory
+#ifndef LOCALMATH_H
+#define LOCALMATH_H
+
+//definition in LocalMath.cc
+int fact(int);        // iterative definition of factorial
+int factorial(int);   // recrusive version of factorial
+int gcd(int, int);    // find greatest common divisor
+#endif
diff --git a/6/README b/6/README
new file mode 100644 (file)
index 0000000..cf5db64
--- /dev/null
+++ b/6/README
@@ -0,0 +1,11 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   mainmath         mainmath
+   refparms         refparms
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/6/arrRet.cc b/6/arrRet.cc
new file mode 100644 (file)
index 0000000..3a1ff6a
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+// code to illustrate declarations of array-related types
+int arr[10];          // arr is an array of ten ints
+int *p1[10];          // p1 is an array of ten pointers
+int (*p2)[10] = &arr; // p2 points to an array of ten ints
+
+using arrT = int[10]; // arrT is a synonym for the type array of ten ints
+
+// three ways to declare function returning pointer to array of ten ints
+arrT* func(int i);               // use a type alias
+auto func(int i) -> int(*)[10];  // use a trailing return type
+int (*func(int i))[10];          // direct declaration
+
+auto func2(int i) -> int(&)[10]; // func2 returns a refernce to an array
+
+// two arrays
+int odd[] = {1,3,5,7,9};
+int even[] = {0,2,4,6,8};
+
+// function that returns a pointer to an int in one of these arrays
+int *elemPtr(int i)
+{
+       // returns a pointer to the first element in one of these arrays
+       return (i % 2) ? odd : even;  
+}
+       
+// returns a pointer to an array of five int elements
+decltype(odd) *arrPtr(int i)
+{
+       return (i % 2) ? &odd : &even; // returns a pointer to the array 
+}
+
+// returns a reference to an array of five int elements
+int (&arrRef(int i))[5]
+{
+       return (i % 2) ? odd : even;
+}
+
+int main()
+{
+       int *p = elemPtr(6);         // p points to an int
+       int (*arrP)[5] = arrPtr(5);  // arrP points to an array of five ints
+       int (&arrR)[5] = arrRef(4);  // arrR refers to an array of five ints
+
+       for (size_t i = 0; i < 5; ++i)
+               // p points to an element in an array, which we subscript
+               cout << p[i] << endl;  
+
+       for (size_t i = 0; i < 5; ++i)
+               // arrP points to an array, 
+               // we must dereference the pointer to get the array itself
+               cout << (*arrP)[i] << endl;
+
+       for (size_t i = 0; i < 5; ++i)
+               // arrR refers to an array, which we can subscript
+               cout << arrR[i] << endl;
+
+       return 0;
+}
diff --git a/6/count-calls.cc b/6/count-calls.cc
new file mode 100644 (file)
index 0000000..3612938
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+size_t count_calls()
+{
+       static size_t ctr = 0;  // value will persist across calls
+       return ++ctr;
+}
+
+int main() 
+{
+       for (size_t i = 0; i != 10; ++i)
+               cout << count_calls() << endl;
+       return 0;
+}
diff --git a/6/data/mainmath b/6/data/mainmath
new file mode 100644 (file)
index 0000000..5281552
--- /dev/null
@@ -0,0 +1 @@
+5 
diff --git a/6/data/refparms b/6/data/refparms
new file mode 100644 (file)
index 0000000..cff9da2
--- /dev/null
@@ -0,0 +1,15 @@
+Hello World!
+42
+65
+95
+100
+39
+67
+95
+76
+88
+76
+83
+92
+76
+93
diff --git a/6/errMsg_initList.cc b/6/errMsg_initList.cc
new file mode 100644 (file)
index 0000000..9bff2e8
--- /dev/null
@@ -0,0 +1,110 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string; 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <initializer_list>
+using std::initializer_list;
+
+#include <sstream>
+using std::ostringstream;
+
+// chapter 7 will explain the code used in defining ErrCode
+struct ErrCode { 
+       ErrCode(int i) : num(i) { }  // initializes objects of type ErrCode
+       string msg()                 // member function of ErrCode
+       { ostringstream s; s << "ErrCode " << num; return s.str(); }
+       int num;  // data member, note uninitialized
+};
+
+// version that takes an ErrCode and a list of strings
+void error_msg(ErrCode e, initializer_list<string> il)
+{
+       cout << e.msg() << ": ";
+       for (const auto &elem : il)
+               cout << elem << " " ;
+       cout << endl;
+}
+
+// overloaded version takes only a list of strings
+void error_msg(initializer_list<string> il)
+{
+       for (auto beg = il.begin(); beg != il.end(); ++beg)
+               cout << *beg << " " ;
+       cout << endl;
+}
+
+// function to illustrate list initializing return value
+vector<string> functionX()
+{
+       string expected = "description", actual = "some other case";
+       // . . .
+       if (expected.empty())
+               return {};  // return an empty vector
+       else if (expected == actual)
+               return {"functionX", "okay"}; // return list-initialized vector
+       else 
+               return {"functionX", expected, actual}; 
+}
+       
+int main()
+{
+       string expected = "description", actual = "some other case";
+       initializer_list<int> li = {0,1,2,3};
+
+       // expected, actual are strings 
+       if (expected != actual)
+               error_msg({"functionX", expected, actual});
+       else
+               error_msg({"functionX", "okay"});
+
+       // expected, actual are strings 
+       if (expected != actual)
+               error_msg(ErrCode(42), {"functionX", expected, actual});
+       else
+               error_msg(ErrCode(0), {"functionX", "okay"});
+       
+       // can pass an empty list, calls second version of error_msg
+       error_msg({}); // prints blank line
+
+       // call function that list initializes its return value
+       // results is a vector<string>
+       auto results = functionX();
+       for (auto i : results) 
+               cout << i << " ";
+       cout << endl;
+}
+
diff --git a/6/fact.cc b/6/fact.cc
new file mode 100644 (file)
index 0000000..43c49d5
--- /dev/null
+++ b/6/fact.cc
@@ -0,0 +1,46 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+// declarations of our factorial functions
+// definitions are in LocalMath.cc
+#include "LocalMath.h"
+
+int main()
+{
+       cout << factorial(5) << endl;
+       cout << fact(5) << endl;
+       cout << factorial(0) << endl;
+       cout << fact(0) << endl;
+
+       return 0;
+}
+
diff --git a/6/fcnptrRet.cc b/6/fcnptrRet.cc
new file mode 100644 (file)
index 0000000..0f19409
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+// declarations (not strictly speaking necessary in this file)
+string::size_type sumLength(const string&, const string&);
+string::size_type largerLength(const string&, const string&);
+
+// definition of these functions
+string::size_type sumLength(const string &s1, const string &s2)
+{
+       return s1.size() + s2.size();
+}
+
+string::size_type largerLength(const string &s1, const string &s2)
+{
+       return (s1.size() > s2.size()) ? s1.size() : s2.size();
+}
+
+// depending on the value of its string parameter,
+// getFcn returns a pointer to sumLength or to largerLength
+
+// three ways to declare getFcn 
+// 1. use decltype for the return type, 
+//    remembering to add a * to indicate that getFcn returns a pointer
+decltype(sumLength) *getFcn(const string &);
+
+// use trailing return type
+auto getFcn(const string&) -> string::size_type(*)(const string&, const string&);
+
+// direct definition
+string::size_type (*getFcn(const string&))(const string&, const string&);
+
+// define getFcn
+decltype(sumLength)* 
+getFcn(const string &fetch)
+{
+       if (fetch == "sum")
+               return sumLength;
+       return largerLength;
+}
+
+int main()
+{
+       // "sum" is the argument to getFcn
+       // ("hello", "world!") are arguments to the function getFcn returns
+       cout << getFcn("sum")("hello", "world!") << endl;    // prints 11
+       cout << getFcn("larger")("hello", "world!") << endl; // prints 6
+
+       return 0;
+}
diff --git a/6/good_printmain.cc b/6/good_printmain.cc
new file mode 100644 (file)
index 0000000..883625d
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iterator>
+using std::begin; using std::end;
+
+#include <cstddef>
+using std::size_t;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+// const int ia[] is equivalent to const int* ia
+// size is passed explicitly and used to control access to elements of ia
+void print(const int ia[], size_t size) 
+{
+    for (size_t i = 0; i != size; ++i) {
+        cout << ia[i] << endl;
+    }
+}
+
+int main() 
+{
+    int j[] = { 0, 1 };  // int array of size 2
+
+    print(j, end(j) - begin(j));  
+
+    return 0;
+}
diff --git a/6/inline_shorter.cc b/6/inline_shorter.cc
new file mode 100644 (file)
index 0000000..0126676
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+//inline version: find the shorter of two strings
+inline const string &
+shorterString(const string &s1, const string &s2)
+{
+        return s1.size() <= s2.size() ? s1 : s2;
+}
+
+int main()
+{
+       string s1("successes"), s2("failure");
+       cout << shorterString(s1, s2) << endl;
+
+       // call the size member of the string returned by shorterString
+       cout << shorterString(s1, s2).size() << endl;
+
+       // equivalent code as generated by the call to inline version
+       // of shorterString
+       cout << (s1.size() < s2.size() ? s1 : s2) << endl;
+       return 0;
+}
diff --git a/6/mainmath.cc b/6/mainmath.cc
new file mode 100644 (file)
index 0000000..b9645e7
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "LocalMath.h"
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       // pass a literal to fact
+    int f = fact(5);  // f equals 120, i.e., the result of fact(5)
+       cout << "5! is " << f << endl;
+
+    // call fact on i and print the result
+       int i = 5;
+    int j = fact(i);
+       cout << i << "! is " << j << endl;
+
+       // call fact on a const int
+    const int ci = 3;
+    int k = fact(ci);   
+       cout << ci << "! is " << k << endl;
+
+    return 0;
+}
+
diff --git a/6/mainret.cc b/6/mainret.cc
new file mode 100644 (file)
index 0000000..da55420
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstdlib>
+/* EXIT_FAILURE and EXIT_SUCCESS are preprocessor variables
+ *       such variables are not in the std namespace, 
+ *       hence, no using declaration and no std:: when we use these names
+*/
+int main()
+{
+    bool some_failure = false;
+    if (some_failure)
+        return EXIT_FAILURE;  // defined in cstdlib
+    else
+        return EXIT_SUCCESS;  // defined in cstdlib
+}
diff --git a/6/make_plural.h b/6/make_plural.h
new file mode 100644 (file)
index 0000000..1dda54f
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#ifndef MAKE_PLURAL_H
+#define MAKE_PLURAL_H
+
+// return the plural version of word if ctr is greater than 1
+inline
+string make_plural(size_t ctr, const string &word, 
+                               const string &ending)
+{
+       return (ctr > 1) ? word + ending : word;
+}
+
+#endif
diff --git a/6/makefile b/6/makefile
new file mode 100644 (file)
index 0000000..632a65b
--- /dev/null
@@ -0,0 +1,21 @@
+# executable files for this directory
+OBJECTS = arrRet.exe count-calls.exe errMsg_initList.exe\
+                 fact.exe fcnptrRet.exe good_printmain.exe \
+                 inline_shorter.exe mainmath.exe mainret.exe \
+                 mk_plural.exe ref-fcn.exe refparms.exe refret-ex.exe  \
+                 reset.exe stl-arr-fcns.exe usefcnptr.exe wdebug.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = 
+
+fact.o mainmath.o LocalMath.o: LocalMath.h
+
+fact.exe: fact.o LocalMath.o
+       $(CC) $(CCFLAGS) fact.o LocalMath.o -o fact.exe
+
+mainmath.exe: mainmath.o LocalMath.o 
+       $(CC) $(CCFLAGS) mainmath.o LocalMath.o -o mainmath.exe
+
diff --git a/6/mk_plural.cc b/6/mk_plural.cc
new file mode 100644 (file)
index 0000000..a990a45
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include "make_plural.h"
+
+int main()
+{
+       size_t cnt = 1;
+       cout << make_plural(cnt, "success", "es") << endl;
+
+       cnt = 2;
+       cout << make_plural(cnt, "failure", "s") << endl;
+
+       return 0;
+}
diff --git a/6/printFcns.h b/6/printFcns.h
new file mode 100644 (file)
index 0000000..f8b09fe
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef PRINTFCNS_H
+#define PRINTFCNS_H
+#include <vector>
+void print(const char *cp);
+void print(const int *beg, const int *end); 
+void print(std::vector<int>::const_iterator beg,
+           std::vector<int>::const_iterator end);
+void print(const int ia[], size_t size);
+void print(const std::vector<int>&);
+
+inline foo() {
+int j[2] = {0,1};
+print("Hello World");        // calls print(const char*)
+print(j, end(j) - begin(j)); // calls print(const int*, size_t)
+print(begin(j), end(j));     // calls print(const int*, const int*)
+#endif
diff --git a/6/ref-fcn.cc b/6/ref-fcn.cc
new file mode 100644 (file)
index 0000000..74afd64
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+char &get_val(string &str, string::size_type ix) 
+{
+    return str[ix]; // get_val assumes the given index is valid
+}
+
+int main() 
+{
+    string s("a value");
+    cout << s << endl;   // prints a value
+
+    get_val(s, 0) = 'A'; // changes s[0] to A
+    cout << s << endl;   // prints A value
+
+    return 0;
+}
+
diff --git a/6/refparms.cc b/6/refparms.cc
new file mode 100644 (file)
index 0000000..855ddf0
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include <vector>
+using std::vector;
+
+#include <cstddef>
+using std::size_t;
+
+// returns the index of the first occurrence of c in s
+// the reference parameter occurs counts how often c occurs
+string::size_type find_char(const string &s, char c, 
+                           string::size_type &occurs)
+{
+    auto ret = s.size();   // position of the first occurrence, if any
+    occurs = 0;            // set the occurrence count parameter 
+
+    for (decltype(ret) i = 0; i != s.size(); ++i) {
+        if (s[i] == c) {
+            if (ret == s.size()) 
+                ret = i;   // remember the first occurrence of c
+            ++occurs;      // increment the occurrence count
+         }
+       }
+    return ret;            // count is returned implicitly in occurs
+}
+
+// returns an iterator that refers to the first occurrence of value
+// the reference parameter occurs contains a second return value
+vector<int>::const_iterator find_val(
+    vector<int>::const_iterator beg,  // first element
+    vector<int>::const_iterator end,  // one past last element
+    int value,                        // the value we want
+    vector<int>::size_type &occurs)   // number of times it occurs
+{
+    auto res_iter = end; // res_iter will hold first occurrence, if any
+    occurs = 0;          // set occurrence count parameter 
+
+    for ( ; beg != end; ++beg)
+        if (*beg == value) {
+            // remember first occurrence of value
+            if (res_iter == end) 
+                res_iter = beg;
+            ++occurs;    // increment occurrence count
+         }
+
+    return res_iter;     // count returned implicitly in occurs
+}
+
+int main() 
+{
+       
+       string s;
+       getline(cin, s); 
+       size_t ctr = 0;
+       auto index = find_char(s, 'o', ctr);
+       cout << index << " " << ctr << endl;
+       
+       vector<int> ivec;
+       int i;
+       // read values into ivec
+       while (cin >> i)
+               ivec.push_back(i);
+
+       // for each value in the list of ints
+       for (auto i : {42, 33, 92}) {
+               auto it = find_val(ivec.begin(), ivec.end(), i, ctr);
+               if (it == ivec.end())
+                       cout << i << " is not in the input data" << endl;
+               else
+                       cout << i << " was at position " 
+                            << it - ivec.begin() << endl;
+       }
+
+       return 0;
+}
diff --git a/6/refret-ex.cc b/6/refret-ex.cc
new file mode 100644 (file)
index 0000000..8081b53
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+// get returns a reference to an element in the given array
+int &get(int *arry, int index) { return arry[index]; }
+
+int main() {
+    int ia[10];  // array of ten uninitialized ints
+
+    for (int i = 0; i != 10; ++i)
+        get(ia, i) = i;  // call get to assign values to the elements
+
+       for (auto i : ia)    // print the elements
+               cout << i << " ";
+       cout << endl;
+
+       return 0;
+}
diff --git a/6/reset.cc b/6/reset.cc
new file mode 100644 (file)
index 0000000..9e3614b
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <vector>
+using std::vector;
+
+// function that takes a reference to an int 
+// and sets the given object to zero
+// i is just another name for the object passed to reset
+void reset(int &i)  
+{
+    i = 0;  // changes the value of the object to which i refers
+}
+
+// function that takes a pointer 
+// and sets the pointed-to value to zero
+void reset(int *ip)
+{
+    *ip = 0;  // changes the value of the object to which ip points
+    ip = 0;   // changes the local copy of ip; the argument is unchanged
+}
+
+int main() 
+{
+       int j = 42;
+       reset(j);  // j is passed by reference; the value in j is changed
+       cout << "j = " << j  << endl;  // prints j = 0
+
+       j = 42;    // restore the original value of j
+       reset(&j);                     // changes j but not the address of j
+       cout << "j = "  << j << endl;  // prints j = 0
+
+       j = 42;    // restore the original value of j
+       int *p = &j;
+       reset(p); // changes object to which p points not the address in p
+       cout << "j = "  << *p << endl;  // prints j = 0
+
+    return 0;
+}
diff --git a/6/runpgms b/6/runpgms
new file mode 100644 (file)
index 0000000..fe04ea7
--- /dev/null
+++ b/6/runpgms
@@ -0,0 +1,9 @@
+echo "mainmath:" && ./mainmath < data/mainmath
+echo "refparms:" && ./refparms < data/refparms
+
+../noinput arrRet.exe count-calls.exe \
+                  errMsg_initList.exe fact.exe fcnptrRet.exe \
+                  good_printmain.exe inline_shorter.exe \
+                  mainret.exe mk_plural.exe \
+                  ref-fcn.exe refret-ex.exe  reset.exe \
+                  stl-arr-fcns.exe usefcnptr.exe wdebug.exe
diff --git a/6/stl-arr-fcns.cc b/6/stl-arr-fcns.cc
new file mode 100644 (file)
index 0000000..e0761c3
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::endl; using std::cout;
+
+#include <iterator>
+using std::begin; using std::end;
+
+// prints a null-terminated array of characters
+void print(const char *cp)
+{
+       if (cp)          // if cp is not a null pointer
+               while (*cp)  // so long as the character it points to is not a null character
+                       cout << *cp++;  // print the character and advance the pointer
+}
+
+// print ints in the given range
+void print(const int *beg, const int *end) 
+{
+       // print every element starting at beg up to but not including end
+    while (beg != end) 
+        cout << *beg++ << " "; // print the current element 
+                                      // and advance the pointer
+}
+
+int main() 
+{
+       print("hi world!"); // calls first version of print
+       cout << endl;
+
+    // j is converted to a pointer to the first element in j
+    // the second argument is a pointer to one past the end of j
+    int j[2] = {0, 1};  
+    print(begin(j), end(j));  // library begin and end functions
+       cout << endl;
+
+       // equivalent call, directly calculate the begin and end pointers
+       print(j, j + 2); 
+       cout << endl;
+
+    return 0;
+}
diff --git a/6/use_mk_plural.cc b/6/use_mk_plural.cc
new file mode 100644 (file)
index 0000000..6f5ff2b
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+// return the plural version of word if ctr is greater than 1
+string make_plural(size_t ctr, const string &word, 
+                               const string &ending)
+{
+       return (ctr > 1) ? word + ending : word;
+}
+
+int main()
+{
+       size_t cnt = 1;
+       cout << make_plural(cnt, "success", "es") << endl;
+       cnt = 2;
+       cout << make_plural(cnt, "failure", "s") << endl;
+
+       return 0;
+}
diff --git a/6/usefcnptr.cc b/6/usefcnptr.cc
new file mode 100644 (file)
index 0000000..d23daa9
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <vector>
+using std::vector;
+
+// function to return minimum element in an vector of ints
+int min_element(vector<int>::iterator, 
+                vector<int>::iterator);
+
+// pointer to function, initialized to point to min_element
+int (*pf)(vector<int>::iterator, vector<int>::iterator) 
+        = min_element;
+
+int main() 
+{
+    vector<int> ivec;
+    // give ivec some values
+    cout << "Direct call: "   
+         << min_element(ivec.begin(), ivec.end()) << endl;
+
+    cout << "Indirect call: " 
+         << pf(ivec.begin(), ivec.end()) << endl;
+
+       cout << "equivalent indirect call: "
+            << (*pf)(ivec.begin(), ivec.end()) << endl;
+
+    return 0;
+}
+
+// returns minimum element in an vector of ints
+int min_element(vector<int>::iterator beg, 
+                vector<int>::iterator end) {
+    int minVal = 0;
+    while (beg != end) {
+        if (minVal > *beg)
+            minVal = *beg;
+        ++beg;
+    }
+    return minVal;
+}
+
diff --git a/6/wdebug.cc b/6/wdebug.cc
new file mode 100644 (file)
index 0000000..912f237
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <cassert>
+// assert is a preprocessor macro and therefore not in std
+// hence we need to include cassert header, 
+// but no using declaration for assert 
+
+#include <string> 
+using std::string;
+
+#include <iostream> 
+using std::endl; using std::cerr; using std::cin;
+
+#include <cstddef>
+using std::size_t;
+
+void print(const int ia[], size_t size)
+{
+#ifndef NDEBUG
+// __func__ is a local static defined by the compiler that holds the name of this function
+cerr << __func__ << ": array size is " << size << endl;
+#endif
+// . . .
+}
+
+int main()
+{
+    string word = "foo";
+    const string::size_type threshold = 5;
+    if (word.size() < threshold) 
+        cerr << "Error: " << __FILE__
+             << " : in function " << __func__ 
+             << " at line " << __LINE__ << endl
+             << "       Compiled on " << __DATE__ 
+             << " at " << __TIME__ << endl
+             << "       Word read was \"" << word 
+             << "\":  Length too short" << endl;
+    word = "something longer than five chars";
+    assert(word.size() > threshold);
+
+    return 0;
+}
diff --git a/7/Account.cc b/7/Account.cc
new file mode 100644 (file)
index 0000000..c6ceb34
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include "Account.h"
+
+// define static data and function members
+const string Account::accountType("Savings Account");
+double Account::interestRate = initRate();
+
+void Account::rate(double newRate) 
+{
+    interestRate = newRate; 
+}
diff --git a/7/Account.h b/7/Account.h
new file mode 100644 (file)
index 0000000..bff1822
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+#ifndef ACCOUNT_H
+#define ACCOUNT_H
+
+#include <string>
+
+class Account {
+public:
+       Account() = default;
+       Account(const std::string &s, double amt):
+               owner(s), amount(amt) { }
+
+    void calculate() { amount += amount * interestRate; }
+    double balance() { return amount; }
+public:
+    static double rate() { return interestRate; }
+    static void rate(double);   
+private:
+    std::string owner; 
+    double amount = 0.0;
+    static double interestRate; 
+    static double initRate() { return .0225; }
+    static const std::string accountType;
+    static constexpr int period = 30;// period is a constant expression
+    double daily_tbl[period];
+};
+#endif
diff --git a/7/Debug.cc b/7/Debug.cc
new file mode 100644 (file)
index 0000000..36cc1ba
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Debug.h"
+// only implementation for the Debug classes are definitions
+// for static members named enable 
+constexpr Debug HW_Subsystem::enable;
+constexpr Debug IO_Subsystem::enable;
diff --git a/7/Debug.h b/7/Debug.h
new file mode 100644 (file)
index 0000000..8ee1195
--- /dev/null
+++ b/7/Debug.h
@@ -0,0 +1,71 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef DEBUG_H
+#define DEBUG_H
+class Debug {
+public:
+       constexpr Debug(bool b = true): hw(b), io(b), other(b) { }
+       constexpr Debug(bool h, bool i, bool o): 
+                                       hw(h), io(i), other(o) { }
+       constexpr bool any() { return hw || io || other; }
+       constexpr bool hardware() { return hw || io; }
+       constexpr bool app() { return other; }
+
+       void set_io(bool b) { io = b; }
+       void set_hw(bool b) { hw = b; }
+       void set_other(bool b) { hw = b; }
+private:
+       bool hw;    // hardware errors other than IO errors
+       bool io;    // IO errors
+       bool other; // other errors
+};
+
+class HW_Subsystem {
+public:
+       HW_Subsystem(): debug(false) { }          // by default no debugging
+       bool field_debug()   { return debug.any(); }
+       bool default_debug() { return enable.any() && debug.any(); }
+       void set_debug(bool b) { debug.set_hw(b); }  // turn on hardware debugging
+private:
+       Debug debug;
+       constexpr static Debug enable{true, false, false};
+};
+
+class IO_Subsystem {
+public:
+       IO_Subsystem(): debug(false) { }          // by default no debugging
+       bool field_debug()     { return debug.any(); }
+       bool default_debug()   { return enable.any() && debug.any(); }
+       void set_debug(bool b) { debug.set_io(b); }  // turn on IO debugging
+private:
+       Debug debug;
+       constexpr static Debug enable{true, false, true};
+};
+#endif
diff --git a/7/README b/7/README
new file mode 100644 (file)
index 0000000..c19cdd4
--- /dev/null
+++ b/7/README
@@ -0,0 +1,11 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   book_sales       avg_price     
+   add_item         add_item
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/7/Sales_data.cc b/7/Sales_data.cc
new file mode 100644 (file)
index 0000000..2ef3509
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+
+#include <iostream>
+using std::istream; using std::ostream;
+
+#include "Sales_data.h"
+Sales_data::Sales_data(std::istream &is) 
+{
+       // read will read a transaction from is into this object
+       read(is, *this);
+}
+
+double 
+Sales_data::avg_price() const {
+       if (units_sold)
+               return revenue/units_sold;
+       else
+               return 0;
+}
+
+// add the value of the given Sales_data into this object
+Sales_data& 
+Sales_data::combine(const Sales_data &rhs)
+{
+       units_sold += rhs.units_sold; // add the members of rhs into 
+       revenue += rhs.revenue;       // the members of ``this'' object
+       return *this; // return the object on which the function was called
+}
+
+Sales_data 
+add(const Sales_data &lhs, const Sales_data &rhs)
+{
+       Sales_data sum = lhs;  // copy data members from lhs into sum
+       sum.combine(rhs);      // add data members from rhs into sum
+       return sum;
+}
+
+// transactions contain ISBN, number of copies sold, and sales price
+istream&
+read(istream &is, Sales_data &item)
+{
+       double price = 0;
+       is >> item.bookNo >> item.units_sold >> price;
+       item.revenue = price * item.units_sold;
+       return is;
+}
+
+ostream&
+print(ostream &os, const Sales_data &item)
+{
+       os << item.isbn() << " " << item.units_sold << " " 
+          << item.revenue << " " << item.avg_price();
+       return os;
+}
diff --git a/7/Sales_data.h b/7/Sales_data.h
new file mode 100644 (file)
index 0000000..d710edb
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef SALES_DATA_H
+#define SALES_DATA_H
+
+#include <string>
+#include <iostream>
+
+class Sales_data {
+friend Sales_data add(const Sales_data&, const Sales_data&);
+friend std::ostream &print(std::ostream&, const Sales_data&);
+friend std::istream &read(std::istream&, Sales_data&);
+public:
+       // constructors
+       Sales_data() = default;
+       Sales_data(const std::string &s): bookNo(s) { }
+       Sales_data(const std::string &s, unsigned n, double p):
+                  bookNo(s), units_sold(n), revenue(p*n) { }
+       Sales_data(std::istream &);
+
+       // operations on Sales_data objects
+       std::string isbn() const { return bookNo; }
+       Sales_data& combine(const Sales_data&);
+       double avg_price() const;
+private:
+       std::string bookNo;
+       unsigned units_sold = 0;
+       double revenue = 0.0;
+};
+
+
+// nonmember Sales_data interface functions
+Sales_data add(const Sales_data&, const Sales_data&);
+std::ostream &print(std::ostream&, const Sales_data&);
+std::istream &read(std::istream&, Sales_data&);
+
+// used in future chapters
+inline 
+bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs)
+{
+       return lhs.isbn() < rhs.isbn();
+}
+#endif
diff --git a/7/Screen.h b/7/Screen.h
new file mode 100644 (file)
index 0000000..92c8bc1
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+#include <iostream>
+
+class Screen {
+public:
+    typedef std::string::size_type pos;
+       Screen() = default;  // needed because Screen has another constructor
+       // cursor initialized to 0 by its in-class initializer
+    Screen(pos ht, pos wd, char c): height(ht), width(wd), 
+                                       contents(ht * wd, c) { }
+       friend class Window_mgr;
+    Screen(pos ht = 0, pos wd = 0): 
+       cursor(0), height(ht), width(wd), contents(ht * wd, ' ') { }
+    char get() const              // get the character at the cursor
+           { return contents[cursor]; }       // implicitly inline
+    inline char get(pos ht, pos wd) const; // explicitly inline
+       Screen &clear(char = bkground);
+private:
+       static const char bkground = ' ';
+public:
+    Screen &move(pos r, pos c);      // can be made inline later
+    Screen &set(char);
+    Screen &set(pos, pos, char);
+    // display overloaded on whether the object is const or not
+    Screen &display(std::ostream &os) 
+                  { do_display(os); return *this; }
+    const Screen &display(std::ostream &os) const
+                  { do_display(os); return *this; }
+private:
+     // function to do the work of displaying a Screen
+     void do_display(std::ostream &os) const {os << contents;}
+    pos cursor = 0;
+    pos height = 0, width = 0;
+    std::string contents;
+};
+
+Screen &Screen::clear(char c) 
+{
+       contents = std::string(height*width, c);
+       return *this;
+}
+
+inline                   // we can specify inline on the definition
+Screen &Screen::move(pos r, pos c)
+{
+    pos row = r * width; // compute the row location
+    cursor = row + c;    // move cursor to the column within that row
+    return *this;        // return this object as an lvalue
+}
+
+char Screen::get(pos r, pos c) const // declared as inline in the class
+{
+    pos row = r * width;      // compute row location
+    return contents[row + c]; // return character at the given column
+}
+
+inline Screen &Screen::set(char c) 
+{ 
+    contents[cursor] = c; // set the new value at the current cursor location
+    return *this;         // return this object as an lvalue
+}
+inline Screen &Screen::set(pos r, pos col, char ch)
+{
+       contents[r*width + col] = ch;  // set specified location to given value
+       return *this;                  // return this object as an lvalue
+}
+
diff --git a/7/WindowMgr.h b/7/WindowMgr.h
new file mode 100644 (file)
index 0000000..c4060ba
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#ifndef WINDOW_MGR
+#define WINDOW_MGR
+
+#include <vector>
+#include <string>
+#include <iostream>
+#include "newscreen.h"
+
+class BitMap;
+// overloaded storeOn functions
+extern std::ostream& storeOn(std::ostream &, Screen &);
+extern BitMap& storeOn(BitMap &, Screen &);
+
+class Window_mgr {
+public:
+       // location ID for each screen on the window
+       using ScreenIndex = std::vector<Screen>::size_type;
+
+       // add a Screen to the window and returns its index
+       ScreenIndex addScreen(const Screen&);
+       
+       // reset the Screen at the given position to all blanks
+       void clear(ScreenIndex);
+
+       // change dimensions of a given Screen
+       void resize(Screen::pos r, Screen::pos c, ScreenIndex i);
+private:
+       // Screens this Window_mgr is tracking
+       // by default, a Window_mgr has one standard sized blank Screen 
+       std::vector<Screen> screens{Screen(24, 80, ' ')};
+};
+
+// return type is seen before we're in the scope of Window_mgr
+inline
+Window_mgr::ScreenIndex
+Window_mgr::addScreen(const Screen &s)
+{
+       screens.push_back(s);
+       return screens.size() - 1;
+}
+
+inline
+void Window_mgr::clear(ScreenIndex i)
+{
+       // s is a reference to the Screen we want to clear
+       Screen &s = screens[i];
+       // reset the contents of that Screen to all blanks
+       s.contents = std::string(s.height * s.width, ' ');
+}
+
+inline
+void
+Window_mgr::resize(Screen::pos r, Screen::pos c, ScreenIndex i)
+{
+    screens[i].height = r;  // Window_mgr is a friend of Screen
+    screens[i].width = c;   // so it is ok to use Screen's private members
+       // resize and clear the contents member
+       screens[i].contents = std::string(r * c, ' '); 
+}
+
+#endif
diff --git a/7/add_item.cc b/7/add_item.cc
new file mode 100644 (file)
index 0000000..0b85f75
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cerr; using std::cin; using std::cout; using std::endl;
+
+#include "Sales_data.h"
+
+int main()
+{
+       Sales_data data1, data2;
+       if (read(cin, data1) && read(cin, data2)) {  // read the transactions
+               if (data1.isbn() == data2.isbn()) {      // check isbns
+                       data1.combine(data2);                // add the transactions
+                       print(cout, data1);                  // print the results
+                       cout << endl;                        // followed by a newline 
+               }
+       } else
+               cerr << "Input failed!" << endl;        
+
+       return 0;
+}
diff --git a/7/avg_price.cc b/7/avg_price.cc
new file mode 100644 (file)
index 0000000..bc8420d
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cerr; using std::cin; using std::cout; using std::endl;
+
+#include "Sales_data.h"
+
+int main()
+{
+       Sales_data total;         // variable to hold the running sum
+       if (read(cin, total))  {  // read the first transaction
+               Sales_data trans;     // variable to hold data for the next transaction
+               while(read(cin, trans)) {      // read the remaining transactions
+                       if (total.isbn() == trans.isbn())   // check the isbns
+                               total.combine(trans);  // update the running total
+                       else {
+                               print(cout, total) << endl;  // print the results
+                               total = trans;               // process the next book
+                       }
+               }
+               print(cout, total) << endl;          // print the last transaction
+       } else {                                 // there was no input
+               cerr << "No data?!" << endl;         // notify the user
+       }
+
+       return 0;
+}
diff --git a/7/data/add_item b/7/data/add_item
new file mode 100644 (file)
index 0000000..92fd61c
--- /dev/null
@@ -0,0 +1,2 @@
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/7/data/book_sales b/7/data/book_sales
new file mode 100644 (file)
index 0000000..0d14afe
--- /dev/null
@@ -0,0 +1,10 @@
+0-201-70353-X 4 24.99
+0-201-82470-1 4 45.39
+0-201-88954-4 2 15.00 
+0-201-88954-4 5 12.00 
+0-201-88954-4 7 12.00 
+0-201-88954-4 2 12.00 
+0-399-82477-1 2 45.39
+0-399-82477-1 3 45.39
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/7/makefile b/7/makefile
new file mode 100644 (file)
index 0000000..fb6ad3b
--- /dev/null
@@ -0,0 +1,30 @@
+# executable files for this directory
+OBJECTS = add_item.exe avg_price.exe \
+                 useAccount.exe useDebug.exe useScreen.exe 
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../1
+
+useScreen.exe: Screen.h
+
+Account.o useAccount.o: Account.h
+Debug.o useDebug.o: Debug.h
+
+useAccount.exe: Account.o useAccount.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) Account.o useAccount.o \
+               -o useAccount.exe
+
+useDebug.exe: Debug.o useDebug.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) Debug.o useDebug.o -o useDebug.exe
+
+add_item.exe: add_item.o Sales_data.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) add_item.o Sales_data.o \
+               -o add_item.exe
+
+avg_price.exe: avg_price.o Sales_data.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) avg_price.o Sales_data.o \
+               -o avg_price.exe
diff --git a/7/runpgms b/7/runpgms
new file mode 100644 (file)
index 0000000..3862f3d
--- /dev/null
+++ b/7/runpgms
@@ -0,0 +1,4 @@
+echo "avg_price:" && ./avg_price < data/book_sales
+echo "add_item:" && ./add_item < data/add_item
+
+../noinput useAccount.exe useDebug.exe useScreen.exe 
diff --git a/7/useAccount.cc b/7/useAccount.cc
new file mode 100644 (file)
index 0000000..acea15d
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include "Account.h"
+
+int main()
+{
+       Account a1("bem", 42);
+       cout << a1.balance() << endl;
+       a1.calculate();
+       cout << a1.balance() << endl;
+
+       return 0;
+}
diff --git a/7/useDebug.cc b/7/useDebug.cc
new file mode 100644 (file)
index 0000000..980ef6f
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cerr; using std::endl;
+
+#include "Debug.h"
+
+int main()
+{
+       constexpr Debug io_sub(false, true, false);  // debugging IO    
+       if (io_sub.any())  // equivalent to if(true)
+               cerr << "print appropriate error messages" << endl;
+
+       constexpr Debug prod(false); // no debugging during production
+       if (prod.any())    // equivalent to if(false)
+               cerr << "print an error message" << endl;
+
+       IO_Subsystem ioErrs;        // by default, don't print any debugging
+       // no debugging here
+       if (ioErrs.default_debug()) // if (false || debug.any())
+               cerr << "print message 3" << endl;
+       ioErrs.set_debug(true);     // turn on debugging
+       if (ioErrs.default_debug()) // if (false || debug.any())
+               cerr << "print message 4" << endl;
+       ioErrs.set_debug(false);    // okay, debugging section complete
+
+       HW_Subsystem hw;
+       hw.set_debug(true);
+       if (ioErrs.default_debug() || hw.default_debug()) // if (false || debug.any())
+               cerr << "print message 5" << endl;
+}
diff --git a/7/useScreen.cc b/7/useScreen.cc
new file mode 100644 (file)
index 0000000..15785ff
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <string>
+using std::string;
+
+#include "Screen.h"
+
+int main()
+{
+       Screen myScreen(5,3);
+       // move the cursor to a given position, and set that character 
+       myScreen.move(4,0).set('#');
+
+       Screen nextScreen(5, 5, 'X');
+       nextScreen.move(4,0).set('#').display(cout);
+       cout << "\n";
+       nextScreen.display(cout);
+       cout << endl;
+
+       const Screen blank(5, 3);
+       myScreen.set('#').display(cout);  // calls nonconst version
+       cout << endl;
+       blank.display(cout);              // calls const version
+       cout << endl;
+
+       myScreen.clear('Z').display(cout); cout << endl;
+       myScreen.move(4,0);
+       myScreen.set('#');
+       myScreen.display(cout); cout << endl;
+       myScreen.clear('Z').display(cout); cout << endl;
+
+       // if move returns Screen not Screen&
+       Screen temp = myScreen.move(4,0); // the return value would be copied
+       temp.set('#'); // the contents inside myScreen would be unchanged
+       myScreen.display(cout);
+       cout << endl;
+}
diff --git a/8/README b/8/README
new file mode 100644 (file)
index 0000000..9d8aab5
--- /dev/null
+++ b/8/README
@@ -0,0 +1,14 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   add_item         add_item
+   add_item         add_itemV2
+   add_item         fileio
+   sstream          sstream
+   sstream          fileio (Note, fileio reads both add_item and sstream)
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/8/add_item.cc b/8/add_item.cc
new file mode 100644 (file)
index 0000000..015be26
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Sales_data.h"
+#include <iostream>
+
+using std::cerr; using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       Sales_data data1, data2;
+       read(cin, data1);                       // read the transactions
+       read(cin, data2);                      
+       // code to add into data1 and data2 unchanged from chapter 7
+       if (data1.isbn() == data2.isbn()) {     // check isbns
+               Sales_data sum = add(data1, data2); // add the transactions
+               print(cout, sum);                   // print the results
+               cout << endl;                       // followed by a newline 
+       }
+
+       return 0;
+}
diff --git a/8/add_itemV2.cc b/8/add_itemV2.cc
new file mode 100644 (file)
index 0000000..2c3234d
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include "Sales_data.h"
+#include <iostream>
+
+using std::cerr; using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       Sales_data data1, data2;
+       if (read(cin, data1) && read(cin, data2)) {  // read the transactions
+               if (data1.isbn() == data2.isbn()) {      // check isbns
+                       Sales_data sum = add(data1, data2);  // add the transactions
+                       print(cout, sum);                    // print the results
+                       cout << endl;                        // followed by a newline 
+               }
+       } else
+               cerr << "Input failed!" << endl;        
+
+       return 0;
+}
diff --git a/8/buf.cc b/8/buf.cc
new file mode 100644 (file)
index 0000000..3746694
--- /dev/null
+++ b/8/buf.cc
@@ -0,0 +1,53 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::endl; using std::flush; using std::ends;
+using std::unitbuf; using std::nounitbuf; using std::cout;
+
+int main()
+{
+       // writes hi and a newline, then flushes the buffer
+    cout << "hi!" << endl;  
+
+       // writes hi, then flushes the buffer; adds no data
+    cout << "hi!" << flush; 
+
+       // writes hi and a null, then flushes the buffer
+    cout << "hi!" << ends;  
+
+    cout << unitbuf;         // all writes will be flushed immediately
+
+       // any output is flushed immediately, no buffering
+    cout << "first" << " second" << endl;
+
+       cout << nounitbuf;       // returns to normal buffering
+
+       return 0;
+}
diff --git a/8/clearIO.cc b/8/clearIO.cc
new file mode 100644 (file)
index 0000000..f85657d
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+#include <sstream>
+using std::istringstream; 
+
+#include <string>
+using std::string;
+
+void read()
+{
+       // turns on both fail and bad bits
+       cin.setstate(cin.badbit | cin.eofbit | cin.failbit);
+}
+
+void off()
+{
+       // turns off failbit and badbit but all other bits unchanged
+       cin.clear(cin.rdstate() & ~cin.failbit & ~cin.badbit);
+} 
+
+
+int main()
+{
+       cout << "before read" << endl;
+       if (cin.good()) cout << "cin's good" << endl;
+       if (cin.bad()) cout << "cin's bad" << endl;
+       if (cin.fail()) cout << "cin's fail" << endl;
+       if (cin.eof()) cout << "cin's eof" << endl;
+       
+       read();
+       cout << "after read" << endl;
+       if (cin.good()) cout << "cin's good" << endl;
+       if (cin.bad()) cout << "cin's bad" << endl;
+       if (cin.fail()) cout << "cin's fail" << endl;
+       if (cin.eof()) cout << "cin's eof" << endl;
+       
+       off();
+       cout << "after off" << endl;
+       if (cin.good()) cout << "cin's good" << endl;
+       if (cin.bad()) cout << "cin's bad" << endl;
+       if (cin.fail()) cout << "cin's fail" << endl;
+       if (cin.eof()) cout << "cin's eof" << endl;
+       return 0;
+}
diff --git a/8/data/add_item b/8/data/add_item
new file mode 100644 (file)
index 0000000..92fd61c
--- /dev/null
@@ -0,0 +1,2 @@
+0-201-78345-X 3 20.00
+0-201-78345-X 2 25.00
diff --git a/8/data/sstream b/8/data/sstream
new file mode 100644 (file)
index 0000000..284eaef
--- /dev/null
@@ -0,0 +1,3 @@
+morgan 2015552368 8625550123 
+drew 9735550130 
+lee 6095550132 2015550175 8005550000
diff --git a/8/fileIO.cc b/8/fileIO.cc
new file mode 100644 (file)
index 0000000..7162d3c
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cerr; using std::cout; using std::endl;
+
+#include <fstream>
+using std::ifstream; 
+
+#include <string>
+using std::string;
+
+#include <stdexcept>
+using std::runtime_error;
+
+void process(ifstream &is)
+{
+       string s;
+       while (is >> s)
+               cout << s << endl;
+}
+
+int main(int argc, char* argv[])
+{
+       // for each file passed to the program
+       for (auto p = argv + 1; p != argv + argc; ++p) {
+           ifstream input(*p);   // create input and open the file
+           if (input) {          // if the file is ok, ``process'' this file
+               process(input);
+               } else
+               cerr << "couldn't open: " + string(*p);
+       } // input goes out of scope and is destroyed on each iteration
+       
+       auto p = argv + 1, end = argv + argc;
+       
+       ifstream input;           
+       while (p != end) {        // for each file passed to the program
+               input.open(*p);       // open the file, automatically clears the stream 
+           if (input) {          // if the file is ok, read and ``process'' the input
+                       process(input);
+               } else
+                       cerr << "couldn't open: " + string(*p);
+           input.close();        // close file when we're done with it
+           ++p;                  // increment pointer to get next file
+       }
+}
diff --git a/8/makefile b/8/makefile
new file mode 100644 (file)
index 0000000..74d1ddc
--- /dev/null
@@ -0,0 +1,18 @@
+# executable files for this directory
+OBJECTS = buf.exe clearIO.exe fileIO.exe add_item.exe \
+                 add_itemV2.exe sstream.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I../7
+
+add_itemV2.exe: add_itemV2.o ../7/Sales_data.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) add_itemV2.o ../7/Sales_data.o \
+               -o add_itemV2.exe
+
+add_item.exe: add_item.o ../7/Sales_data.o
+       $(CC) $(CCFLAGS) $(LOCFLAGS) add_item.o ../7/Sales_data.o \
+               -o add_item.exe
diff --git a/8/runpgms b/8/runpgms
new file mode 100644 (file)
index 0000000..4975537
--- /dev/null
+++ b/8/runpgms
@@ -0,0 +1,6 @@
+echo "add_item:" && ./add_item < data/add_item
+echo "add_itemV2:" && ./add_itemV2 < data/add_item
+echo "fileIO:" && ./fileIO data/add_item data/sstream
+echo "sstream:" && ./sstream < data/sstream
+
+../noinput buf.exe clearIO.exe 
diff --git a/8/sstream.cc b/8/sstream.cc
new file mode 100644 (file)
index 0000000..70901f3
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <iostream>
+using std::cin; using std::cout; using std::cerr;
+using std::istream; using std::ostream; using std::endl;
+
+#include <sstream>
+using std::ostringstream; using std::istringstream;
+
+#include <vector>
+using std::vector;
+
+#include <string>
+using std::string;
+
+// members are public by default
+struct PersonInfo { 
+       string name;
+       vector<string> phones;
+};
+
+// we'll see how to reformat phone numbers in chapter 17
+// for now just return the string we're given
+string format(const string &s) { return s; }
+
+bool valid(const string &s)
+{
+       // we'll see how to validate phone numbers 
+       // in chapter 17, for now just return true
+       return true;
+}
+
+vector<PersonInfo>
+getData(istream &is)
+{
+       // will hold a line and word from input, respectively
+       string line, word;
+
+       // will hold all the records from the input
+       vector<PersonInfo> people;
+
+       // read the input a line at a time until end-of-file (or other error)
+       while (getline(is, line)) {       
+               PersonInfo info;            // object to hold this record's data
+           istringstream record(line); // bind record to the line we just read
+               record >> info.name;        // read the name
+           while (record >> word)      // read the phone numbers 
+                       info.phones.push_back(word);  // and store them
+               people.push_back(info); // append this record to people
+       }
+       
+       return people;
+}
+
+ostream& process(ostream &os, vector<PersonInfo> people)
+{
+       for (const auto &entry : people) {    // for each entry in people
+               ostringstream formatted, badNums; // objects created on each loop
+               for (const auto &nums : entry.phones) {  // for each number 
+                       if (!valid(nums)) {           
+                               badNums << " " << nums;  // string in badNums
+                       } else                        
+                               // ``writes'' to formatted's string
+                               formatted << " " << format(nums); 
+               }
+               if (badNums.str().empty())      // there were no bad numbers
+                       os << entry.name << " "     // print the name 
+                          << formatted.str() << endl; // and reformatted numbers 
+               else                   // otherwise, print the name and bad numbers
+                       cerr << "input error: " << entry.name 
+                            << " invalid number(s) " << badNums.str() << endl;
+       }
+       
+       return os;
+}
+
+int main()
+{
+       process(cout, getData(cin));
+
+       return 0;
+}
diff --git a/9/README b/9/README
new file mode 100644 (file)
index 0000000..236afea
--- /dev/null
+++ b/9/README
@@ -0,0 +1,10 @@
+Some programs read cin for their input.
+Sample data files are in the data directory:
+
+     File           Programs that use that input file
+     ----           --------
+   erase            erase
+
+Programs not listed above print output and do
+not read any input
+
diff --git a/9/capacity.cc b/9/capacity.cc
new file mode 100644 (file)
index 0000000..fcdfe43
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+    vector<int> ivec;
+
+    // size should be zero; capacity is implementation defined
+    cout << "ivec: size: " << ivec.size()
+         << " capacity: "  << ivec.capacity() << endl;
+
+    // give ivec 24 elements
+    for (vector<int>::size_type ix = 0; ix != 24; ++ix) 
+         ivec.push_back(ix);
+     
+    // size should be 24; capacity will be >= 24 and is implementation defined
+    cout << "ivec: size: " << ivec.size()
+         << " capacity: "  << ivec.capacity() << endl;
+    ivec.reserve(50); // sets capacity to at least 50; might be more
+    // size should be 24; capacity will be >= 50 and is implementation defined
+    cout << "ivec: size: " << ivec.size()
+         << " capacity: "  << ivec.capacity() << endl;
+
+    // add elements to use up the excess capacity
+    while (ivec.size() != ivec.capacity())
+         ivec.push_back(0);
+
+    // capacity should be unchanged and size and capacity are now equal
+    cout << "ivec: size: " << ivec.size()
+         << " capacity: "  << ivec.capacity() << endl;
+    ivec.push_back(42); // add one more element
+
+    // size should be 51; capacity will be >= 51 and is implementation defined
+    cout << "ivec: size: " << ivec.size()
+         << " capacity: "  << ivec.capacity() << endl;
+
+       ivec.shrink_to_fit();  // ask for the memory to be returned
+
+    // size should be unchanged; capacity is implementation defined
+    cout << "ivec: size: " << ivec.size()
+         << " capacity: "  << ivec.capacity() << endl;
+
+return 0;
+}
+
diff --git a/9/data/erase b/9/data/erase
new file mode 100644 (file)
index 0000000..82e87be
--- /dev/null
@@ -0,0 +1,7 @@
+Esmeralda
+Frollo
+Hunckback
+Pierre
+Quasimodo 
+Torterue 
+Tristan
diff --git a/9/erase1.cc b/9/erase1.cc
new file mode 100644 (file)
index 0000000..7993fd3
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::find; 
+
+#include <string>
+using std::string; 
+
+#include <list>
+using std::list;
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cin; using std::cout; using std::endl;
+
+int main()
+{
+       list<string> slist;
+       string s;
+       while (cin >> s)
+               slist.push_back(s);  // read the contents into slist
+
+       /* we'll explain find in chapter 10
+        * find looks in the sequence denoted by its first two
+        * iterator arguments for the value of its third argument
+        * returns an iterator to the first element with that value
+        * if that element exists in the input sequence
+        * otherwise returns the end iterator
+       */
+       auto iter = find(slist.begin(), slist.end(), "Quasimodo");
+       if (iter != slist.end())
+            slist.erase(iter);
+       
+       auto orig = slist; // keep a copy before we destroy the contents
+       slist.clear();     // delete all the elements within the container
+       cout << "after clear, size is: " << slist.size() << endl;
+       
+       slist = orig; // restore the data
+       slist.erase(slist.begin(), slist.end()); // equivalent
+       cout << "after erase begin to end, size is: " << slist.size() << endl;
+       
+       slist = orig; // restore the data
+       auto elem1 = slist.begin(), elem2 = slist.end();
+       // delete the range of elements between two iterators
+       // returns an iterator to the element just after the last removed element
+       elem1 = slist.erase(elem1, elem2); // after the call elem1 == elem2
+       cout << "after erase elem1 to elem2 size is: " << slist.size() << endl;
+       
+       if (elem1 != elem2)
+               cout << "somethings wrong" << endl;
+       else
+               cout << "okay, they're equal " << endl;
+       
+}
diff --git a/9/erase2.cc b/9/erase2.cc
new file mode 100644 (file)
index 0000000..1eb95ee
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <algorithm>
+using std::find; 
+
+#include <string>
+using std::string; 
+
+#include <list>
+using std::list;
+
+#include <forward_list>
+using std::forward_list;
+
+#include <vector>
+using std::vector; 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main() 
+{
+       // lst has ten elements 0 ... 9 in value
+       list<int> lst = {0,1,2,3,4,5,6,7,8,9};
+
+       // print the initial values in lst
+       cout << "initial list: ";
+       for (auto it : lst)
+               cout << it << " ";
+       cout << endl;
+
+       // erase the odd elements in lst
+       auto it = lst.begin(); 
+       while (it != lst.end())
+               if (*it % 2)             // if the element is odd
+                       it = lst.erase(it);  // erase this element
+               else
+                       ++it;
+
+       // print the current contents of lst
+       cout << "after erasing odd elements from lst: ";
+       for (auto it : lst)
+               cout << it << " ";
+       cout << endl;
+       
+       // repeat the same actions but on a forward_list
+       forward_list<int> flst = {0,1,2,3,4,5,6,7,8,9};
+
+       // print the initial values in flst
+       cout << "initial list: ";
+       for (auto it : flst)
+               cout << it << " ";
+       cout << endl;
+
+       // erase the odd elements in flst
+       auto prev = flst.before_begin(); // element "off the start" of flst
+       auto curr = flst.begin();     // denotes the first element in flst
+       while (curr != flst.end()) {  // while there are still elements 
+               if (*curr % 2)                     // if the element is odd
+               curr = flst.erase_after(prev); // erase it and move curr 
+               else {
+                       prev = curr; // move the iterators to denote the next
+                       ++curr;      // element and one before the next element
+               }
+       }
+
+       // print the current contents of lst
+       cout << "after erasing elements from flst: ";
+       for (auto it : flst)
+               cout << it << " ";
+       cout << endl;
+
+       return 0;
+}
diff --git a/9/find-str.cc b/9/find-str.cc
new file mode 100644 (file)
index 0000000..fb2e647
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string; 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main() 
+{
+    string name("AnnaBelle");
+    auto pos1 = name.find("Anna"); // pos1 == 0
+    cout << pos1 ;
+    string lowercase("annabelle");
+    pos1 = lowercase.find("Anna");   // pos1 == npos
+    cout << " " << pos1 << endl;
+    return 0;
+}
diff --git a/9/find_ops.cc b/9/find_ops.cc
new file mode 100644 (file)
index 0000000..34a369a
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string; 
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main() 
+{
+       string numbers("0123456789"), name("r2d2");
+       // returns 1, i.e., the index of the first digit in name
+       auto pos = name.find_first_of(numbers);  
+       if (pos != string::npos)
+               cout << "found number at index: " << pos 
+                    << " element is " << name[pos] << endl;
+       else
+               cout << "no number in: " << name << endl;
+       
+       pos = 0;  
+       // each iteration finds the next number in name
+       while ((pos = name.find_first_of(numbers, pos)) 
+                     != string::npos) {
+           cout << "found number at index: " << pos 
+                << " element is " << name[pos] << endl;
+       
+           ++pos; // move to the next character
+       }
+       
+       string river("Mississippi");
+       
+       auto first_pos = river.find("is");  // returns 1
+       auto last_pos = river.rfind("is");  // returns 4
+       cout << "find returned: " << first_pos 
+            << " rfind returned: " << last_pos << endl;
+       
+       string dept("03714p3");
+       // returns 5, which is the index to the character 'p'
+       pos = dept.find_first_not_of(numbers);
+       cout << "first_not returned: " << pos << endl;
+       
+       return 0;
+}
diff --git a/9/makefile b/9/makefile
new file mode 100644 (file)
index 0000000..b52891b
--- /dev/null
@@ -0,0 +1,11 @@
+# executable files for this directory
+OBJECTS = capacity.exe erase1.exe erase2.exe find_ops.exe \
+                 find-str.exe refreshIter.exe stack.exe \
+                 str_assignOps.exe substr.exe useConvs.exe
+
+# tells make to use the file "../GNU_makefile_template", which
+# defines general rules for making .o and .exe files
+include ../GNU_makefile_template
+
+LOCFLAGS = -I..\7
+
diff --git a/9/refreshIter.cc b/9/refreshIter.cc
new file mode 100644 (file)
index 0000000..589fe20
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+void printVec(const vector<int> &vi)
+{
+       // print the vector's elements
+       auto iter = vi.begin();
+       while (iter != vi.end())
+               cout << *iter++ << endl;
+}
+
+int main()
+{
+       // silly loop to remove even-valued elements 
+       // and insert a duplicate of odd-valued elements
+       vector<int> vi = {0,1,2,3,4,5,6,7,8,9};
+       printVec(vi);
+
+       // we call begin, not cbegin because we're changing vi
+       auto iter = vi.begin(); 
+       while (iter != vi.end()) {
+               if (*iter % 2) {    // if the element is odd
+                       iter = vi.insert(iter, *iter);  // duplicate  it
+                       iter += 2; // advance past this element and the new one 
+               } else 
+                       iter = vi.erase(iter);          // remove even elements
+                       // don't advance the iterator; 
+                       // iter denotes the element after the one we erased
+       }
+       printVec(vi);
+
+       return 0;
+}
+       
diff --git a/9/runpgms b/9/runpgms
new file mode 100644 (file)
index 0000000..c747211
--- /dev/null
+++ b/9/runpgms
@@ -0,0 +1,5 @@
+echo "erase1: " && ./erase1 < data/erase
+
+../noinput capacity.exe erase2.exe find_ops.exe \
+                  find-str.exe refreshIter.exe stack.exe \
+                  str_assignOps.exe substr.exe useConvs.exe
diff --git a/9/stack.cc b/9/stack.cc
new file mode 100644 (file)
index 0000000..60ab145
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <cstddef>
+using std::size_t;
+
+#include <deque>
+using std::deque;
+
+#include <stack>
+using std::stack; 
+
+#include <iostream>
+using std::cout; using std::cerr; using std::endl;
+
+bool process(int);
+
+int main()
+{
+       stack<int> intStack;  // empty stack
+
+       // fill up the stack
+       for (size_t ix = 0; ix != 10; ++ix)
+       intStack.push(ix);   // intStack holds 0 . . . 9 inclusive
+
+       // while there are still values in intStack
+       while (!intStack.empty()) {
+       int value = intStack.top();
+       // code that uses value
+               cout << value << endl;
+       intStack.pop(); // pop the top element, and repeat
+       }
+
+       return 0;
+}
diff --git a/9/str_assignOps.cc b/9/str_assignOps.cc
new file mode 100644 (file)
index 0000000..8ffa561
--- /dev/null
@@ -0,0 +1,106 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <vector>
+using std::vector;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+
+int main() 
+{
+       string s = "some string", s2 = "some other string";
+       // equivalent ways to insert all the characters from s2 at beginning of s
+       // insert iterator range before s.begin()
+       s.insert(s.begin(), s2.begin(), s2.end());  
+       cout << "insert iterators version:        " << s << endl;
+       
+       s = "some string";
+       s.insert(0, s2); // insert a copy of s2 before position 0 in s
+       cout << "insert string at given position: " << s << endl;
+
+       s = "some string";
+       // insert s2.size() characters from s2 starting at s2[0] before s[0]
+       s.insert(0, s2, 0, s2.size());   
+       cout << "insert positional version:       " << s << endl;
+
+       
+       s = "";  // s is now empty
+       vector<char> c_vec(1, 'a');
+       // insert characters from c_vec into s
+       s.insert(s.begin(), c_vec.begin(), c_vec.end());  
+       s.insert(s.size(), 5, '!'); // add five exclamation points at the end of s
+       cout << s << endl;
+
+       s.erase(s.size() - 5, 5);   // erase the last five characters from s
+       cout << s << endl;
+
+       s = "";  // s is now empty
+       const char *cp = "Stately, plump Buck";
+       s.assign(cp, 7);            // s == "Stately"
+       cout << s << endl;
+       s.insert(s.size(), cp + 7); // s == "Stately, plump Buck"
+       cout << s << endl;
+       
+       s = "C++ Primer";  // reset s and s2
+       s2 = s;            // to "C++ Primer"
+       s.insert(s.size(), " 4th Ed."); // s == "C++ Primer 4th Ed."
+       s2.append(" 4th Ed."); // equivalent: appends " 4th Ed." to s2; 
+       cout << s << " " << s2 << endl;
+       
+       // two ways to replace "4th" by "5th"
+       // 1. insert and erase
+       s.erase(11, 3);                 // s == "C++ Primer Ed."
+       s.insert(11, "5th");            // s == "C++ Primer 5th Ed."
+       
+       // 2. use replace
+       // erase three characters starting at position 11
+       //  and then insert "5th"
+       s2.replace(11, 3, "5th"); // equivalent: s == s2
+       
+       cout << s << " " << s2 << endl;
+       
+       // two ways to replace "5th" by "Fifth"
+       // 1. use replace if we know where the string we want to replace is
+       s.replace(11, 3, "Fifth"); // s == "C++ Primer Fifth Ed."
+       
+       // 2. call find first to get position from which to replace
+       auto pos = s2.find("5th");
+       if (pos != string::npos)
+               s2.replace(pos, 3, "Fifth");
+       else
+               cout << "something's wrong, s2 is: " << s2 << endl;
+       cout << s << " " << s2 << endl;
+
+       return 0;
+}
diff --git a/9/substr.cc b/9/substr.cc
new file mode 100644 (file)
index 0000000..c3ca04c
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+#include <string>
+using std::string;
+
+#include <iostream>
+using std::cout; using std::endl;
+
+#include <stdexcept>
+using std::out_of_range;
+
+int main()
+{
+       try {
+               string s("hello world");
+               cout << s.substr(0, 5) << endl;  // prints hello
+               cout << s.substr(6) << endl;     // prints world
+               cout << s.substr(6, 11) << endl; // prints world
+               cout << s.substr(12) << endl;    // throws out_of_range 
+       } catch(out_of_range) {cout << "caught out_of_range" << endl; }
+
+       return 0;
+}
diff --git a/9/useConvs.cc b/9/useConvs.cc
new file mode 100644 (file)
index 0000000..a3c1411
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced.Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/ 
+
+// Version_test.h contains definitions for to_string and stod
+// if the compiler does not yet define these functions,
+// this code will use the definitions we provide
+#include "Version_test.h" 
+
+#include <string>
+using std::string;
+#ifdef STRING_NUMERIC_CONVS
+using std::to_string; using std::stod;
+#endif
+
+#include <iostream>
+using std::cout; using std::endl;
+
+int main()
+{
+       int i = 42;
+       // converts the int i to its character representation
+       string s = to_string(i);  
+
+       double d = stod(s);   // converts the string s to floating-point
+       cout << "i = " << i << " s = " << s << " d is: " << d << endl;
+
+       // convert the first substring in s that starts with a digit,  d = 3.14
+       string s2 = "pi = 3.14";
+       d = stod(s2.substr(s2.find_first_of("+-.0123456789")));
+
+       cout << "d = " << d << " s = " << s << " s2 is: " << s2 << endl;
+
+       return 0;
+}
diff --git a/CompilerNotes.pdf b/CompilerNotes.pdf
new file mode 100644 (file)
index 0000000..d1b406f
Binary files /dev/null and b/CompilerNotes.pdf differ
diff --git a/GCC 4.7 README.pdf b/GCC 4.7 README.pdf
new file mode 100644 (file)
index 0000000..c85ee47
Binary files /dev/null and b/GCC 4.7 README.pdf differ
diff --git a/GNU_makefile_template b/GNU_makefile_template
new file mode 100644 (file)
index 0000000..a92dc64
--- /dev/null
@@ -0,0 +1,56 @@
+#g++ compiler: options
+
+# -std=c++0x enables ISO C++ 11 standard
+# -I.. pulls in the Version_test.h file for conditional compilation
+#      of code the uses features that might not yet be implemented
+CC = g++
+CCFLAGS = -std=c++0x -I..
+
+# Some programs include headers defined in earlier chapters
+# LOCFLAGS used to set tell the compiler where to find a
+# header that is not in the same directory as the source file itself
+# LOCFLAGS will be set in directory level makefiles as needed
+LOCFLAGS =
+
+### 
+
+####### To compile without using a makefile
+# To compile an object file from a source file you could execute
+# g++ -std=c++0x -c filename.cc # produces filename.obj
+# To compile an executable file from an object file, you would execute
+# g++ -std=c++0x filename.o      # produces filename.exe
+# To compile an executable file from a source file, you would execute
+# g++ -std=c++0x filename.cc    # produces filename.exe
+#######
+
+# each subdirectory contains a Makefile that lists the executable
+# files that can be made in that directory.  That list is assigned
+# to the make macro named $OBJECTS
+# This rule says that the make target named "all" depends on those
+# files.  Executing "make all" in a subdirectory will cause make
+# to build each .exe file listed in that subdirectory's makefile
+all: $(OBJECTS) 
+
+# rule that says how to make a .o object file from a .cc source file
+# for a given source file in a given directory you could compile it
+# into an object file by executing "make filename.o"
+
+# $< and $@ are macros defined by make
+#     $< refers to the file being processed (i.e., compiled or linked)
+#     $@ refers to the generated file
+%.o: %.cc 
+       $(CC) $(CCFLAGS) $(LOCFLAGS) -c $< -o $@
+
+# rule that says how to make a .exe executable file from a .o object file
+%.exe: %.o 
+       $(CC) $(CCFLAGS) $(LOCFLAGS) $< -o $@
+
+# target to clean up the object files and any core files
+# executing "make clean" in a subdirectory will remove all
+# files named core or any file ending in .obj, or .stackdump
+clean:
+       rm -rf *.o core *.stackdump
+
+# target to remove executable files as well as object and core files
+clobber: clean
+       rm -rf *.exe 
diff --git a/Version_test.h b/Version_test.h
new file mode 100644 (file)
index 0000000..188b49d
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * This file contains code from "C++ Primer, Fifth Edition", by Stanley B.
+ * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the
+ * copyright and warranty notices given in that book:
+ * 
+ * "Copyright (c) 2013 by Objectwrite, Inc., Josee Lajoie, and Barbara E. Moo."
+ * 
+ * 
+ * "The authors and publisher have taken care in the preparation of this book,
+ * but make no expressed or implied warranty of any kind and assume no
+ * responsibility for errors or omissions. No liability is assumed for
+ * incidental or consequential damages in connection with or arising out of the
+ * use of the information or programs contained herein."
+ * 
+ * Permission is granted for this code to be used for educational purposes in
+ * association with the book, given proper citation if and when posted or
+ * reproduced. Any commercial use of this code requires the explicit written
+ * permission of the publisher, Addison-Wesley Professional, a division of
+ * Pearson Education, Inc. Send your request for permission, stating clearly
+ * what code you would like to use, and in what specific way, to the following
+ * address: 
+ * 
+ *     Pearson Education, Inc.
+ *     Rights and Permissions Department
+ *     One Lake Street
+ *     Upper Saddle River, NJ  07458
+ *     Fax: (201) 236-3290
+*/
+
+#ifndef VERSION_TEST_H
+#define VERSION_TEST_H
+
+
+/* As of the first printing of C++ Primer, 5th Edition (July 2012), 
+ * the gcc Version 4.7.0 did not yet support some C++ 11 features.  
+ *
+ * The code we distribute contains both normal C++ code and 
+ * workarounds for missing features.  We use preprocessor variables to
+ * determine whether a given features is implemented in a given release
+ * of the compiler.  
+ * The base version we used to test the code in the book
+ * is gcc version 4.7.0 (GCC) 
+ *
+ * When new releases are available we will update this file which will
+ * #define the features implmented in that release.
+*/
+
+#if __cplusplus == 201103L 
+// base version, future releases of this file will 
+// #define these variables as features as they are implemented 
+
+/* Code in this delivery use the following variables to control compilation
+
+   Variable tests           C++ 11 Feature 
+HEX_MANIPS               hexfloat and defaultfloat manipulators
+REFMEMS                  reference qualified member functions
+REGEX                    regular expressions library
+STRING_NUMERIC_CONVS     conversions to and from string to numeric
+*/
+#endif  // ends compiler version check
+
+#ifndef STRING_NUMERIC_CONVS
+// if the library doesn't define to_string 
+// or the numeric conversion functions
+// as a workaround we define to_string and stod in this file
+
+// Readers can ignore the implemnetations of to_string and stod 
+// but can use these functions in their code.
+
+#include <iostream>
+#include <cstdlib>
+#include <cstddef>
+#include <string>
+// we use sprintf from stdio to implement to_string
+#include <cstdio>
+
+inline
+std::string to_string(int i)
+{
+       char buf[100];
+       std::sprintf(buf, "%d", i);
+       return buf;
+}
+
+inline
+double stod(const std::string &s, std::size_t * = 0)
+{
+       char **buf = 0;
+       return std::strtod(s.c_str(), buf);
+}
+#endif   // STRING_NUMERIC_CONVS
+
+#include <iostream>
+
+#ifndef HEX_MANIPS
+inline
+std::ostream &defaultfloat(std::ostream &os) 
+{
+       os.unsetf(std::ios_base::floatfield);
+       return os;
+}
+#endif   // HEX_MANIPS
+
+#endif  // ends header guard
+
diff --git a/makefile b/makefile
new file mode 100644 (file)
index 0000000..bec114f
--- /dev/null
+++ b/makefile
@@ -0,0 +1,65 @@
+DIRS = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
+
+all:  
+       cd 1; make 
+       cd 2; make 
+       cd 3; make 
+       cd 4; make 
+       cd 5; make 
+       cd 6; make
+       cd 7; make 
+       cd 8; make 
+       cd 9; make
+       cd 10; make
+       cd 11; make
+       cd 12; make
+       cd 13; make 
+       cd 14; make 
+       cd 15; make 
+       cd 16; make 
+       cd 17; make 
+       cd 18; make 
+       cd 19; make
+
+clean: 
+       cd 1; make clean 
+       cd 2; make clean 
+       cd 3; make clean 
+       cd 4; make clean 
+       cd 5; make clean 
+       cd 6; make clean 
+       cd 7; make clean 
+       cd 8; make clean 
+       cd 9; make clean
+       cd 10; make clean
+       cd 11; make clean
+       cd 12; make clean
+       cd 13; make clean 
+       cd 14; make clean 
+       cd 15; make clean 
+       cd 16; make clean 
+       cd 17; make clean 
+       cd 18; make clean 
+       cd 19; make clean
+
+clobber: 
+       cd 1; make clobber 
+       cd 2; make clobber 
+       cd 3; make clobber 
+       cd 4; make clobber 
+       cd 5; make clobber 
+       cd 6; make clobber 
+       cd 7; make clobber 
+       cd 8; make clobber 
+       cd 9; make clobber
+       cd 10; make clobber
+       cd 11; make clobber
+       cd 12; make clobber
+       cd 13; make clobber 
+       cd 14; make clobber 
+       cd 15; make clobber 
+       cd 16; make clobber 
+       cd 17; make clobber 
+       cd 18; make clobber 
+       cd 19; make clobber
+
diff --git a/noinput b/noinput
new file mode 100644 (file)
index 0000000..e247914
--- /dev/null
+++ b/noinput
@@ -0,0 +1,5 @@
+for i 
+do
+    echo $i
+    ./$i
+done
diff --git a/runpgms b/runpgms
new file mode 100644 (file)
index 0000000..c8ccd61
--- /dev/null
+++ b/runpgms
@@ -0,0 +1,7 @@
+for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
+do
+       echo $i
+       cd $i 
+       runpgms 
+       cd ..
+done