├── Makefile ├── Makefile.h ├── README ├── algo ├── Makefile ├── accu1.cpp ├── adjdiff1.cpp ├── adjfind1.cpp ├── algostuff.hpp ├── bounds1.cpp ├── bsearch1.cpp ├── copy1.cpp ├── copy2.cpp ├── copy3.cpp ├── count1.cpp ├── eqrange1.cpp ├── equal1.cpp ├── fill1.cpp ├── find1.cpp ├── find2.cpp ├── findend1.cpp ├── findof1.cpp ├── foreach1.cpp ├── foreach2.cpp ├── foreach3.cpp ├── generate.cpp ├── heap1.cpp ├── imerge1.cpp ├── includes.cpp ├── inner1.cpp ├── lexico1.cpp ├── merge1.cpp ├── minmax1.cpp ├── misma1.cpp ├── nth1.cpp ├── part1.cpp ├── partsum1.cpp ├── perm1.cpp ├── psort1.cpp ├── psort2.cpp ├── random1.cpp ├── relabs.cpp ├── remove1.cpp ├── remove2.cpp ├── replace1.cpp ├── replace2.cpp ├── reverse1.cpp ├── rotate1.cpp ├── rotate2.cpp ├── search1.cpp ├── search2.cpp ├── searchn1.cpp ├── setalgos.cpp ├── sort1.cpp ├── sort2.cpp ├── swap1.cpp ├── transf1.cpp ├── transf2.cpp ├── unique1.cpp ├── unique2.cpp └── unique3.cpp ├── cont ├── Makefile ├── Queue.hpp ├── Stack.hpp ├── array1.cpp ├── bitset1.cpp ├── bitset2.cpp ├── carray.hpp ├── carray1.cpp ├── countptr.hpp ├── deque1.cpp ├── list1.cpp ├── map1.cpp ├── mapcmp.cpp ├── mapfind.cpp ├── mmap1.cpp ├── mset1.cpp ├── newkey.hpp ├── pqueue1.cpp ├── print.hpp ├── queue1.cpp ├── queue2.cpp ├── refsem1.cpp ├── set1.cpp ├── set2.cpp ├── setcmp.cpp ├── sortset.cpp ├── sortvec.cpp ├── stack1.cpp ├── stack2.cpp └── vector1.cpp ├── fo ├── Makefile ├── compose1.cpp ├── compose10.hpp ├── compose11.hpp ├── compose12.hpp ├── compose2.cpp ├── compose21.hpp ├── compose22.hpp ├── compose3.cpp ├── compose4.cpp ├── fopow.hpp ├── fopow1.cpp ├── foreach3.cpp ├── genera1.cpp ├── genera2.cpp ├── memfun1.cpp ├── nullary.hpp ├── print.hpp ├── removeif.cpp └── sort1.cpp ├── i18n ├── Makefile ├── loc1.cpp ├── loc2.cpp └── numget.cpp ├── io ├── Makefile ├── cat1.cpp ├── cat2.cpp ├── charcat1.cpp ├── charcat2.cpp ├── charset.cpp ├── copy1.cpp ├── copy2.cpp ├── countlines.cpp ├── frac1in.hpp ├── frac1out.hpp ├── frac2in.hpp ├── frac2out.hpp ├── ignore.hpp ├── ignore1.cpp ├── ignoreparam.hpp ├── ignoreparam1.cpp ├── inbuf1.cpp ├── inbuf1.hpp ├── io1.cpp ├── outbuf1.cpp ├── outbuf1.hpp ├── outbuf1x.cpp ├── outbuf1x.hpp ├── outbuf2.cpp ├── outbuf2.hpp ├── outbuf3.cpp ├── outbuf3.hpp ├── rdbuf1.cpp ├── rdbuf2.cpp ├── redirect.cpp ├── rw1.cpp ├── sstr1.cpp ├── sum1.cpp └── sum2.cpp ├── iter ├── Makefile ├── advance1.cpp ├── advance2.cpp ├── assoiter.cpp ├── assoiter.hpp ├── backins.cpp ├── distance.cpp ├── distance.hpp ├── frontins.cpp ├── inserter.cpp ├── istriter.cpp ├── itercat.cpp ├── ostriter.cpp ├── print.hpp ├── reviter1.cpp ├── reviter2.cpp ├── reviter3.cpp ├── reviter4.cpp └── swap1.cpp ├── memory ├── Makefile ├── myalloc.hpp └── myalloc1.cpp ├── num ├── Makefile ├── complex1.cpp ├── complex2.cpp ├── gslice1.cpp ├── indi1.cpp ├── masked1.cpp ├── slice1.cpp ├── val1.cpp └── val2.cpp ├── stl ├── Makefile ├── add1.cpp ├── algo1.cpp ├── copy1.cpp ├── copy2.cpp ├── copy3.cpp ├── deque1.cpp ├── find1.cpp ├── fo1.cpp ├── foreach1.cpp ├── foreach2.cpp ├── ioiter1.cpp ├── iterbug1.cpp ├── list1.cpp ├── list2.cpp ├── map1.cpp ├── mmap1.cpp ├── prime1.cpp ├── print.hpp ├── remove1.cpp ├── remove2.cpp ├── remove3.cpp ├── remove4.cpp ├── riter1.cpp ├── set1.cpp ├── sort1.cpp ├── transform1.cpp └── vector1.cpp ├── string ├── Makefile ├── icstring.hpp ├── icstring1.cpp ├── iter1.cpp ├── iter2.cpp ├── iter3.cpp ├── string1.cpp ├── string2.cpp └── unique.cpp └── util ├── Makefile ├── autoptr.hpp ├── autoptr1.cpp ├── autoptr2.cpp ├── defalloc.hpp ├── limits1.cpp └── minmax1.cpp /Makefile: -------------------------------------------------------------------------------- 1 | include Makefile.h 2 | 3 | all:: 4 | 5 | CODE_DIRS = util stl cont iter fo algo string num io i18n memory 6 | all:: 7 | @for DIR in $(CODE_DIRS); \ 8 | do \ 9 | (cd $$DIR; make all) \ 10 | done 11 | clean:: 12 | @for DIR in $(CODE_DIRS); \ 13 | do \ 14 | (cd $$DIR; make clean) \ 15 | done 16 | 17 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | These are the examples from the following book: 2 | Nicolai M. Josuttis 3 | The C++ Standard Library - A Tutorial and Reference 4 | Addison Wesley Longman, 1999 5 | ISBN 0-201-37926-0 6 | 7 | For further informations see: 8 | http://www.josuttis.com/libbook/ 9 | 10 | I welcome your feedback. 11 | The best way to reach me is by Email: 12 | libbook@josuttis.com 13 | 14 | Copyright 1999 by Addison Wesley Longman, Inc. and Nicolai M. Josuttis. 15 | All rights reserved. 16 | 17 | Permission to use, copy, modify and distribute this software for personal 18 | and educational use is hereby granted without fee, provided that the above 19 | copyright notice appears in all copies and that both that copyright notice 20 | and this permission notice appear in supporting documentation, and that the 21 | names of Addison Wesley Longman or the author are not used in advertising or 22 | publicity pertaining to distribution of the software without specific, 23 | written prior permission. Addison Wesley Longman and the author make no 24 | representations about the suitability of this software for any purpose. 25 | It is provided "as is" without express or implied warranty. 26 | 27 | ADDISON WESLEY LONGMAN AND THE AUTHOR DISCLAIM ALL WARRANTIES WITH REGARD 28 | TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 29 | AND FITNESS. 30 | IN NO EVENT SHALL ADDISON WESLEY LONGMAN OR THE AUTHOR BE LIABLE FOR ANY 31 | SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER 32 | RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF 33 | CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 34 | CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 35 | 36 | -------------------------------------------------------------------------------- /algo/Makefile: -------------------------------------------------------------------------------- 1 | OUTPROGS = \ 2 | foreach1 foreach2 foreach3 \ 3 | count1 minmax1 \ 4 | find1 find2 \ 5 | searchn1 search1 search2 \ 6 | findend1 findof1 adjfind1 \ 7 | equal1 misma1 lexico1 \ 8 | copy1 copy2 \ 9 | transf1 transf2 \ 10 | swap1 fill1 generate \ 11 | replace1 replace2 \ 12 | remove1 remove2 \ 13 | unique1 unique2 unique3 \ 14 | reverse1 rotate1 rotate2 \ 15 | perm1 random1 \ 16 | part1 sort1 sort2 psort1 psort2 nth1 \ 17 | heap1 \ 18 | bsearch1 includes bounds1 eqrange1 \ 19 | merge1 setalgos imerge1 \ 20 | accu1 inner1 partsum1 adjdiff1 relabs 21 | 22 | CPPPROGS = copy3 23 | 24 | HEADERS = algostuff.hpp 25 | 26 | include ../Makefile.h 27 | 28 | -------------------------------------------------------------------------------- /algo/adjdiff1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | deque coll; 17 | 18 | INSERT_ELEMENTS(coll,1,6); 19 | PRINT_ELEMENTS(coll); 20 | 21 | // print all differences between elements 22 | adjacent_difference (coll.begin(), coll.end(), // source 23 | ostream_iterator(cout," ")); // dest. 24 | cout << endl; 25 | 26 | // print all sums with the predecessors 27 | adjacent_difference (coll.begin(), coll.end(), // source 28 | ostream_iterator(cout," "), // dest. 29 | plus()); // operation 30 | cout << endl; 31 | 32 | // print all products between elements 33 | adjacent_difference (coll.begin(), coll.end(), // source 34 | ostream_iterator(cout," "), // dest. 35 | multiplies()); // operation 36 | cout << endl; 37 | } 38 | -------------------------------------------------------------------------------- /algo/algostuff.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #ifndef ALGOSTUFF_HPP 12 | #define ALGOSTUFF_HPP 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | /* PRINT_ELEMENTS() 27 | * - prints optional C-string optcstr followed by 28 | * - all elements of the collection coll 29 | * - separated by spaces 30 | */ 31 | template 32 | inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") 33 | { 34 | typename T::const_iterator pos; 35 | 36 | std::cout << optcstr; 37 | for (pos=coll.begin(); pos!=coll.end(); ++pos) { 38 | std::cout << *pos << ' '; 39 | } 40 | std::cout << std::endl; 41 | } 42 | 43 | /* INSERT_ELEMENTS (collection, first, last) 44 | * - fill values from first to last into the collection 45 | * - NOTE: NO half-open range 46 | */ 47 | template 48 | inline void INSERT_ELEMENTS (T& coll, int first, int last) 49 | { 50 | for (int i=first; i<=last; ++i) { 51 | coll.insert(coll.end(),i); 52 | } 53 | } 54 | 55 | #endif /*ALGOSTUFF_HPP*/ 56 | -------------------------------------------------------------------------------- /algo/bounds1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | list coll; 17 | 18 | INSERT_ELEMENTS(coll,1,9); 19 | INSERT_ELEMENTS(coll,1,9); 20 | coll.sort (); 21 | PRINT_ELEMENTS(coll); 22 | 23 | // print first and last position 5 could get inserted 24 | list::iterator pos1, pos2; 25 | 26 | pos1 = lower_bound (coll.begin(), coll.end(), 27 | 5); 28 | pos2 = upper_bound (coll.begin(), coll.end(), 29 | 5); 30 | 31 | cout << "5 could get position " 32 | << distance(coll.begin(),pos1) + 1 33 | << " up to " 34 | << distance(coll.begin(),pos2) + 1 35 | << " without breaking the sorting" << endl; 36 | 37 | // insert 3 at the first possible position without breaking the sorting 38 | coll.insert (lower_bound(coll.begin(),coll.end(), 39 | 3), 40 | 3); 41 | 42 | // insert 7 at the last possible position without breaking the sorting 43 | coll.insert (upper_bound(coll.begin(),coll.end(), 44 | 7), 45 | 7); 46 | 47 | PRINT_ELEMENTS(coll); 48 | } 49 | -------------------------------------------------------------------------------- /algo/bsearch1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | list coll; 17 | 18 | INSERT_ELEMENTS(coll,1,9); 19 | PRINT_ELEMENTS(coll); 20 | 21 | // check existence of element with value 5 22 | if (binary_search(coll.begin(), coll.end(), 5)) { 23 | cout << "5 is present" << endl; 24 | } 25 | else { 26 | cout << "5 is not present" << endl; 27 | } 28 | 29 | // check existence of element with value 42 30 | if (binary_search(coll.begin(), coll.end(), 42)) { 31 | cout << "42 is present" << endl; 32 | } 33 | else { 34 | cout << "42 is not present" << endl; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /algo/copy1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | vector coll1; 17 | list coll2; 18 | 19 | INSERT_ELEMENTS(coll1,1,9); 20 | 21 | /* copy elements of coll1 into coll2 22 | * - use back inserter to insert instead of overwrite 23 | */ 24 | copy (coll1.begin(), coll1.end(), // source range 25 | back_inserter(coll2)); // destination range 26 | 27 | /* print elements of coll2 28 | * - copy elements to cout using an ostream iterator 29 | */ 30 | copy (coll2.begin(), coll2.end(), // source range 31 | ostream_iterator(cout," ")); // destination range 32 | cout << endl; 33 | 34 | /* copy elements of coll1 into coll2 in reverse order 35 | * - now overwriting 36 | */ 37 | copy (coll1.rbegin(), coll1.rend(), // source range 38 | coll2.begin()); // destination range 39 | 40 | // print elements of coll2 again 41 | copy (coll2.begin(), coll2.end(), // source range 42 | ostream_iterator(cout," ")); // destination range 43 | cout << endl; 44 | } 45 | -------------------------------------------------------------------------------- /algo/copy2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | /* initialize source collection with ``..........abcdef..........'' 17 | */ 18 | vector source(10,'.'); 19 | for (int c='a'; c<='f'; c++) { 20 | source.push_back(c); 21 | } 22 | source.insert(source.end(),10,'.'); 23 | PRINT_ELEMENTS(source,"source: "); 24 | 25 | // copy all letters three elements in front of the 'a' 26 | vector c1(source.begin(),source.end()); 27 | copy (c1.begin()+10, c1.begin()+16, // source range 28 | c1.begin()+7); // destination range 29 | PRINT_ELEMENTS(c1,"c1: "); 30 | 31 | // copy all letters three elements behind the 'f' 32 | vector c2(source.begin(),source.end()); 33 | copy_backward (c2.begin()+10, c2.begin()+16, // source range 34 | c2.begin()+19); // destination range 35 | PRINT_ELEMENTS(c2,"c2: "); 36 | } 37 | -------------------------------------------------------------------------------- /algo/copy3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | copy (istream_iterator(cin), // beginning of source 20 | istream_iterator(), // end of source 21 | ostream_iterator(cout,"\n")); // destination 22 | } 23 | -------------------------------------------------------------------------------- /algo/count1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | bool isEven (int elem) 15 | { 16 | return elem % 2 == 0; 17 | } 18 | 19 | int main() 20 | { 21 | vector coll; 22 | int num; 23 | INSERT_ELEMENTS(coll,1,9); 24 | PRINT_ELEMENTS(coll,"coll: "); 25 | 26 | // count and print elements with value 4 27 | num = count (coll.begin(), coll.end(), // range 28 | 4); // value 29 | cout << "number of elements equal to 4: " << num << endl; 30 | 31 | // count elements with even value 32 | num = count_if (coll.begin(), coll.end(), // range 33 | isEven); // criterion 34 | cout << "number of elements with even value: " << num << endl; 35 | 36 | // count elements that are greater than value 4 37 | num = count_if (coll.begin(), coll.end(), // range 38 | bind2nd(greater(),4)); // criterion 39 | cout << "number of elements greater than 4: " << num << endl; 40 | } 41 | -------------------------------------------------------------------------------- /algo/eqrange1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | list coll; 17 | 18 | INSERT_ELEMENTS(coll,1,9); 19 | INSERT_ELEMENTS(coll,1,9); 20 | coll.sort (); 21 | PRINT_ELEMENTS(coll); 22 | 23 | // print first and last position 5 could get inserted 24 | pair::iterator,list::iterator> range; 25 | 26 | range = equal_range (coll.begin(), coll.end(), 27 | 5); 28 | 29 | cout << "5 could get position " 30 | << distance(coll.begin(),range.first) + 1 31 | << " up to " 32 | << distance(coll.begin(),range.second) + 1 33 | << " without breaking the sorting" << endl; 34 | } 35 | -------------------------------------------------------------------------------- /algo/equal1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | bool bothEvenOrOdd (int elem1, int elem2) 15 | { 16 | return elem1 % 2 == elem2 % 2; 17 | } 18 | 19 | int main() 20 | { 21 | vector coll1; 22 | list coll2; 23 | 24 | INSERT_ELEMENTS(coll1,1,7); 25 | INSERT_ELEMENTS(coll2,3,9); 26 | 27 | PRINT_ELEMENTS(coll1,"coll1: "); 28 | PRINT_ELEMENTS(coll2,"coll2: "); 29 | 30 | // check whether both collections are equal 31 | if (equal (coll1.begin(), coll1.end(), // first range 32 | coll2.begin())) { // second range 33 | cout << "coll1 == coll2" << endl; 34 | } 35 | else { 36 | cout << "coll1 != coll2" << endl; 37 | } 38 | 39 | // check for corresponding even and odd elements 40 | if (equal (coll1.begin(), coll1.end(), // first range 41 | coll2.begin(), // second range 42 | bothEvenOrOdd)) { // comparison criterion 43 | cout << "even and odd elements correspond" << endl; 44 | } 45 | else { 46 | cout << "even and odd elements do not correspond" << endl; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /algo/find1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | list coll; 17 | 18 | INSERT_ELEMENTS(coll,1,9); 19 | INSERT_ELEMENTS(coll,1,9); 20 | 21 | PRINT_ELEMENTS(coll,"coll: "); 22 | 23 | // find first element with value 4 24 | list::iterator pos1; 25 | pos1 = find (coll.begin(), coll.end(), // range 26 | 4); // value 27 | 28 | /* find second element with value 4 29 | * - note: continue the search behind the first 4 (if any) 30 | */ 31 | list::iterator pos2; 32 | if (pos1 != coll.end()) { 33 | pos2 = find (++pos1, coll.end(), // range 34 | 4); // value 35 | } 36 | 37 | /* print all elements from first to second 4 (both included) 38 | * - note: now we need the position of the first 4 again (if any) 39 | * - note: we have to pass the position behind the second 4 (if any) 40 | */ 41 | if (pos1!=coll.end() && pos2!=coll.end()) { 42 | copy (--pos1, ++pos2, 43 | ostream_iterator(cout," ")); 44 | cout << endl; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /algo/find2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | vector coll; 17 | vector::iterator pos; 18 | 19 | INSERT_ELEMENTS(coll,1,9); 20 | 21 | PRINT_ELEMENTS(coll,"coll: "); 22 | 23 | // find first element greater than 3 24 | pos = find_if (coll.begin(), coll.end(), // range 25 | bind2nd(greater(),3)); // criterion 26 | 27 | // print its position 28 | cout << "the " 29 | << distance(coll.begin(),pos) + 1 30 | << ". element is the first greater than 3" << endl; 31 | 32 | // find first element divisible by 3 33 | pos = find_if (coll.begin(), coll.end(), 34 | not1(bind2nd(modulus(),3))); 35 | 36 | // print its position 37 | cout << "the " 38 | << distance(coll.begin(),pos) + 1 39 | << ". element is the first divisible by 3" << endl; 40 | } 41 | -------------------------------------------------------------------------------- /algo/findend1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | deque coll; 17 | list subcoll; 18 | 19 | INSERT_ELEMENTS(coll,1,7); 20 | INSERT_ELEMENTS(coll,1,7); 21 | 22 | INSERT_ELEMENTS(subcoll,3,6); 23 | 24 | PRINT_ELEMENTS(coll, "coll: "); 25 | PRINT_ELEMENTS(subcoll,"subcoll: "); 26 | 27 | // search last occurrence of subcoll in coll 28 | deque::iterator pos; 29 | pos = find_end (coll.begin(), coll.end(), // range 30 | subcoll.begin(), subcoll.end()); // subrange 31 | 32 | // loop while subcoll found as subrange of coll 33 | deque::iterator end(coll.end()); 34 | while (pos != end) { 35 | // print position of first element 36 | cout << "subcoll found starting with element " 37 | << distance(coll.begin(),pos) + 1 38 | << endl; 39 | 40 | // search next occurrence of subcoll 41 | end = pos; 42 | pos = find_end (coll.begin(), end, // range 43 | subcoll.begin(), subcoll.end()); // subrange 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /algo/foreach1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | // function called for each element 15 | void print (int elem) 16 | { 17 | cout << elem << ' '; 18 | } 19 | 20 | int main() 21 | { 22 | vector coll; 23 | 24 | INSERT_ELEMENTS(coll,1,9); 25 | 26 | // call print() for each element 27 | for_each (coll.begin(), coll.end(), // range 28 | print); // operation 29 | cout << endl; 30 | } 31 | -------------------------------------------------------------------------------- /algo/foreach2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | // function object that adds the value with which it is initialized 15 | template 16 | class AddValue { 17 | private: 18 | T theValue; // value to add 19 | public: 20 | // constructor initializes the value to add 21 | AddValue (const T& v) : theValue(v) { 22 | } 23 | 24 | // the function call for the element adds the value 25 | void operator() (T& elem) const { 26 | elem += theValue; 27 | } 28 | }; 29 | 30 | int main() 31 | { 32 | vector coll; 33 | 34 | INSERT_ELEMENTS(coll,1,9); 35 | 36 | // add ten to each element 37 | for_each (coll.begin(), coll.end(), // range 38 | AddValue(10)); // operation 39 | PRINT_ELEMENTS(coll); 40 | 41 | // add value of first element to each element 42 | for_each (coll.begin(), coll.end(), // range 43 | AddValue(*coll.begin())); // operation 44 | PRINT_ELEMENTS(coll); 45 | } 46 | -------------------------------------------------------------------------------- /algo/foreach3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | // function object to process the mean value 15 | class MeanValue { 16 | private: 17 | long num; // number of elements 18 | long sum; // sum of all element values 19 | public: 20 | // constructor 21 | MeanValue () : num(0), sum(0) { 22 | } 23 | 24 | // function call 25 | // - process one more element of the sequence 26 | void operator() (int elem) { 27 | num++; // increment count 28 | sum += elem; // add value 29 | } 30 | 31 | // return mean value (implicit type conversion) 32 | operator double() { 33 | return static_cast(sum) / static_cast(num); 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | vector coll; 40 | 41 | INSERT_ELEMENTS(coll,1,8); 42 | 43 | // process and print mean value 44 | double mv = for_each (coll.begin(), coll.end(), // range 45 | MeanValue()); // operation 46 | cout << "mean value: " << mv << endl; 47 | } 48 | -------------------------------------------------------------------------------- /algo/generate.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "algostuff.hpp" 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | list coll; 18 | 19 | // insert five random numbers 20 | generate_n (back_inserter(coll), // beginning of destination range 21 | 5, // count 22 | rand); // new value generator 23 | PRINT_ELEMENTS(coll); 24 | 25 | // overwrite with five new random numbers 26 | generate (coll.begin(), coll.end(), // destination range 27 | rand); // new value generator 28 | PRINT_ELEMENTS(coll); 29 | } 30 | -------------------------------------------------------------------------------- /algo/heap1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | vector coll; 17 | 18 | INSERT_ELEMENTS(coll,3,7); 19 | INSERT_ELEMENTS(coll,5,9); 20 | INSERT_ELEMENTS(coll,1,4); 21 | 22 | PRINT_ELEMENTS (coll, "on entry: "); 23 | 24 | // convert collection into a heap 25 | make_heap (coll.begin(), coll.end()); 26 | 27 | PRINT_ELEMENTS (coll, "after make_heap(): "); 28 | 29 | // pop next element out of the heap 30 | pop_heap (coll.begin(), coll.end()); 31 | coll.pop_back(); 32 | 33 | PRINT_ELEMENTS (coll, "after pop_heap(): "); 34 | 35 | // push new element into the heap 36 | coll.push_back (17); 37 | push_heap (coll.begin(), coll.end()); 38 | 39 | PRINT_ELEMENTS (coll, "after push_heap(): "); 40 | 41 | /* convert heap into a sorted collection 42 | * - NOTE: after the call it is no longer a heap 43 | */ 44 | sort_heap (coll.begin(), coll.end()); 45 | 46 | PRINT_ELEMENTS (coll, "after sort_heap(): "); 47 | } 48 | -------------------------------------------------------------------------------- /algo/imerge1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | list coll; 17 | 18 | // insert two sorted sequences 19 | INSERT_ELEMENTS(coll,1,7); 20 | INSERT_ELEMENTS(coll,1,8); 21 | PRINT_ELEMENTS(coll); 22 | 23 | // find beginning of second part (element after 7) 24 | list::iterator pos; 25 | pos = find (coll.begin(), coll.end(), // range 26 | 7); // value 27 | ++pos; 28 | 29 | // merge into one sorted range 30 | inplace_merge (coll.begin(), pos, coll.end()); 31 | 32 | PRINT_ELEMENTS(coll); 33 | } 34 | -------------------------------------------------------------------------------- /algo/includes.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | list coll; 17 | vector search; 18 | 19 | INSERT_ELEMENTS(coll,1,9); 20 | PRINT_ELEMENTS(coll,"coll: "); 21 | 22 | search.push_back(3); 23 | search.push_back(4); 24 | search.push_back(7); 25 | PRINT_ELEMENTS(search,"search: "); 26 | 27 | // check whether all elements in search are also in coll 28 | if (includes (coll.begin(), coll.end(), 29 | search.begin(), search.end())) { 30 | cout << "all elements of search are also in coll" 31 | << endl; 32 | } 33 | else { 34 | cout << "not all elements of search are also in coll" 35 | << endl; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /algo/merge1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | list coll1; 17 | set coll2; 18 | 19 | // fill both collections with some sorted elements 20 | INSERT_ELEMENTS(coll1,1,6); 21 | INSERT_ELEMENTS(coll2,3,8); 22 | 23 | PRINT_ELEMENTS(coll1,"coll1: "); 24 | PRINT_ELEMENTS(coll2,"coll2: "); 25 | 26 | // print merged sequence 27 | cout << "merged: "; 28 | merge (coll1.begin(), coll1.end(), 29 | coll2.begin(), coll2.end(), 30 | ostream_iterator(cout," ")); 31 | cout << endl; 32 | } 33 | -------------------------------------------------------------------------------- /algo/minmax1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "algostuff.hpp" 13 | using namespace std; 14 | 15 | bool absLess (int elem1, int elem2) 16 | { 17 | return abs(elem1) < abs(elem2); 18 | } 19 | 20 | int main() 21 | { 22 | deque coll; 23 | 24 | INSERT_ELEMENTS(coll,2,8); 25 | INSERT_ELEMENTS(coll,-3,5); 26 | 27 | PRINT_ELEMENTS(coll); 28 | 29 | // process and print minimum and maximum 30 | cout << "minimum: " 31 | << *min_element(coll.begin(),coll.end()) 32 | << endl; 33 | cout << "maximum: " 34 | << *max_element(coll.begin(),coll.end()) 35 | << endl; 36 | 37 | // process and print minimum and maximum of absolute values 38 | cout << "minimum of absolute values: " 39 | << *min_element(coll.begin(),coll.end(), 40 | absLess) 41 | << endl; 42 | cout << "maximum of absolute values: " 43 | << *max_element(coll.begin(),coll.end(), 44 | absLess) 45 | << endl; 46 | } 47 | -------------------------------------------------------------------------------- /algo/part1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | vector coll1; 17 | vector coll2; 18 | 19 | INSERT_ELEMENTS(coll1,1,9); 20 | INSERT_ELEMENTS(coll2,1,9); 21 | PRINT_ELEMENTS(coll1,"coll1: "); 22 | PRINT_ELEMENTS(coll2,"coll2: "); 23 | cout << endl; 24 | 25 | // move all even elements to the front 26 | vector::iterator pos1, pos2; 27 | pos1 = partition(coll1.begin(), coll1.end(), // range 28 | not1(bind2nd(modulus(),2))); // criterion 29 | pos2 = stable_partition(coll2.begin(), coll2.end(), // range 30 | not1(bind2nd(modulus(),2))); // crit. 31 | 32 | // print collections and first odd element 33 | PRINT_ELEMENTS(coll1,"coll1: "); 34 | cout << "first odd element: " << *pos1 << endl; 35 | PRINT_ELEMENTS(coll2,"coll2: "); 36 | cout << "first odd element: " << *pos2 << endl; 37 | } 38 | -------------------------------------------------------------------------------- /algo/partsum1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | vector coll; 17 | 18 | INSERT_ELEMENTS(coll,1,6); 19 | PRINT_ELEMENTS(coll); 20 | 21 | // print all partial sums 22 | partial_sum (coll.begin(), coll.end(), // source range 23 | ostream_iterator(cout," ")); // destination 24 | cout << endl; 25 | 26 | // print all partial products 27 | partial_sum (coll.begin(), coll.end(), // source range 28 | ostream_iterator(cout," "), // destination 29 | multiplies()); // operation 30 | cout << endl; 31 | } 32 | -------------------------------------------------------------------------------- /algo/perm1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | vector coll; 17 | INSERT_ELEMENTS(coll,1,3); 18 | PRINT_ELEMENTS(coll,"on entry: "); 19 | 20 | /* permute elements until they are sorted 21 | * - runs through all permutations because the elements are sorted now 22 | */ 23 | while (next_permutation(coll.begin(),coll.end())) { 24 | PRINT_ELEMENTS(coll," "); 25 | } 26 | PRINT_ELEMENTS(coll,"afterward: "); 27 | 28 | /* permute until descending sorted 29 | * - this is the next permutation after ascending sorting 30 | * - so the loop ends immediately 31 | */ 32 | while (prev_permutation(coll.begin(),coll.end())) { 33 | PRINT_ELEMENTS(coll," "); 34 | } 35 | PRINT_ELEMENTS(coll,"now: "); 36 | 37 | /* permute elements until they are sorted in descending order 38 | * - runs through all permutations because the elements are sorted 39 | * in descending order now 40 | */ 41 | while (prev_permutation(coll.begin(),coll.end())) { 42 | PRINT_ELEMENTS(coll," "); 43 | } 44 | PRINT_ELEMENTS(coll,"afterward: "); 45 | } 46 | -------------------------------------------------------------------------------- /algo/psort1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | deque coll; 17 | 18 | INSERT_ELEMENTS(coll,3,7); 19 | INSERT_ELEMENTS(coll,2,6); 20 | INSERT_ELEMENTS(coll,1,5); 21 | PRINT_ELEMENTS(coll); 22 | 23 | // sort until the first five elements are sorted 24 | partial_sort (coll.begin(), // beginning of the range 25 | coll.begin()+5, // end of sorted range 26 | coll.end()); // end of full range 27 | PRINT_ELEMENTS(coll); 28 | 29 | // sort inversely until the first five elements are sorted 30 | partial_sort (coll.begin(), // beginning of the range 31 | coll.begin()+5, // end of sorted range 32 | coll.end(), // end of full range 33 | greater()); // sorting criterion 34 | PRINT_ELEMENTS(coll); 35 | 36 | // sort all elements 37 | partial_sort (coll.begin(), // beginning of the range 38 | coll.end(), // end of sorted range 39 | coll.end()); // end of full range 40 | PRINT_ELEMENTS(coll); 41 | } 42 | -------------------------------------------------------------------------------- /algo/psort2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | deque coll1; 17 | vector coll6(6); // initialize with 6 elements 18 | vector coll30(30); // initialize with 30 elements 19 | 20 | INSERT_ELEMENTS(coll1,3,7); 21 | INSERT_ELEMENTS(coll1,2,6); 22 | INSERT_ELEMENTS(coll1,1,5); 23 | PRINT_ELEMENTS(coll1); 24 | 25 | // copy elements of coll1 sorted into coll6 26 | vector::iterator pos6; 27 | pos6 = partial_sort_copy (coll1.begin(), coll1.end(), 28 | coll6.begin(), coll6.end()); 29 | 30 | // print all copied elements 31 | copy (coll6.begin(), pos6, 32 | ostream_iterator(cout," ")); 33 | cout << endl; 34 | 35 | // copy elements of coll1 sorted into coll30 36 | vector::iterator pos30; 37 | pos30 = partial_sort_copy (coll1.begin(), coll1.end(), 38 | coll30.begin(), coll30.end(), 39 | greater()); 40 | 41 | // print all copied elements 42 | copy (coll30.begin(), pos30, 43 | ostream_iterator(cout," ")); 44 | cout << endl; 45 | } 46 | -------------------------------------------------------------------------------- /algo/random1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "algostuff.hpp" 13 | using namespace std; 14 | 15 | class MyRandom { 16 | public: 17 | ptrdiff_t operator() (ptrdiff_t max) { 18 | double tmp; 19 | tmp = static_cast(rand()) 20 | / static_cast(RAND_MAX); 21 | return static_cast(tmp * max); 22 | } 23 | }; 24 | 25 | int main() 26 | { 27 | vector coll; 28 | 29 | INSERT_ELEMENTS(coll,1,9); 30 | PRINT_ELEMENTS(coll,"coll: "); 31 | 32 | // shuffle all elements randomly 33 | random_shuffle (coll.begin(), coll.end()); 34 | 35 | PRINT_ELEMENTS(coll,"shuffled: "); 36 | 37 | // sort them again 38 | sort (coll.begin(), coll.end()); 39 | PRINT_ELEMENTS(coll,"sorted: "); 40 | 41 | /* shuffle elements with self-written random number generator 42 | * - to pass an lvalue we have to use a temporary object 43 | */ 44 | MyRandom rd; 45 | random_shuffle (coll.begin(), coll.end(), // range 46 | rd); // random number generator 47 | 48 | PRINT_ELEMENTS(coll,"shuffled: "); 49 | } 50 | -------------------------------------------------------------------------------- /algo/relabs.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | vector coll; 17 | 18 | coll.push_back(17); 19 | coll.push_back(-3); 20 | coll.push_back(22); 21 | coll.push_back(13); 22 | coll.push_back(13); 23 | coll.push_back(-9); 24 | PRINT_ELEMENTS(coll,"coll: "); 25 | 26 | // convert into relative values 27 | adjacent_difference (coll.begin(), coll.end(), // source 28 | coll.begin()); // destination 29 | PRINT_ELEMENTS(coll,"relative: "); 30 | 31 | // convert into absolute values 32 | partial_sum (coll.begin(), coll.end(), // source 33 | coll.begin()); // destination 34 | PRINT_ELEMENTS(coll,"absolute: "); 35 | } 36 | -------------------------------------------------------------------------------- /algo/remove1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | vector coll; 17 | 18 | INSERT_ELEMENTS(coll,2,6); 19 | INSERT_ELEMENTS(coll,4,9); 20 | INSERT_ELEMENTS(coll,1,7); 21 | PRINT_ELEMENTS(coll,"coll: "); 22 | 23 | // remove all elements with value 5 24 | vector::iterator pos; 25 | pos = remove(coll.begin(), coll.end(), // range 26 | 5); // value to remove 27 | 28 | PRINT_ELEMENTS(coll,"size not changed: "); 29 | 30 | // erase the ``removed'' elements in the container 31 | coll.erase(pos, coll.end()); 32 | PRINT_ELEMENTS(coll,"size changed: "); 33 | 34 | // remove all elements less than 4 35 | coll.erase(remove_if(coll.begin(), coll.end(), // range 36 | bind2nd(less(),4)), // remove criterion 37 | coll.end()); 38 | PRINT_ELEMENTS(coll,"<4 removed: "); 39 | } 40 | -------------------------------------------------------------------------------- /algo/remove2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | list coll1; 17 | 18 | INSERT_ELEMENTS(coll1,1,6); 19 | INSERT_ELEMENTS(coll1,1,9); 20 | PRINT_ELEMENTS(coll1); 21 | 22 | // print elements without those having the value 3 23 | remove_copy(coll1.begin(), coll1.end(), // source 24 | ostream_iterator(cout," "), // destination 25 | 3); // removed value 26 | cout << endl; 27 | 28 | // print elements without those having a value greater than 4 29 | remove_copy_if(coll1.begin(), coll1.end(), // source 30 | ostream_iterator(cout," "), // destination 31 | bind2nd(greater(),4)); // removed elements 32 | cout << endl; 33 | 34 | // copy all elements greater than 3 into a multiset 35 | multiset coll2; 36 | remove_copy_if(coll1.begin(), coll1.end(), // source 37 | inserter(coll2,coll2.end()), // destination 38 | bind2nd(less(),4)); // elements not copied 39 | PRINT_ELEMENTS(coll2); 40 | } 41 | -------------------------------------------------------------------------------- /algo/replace1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | list coll; 17 | 18 | INSERT_ELEMENTS(coll,2,7); 19 | INSERT_ELEMENTS(coll,4,9); 20 | PRINT_ELEMENTS(coll,"coll: "); 21 | 22 | // replace all elements with value 6 with 42 23 | replace (coll.begin(), coll.end(), // range 24 | 6, // old value 25 | 42); // new value 26 | PRINT_ELEMENTS(coll,"coll: "); 27 | 28 | // replace all elements with value less than 5 with 0 29 | replace_if (coll.begin(), coll.end(), // range 30 | bind2nd(less(),5), // criterion for replacement 31 | 0); // new value 32 | PRINT_ELEMENTS(coll,"coll: "); 33 | } 34 | -------------------------------------------------------------------------------- /algo/reverse1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | vector coll; 17 | 18 | INSERT_ELEMENTS(coll,1,9); 19 | PRINT_ELEMENTS(coll,"coll: "); 20 | 21 | // reverse order of elements 22 | reverse (coll.begin(), coll.end()); 23 | PRINT_ELEMENTS(coll,"coll: "); 24 | 25 | // reverse order from second to last element but one 26 | reverse (coll.begin()+1, coll.end()-1); 27 | PRINT_ELEMENTS(coll,"coll: "); 28 | 29 | // print all of them in reverse order 30 | reverse_copy (coll.begin(), coll.end(), // source 31 | ostream_iterator(cout," ")); // destination 32 | cout << endl; 33 | } 34 | -------------------------------------------------------------------------------- /algo/rotate1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | vector coll; 17 | 18 | INSERT_ELEMENTS(coll,1,9); 19 | PRINT_ELEMENTS(coll,"coll: "); 20 | 21 | // rotate one element to the left 22 | rotate (coll.begin(), // beginning of range 23 | coll.begin() + 1, // new first element 24 | coll.end()); // end of range 25 | PRINT_ELEMENTS(coll,"one left: "); 26 | 27 | // rotate two elements to the right 28 | rotate (coll.begin(), // beginning of range 29 | coll.end() - 2, // new first element 30 | coll.end()); // end of range 31 | PRINT_ELEMENTS(coll,"two right: "); 32 | 33 | // rotate so that element with value 4 is the beginning 34 | rotate (coll.begin(), // beginning of range 35 | find(coll.begin(),coll.end(),4), // new first element 36 | coll.end()); // end of range 37 | PRINT_ELEMENTS(coll,"4 first: "); 38 | } 39 | -------------------------------------------------------------------------------- /algo/search1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | deque coll; 17 | list subcoll; 18 | 19 | INSERT_ELEMENTS(coll,1,7); 20 | INSERT_ELEMENTS(coll,1,7); 21 | 22 | INSERT_ELEMENTS(subcoll,3,6); 23 | 24 | PRINT_ELEMENTS(coll, "coll: "); 25 | PRINT_ELEMENTS(subcoll,"subcoll: "); 26 | 27 | // search first occurrence of subcoll in coll 28 | deque::iterator pos; 29 | pos = search (coll.begin(), coll.end(), // range 30 | subcoll.begin(), subcoll.end()); // subrange 31 | 32 | // loop while subcoll found as subrange of coll 33 | while (pos != coll.end()) { 34 | // print position of first element 35 | cout << "subcoll found starting with element " 36 | << distance(coll.begin(),pos) + 1 37 | << endl; 38 | 39 | // search next occurrence of subcoll 40 | ++pos; 41 | pos = search (pos, coll.end(), // range 42 | subcoll.begin(), subcoll.end()); // subrange 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /algo/sort1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | deque coll; 17 | 18 | INSERT_ELEMENTS(coll,1,9); 19 | INSERT_ELEMENTS(coll,1,9); 20 | 21 | PRINT_ELEMENTS(coll,"on entry: "); 22 | 23 | // sort elements 24 | sort (coll.begin(), coll.end()); 25 | 26 | PRINT_ELEMENTS(coll,"sorted: "); 27 | 28 | // sorted reverse 29 | sort (coll.begin(), coll.end(), // range 30 | greater()); // sorting criterion 31 | 32 | PRINT_ELEMENTS(coll,"sorted >: "); 33 | } 34 | -------------------------------------------------------------------------------- /algo/swap1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | vector coll1; 17 | deque coll2; 18 | 19 | INSERT_ELEMENTS(coll1,1,9); 20 | INSERT_ELEMENTS(coll2,11,23); 21 | 22 | PRINT_ELEMENTS(coll1,"coll1: "); 23 | PRINT_ELEMENTS(coll2,"coll2: "); 24 | 25 | // swap elements of coll1 with corresponding elements of coll2 26 | deque::iterator pos; 27 | pos = swap_ranges (coll1.begin(), coll1.end(), // first range 28 | coll2.begin()); // second range 29 | 30 | PRINT_ELEMENTS(coll1,"\ncoll1: "); 31 | PRINT_ELEMENTS(coll2,"coll2: "); 32 | if (pos != coll2.end()) { 33 | cout << "first element not modified: " 34 | << *pos << endl; 35 | } 36 | 37 | // mirror first three with last three elements in coll2 38 | swap_ranges (coll2.begin(), coll2.begin()+3, // first range 39 | coll2.rbegin()); // second range 40 | 41 | PRINT_ELEMENTS(coll2,"\ncoll2: "); 42 | } 43 | -------------------------------------------------------------------------------- /algo/transf1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | vector coll1; 17 | list coll2; 18 | 19 | INSERT_ELEMENTS(coll1,1,9); 20 | PRINT_ELEMENTS(coll1,"coll1: "); 21 | 22 | // negate all elements in coll1 23 | transform (coll1.begin(), coll1.end(), // source range 24 | coll1.begin(), // destination range 25 | negate()); // operation 26 | PRINT_ELEMENTS(coll1,"negated: "); 27 | 28 | // transform elements of coll1 into coll2 with ten times their value 29 | transform (coll1.begin(), coll1.end(), // source range 30 | back_inserter(coll2), // destination range 31 | bind2nd(multiplies(),10)); // operation 32 | PRINT_ELEMENTS(coll2,"coll2: "); 33 | 34 | // print coll2 negatively and in reverse order 35 | transform (coll2.rbegin(), coll2.rend(), // source range 36 | ostream_iterator(cout," "), // destination range 37 | negate()); // operation 38 | cout << endl; 39 | } 40 | -------------------------------------------------------------------------------- /algo/unique2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include "algostuff.hpp" 12 | using namespace std; 13 | 14 | bool differenceOne (int elem1, int elem2) 15 | { 16 | return elem1 + 1 == elem2 || elem1 - 1 == elem2; 17 | } 18 | 19 | int main() 20 | { 21 | // source data 22 | int source[] = { 1, 4, 4, 6, 1, 2, 2, 3, 1, 6, 6, 6, 5, 7, 23 | 5, 4, 4 }; 24 | int sourceNum = sizeof(source)/sizeof(source[0]); 25 | 26 | // initialize coll with elements from source 27 | list coll; 28 | copy(source, source+sourceNum, // source 29 | back_inserter(coll)); // destination 30 | PRINT_ELEMENTS(coll); 31 | 32 | // print elements with consecutive duplicates removed 33 | unique_copy(coll.begin(), coll.end(), // source 34 | ostream_iterator(cout," ")); // destination 35 | cout << endl; 36 | 37 | // print elements without consecutive entries that differ by one 38 | unique_copy(coll.begin(), coll.end(), // source 39 | ostream_iterator(cout," "), // destination 40 | differenceOne); // duplicates criterion 41 | cout << endl; 42 | } 43 | -------------------------------------------------------------------------------- /algo/unique3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | bool bothSpaces (char elem1, char elem2) 17 | { 18 | return elem1 == ' ' && elem2 == ' '; 19 | } 20 | 21 | int main() 22 | { 23 | // don't skip leading whitespaces by default 24 | cin.unsetf(ios::skipws); 25 | 26 | /* copy standard input to standard output 27 | * - while compressing spaces 28 | */ 29 | unique_copy(istream_iterator(cin), // beginning of source: cin 30 | istream_iterator(), // end of source: end-of-file 31 | ostream_iterator(cout), // destination: cout 32 | bothSpaces); // duplicate criterion 33 | } 34 | -------------------------------------------------------------------------------- /cont/Makefile: -------------------------------------------------------------------------------- 1 | OUTPROGS = vector1 deque1 list1 \ 2 | set2 set1 mset1 setcmp \ 3 | map1 mmap1 mapfind mapcmp \ 4 | array1 carray1 refsem1 \ 5 | stack1 stack2 queue1 queue2 pqueue1 \ 6 | bitset2 7 | 8 | CPPPROGS = sortset sortvec bitset1 9 | 10 | HEADERS = newkey.hpp carray.hpp countptr.hpp Stack.hpp Queue.hpp print.hpp 11 | 12 | include ../Makefile.h 13 | 14 | 15 | refsem1: countptr.hpp refsem1.cpp 16 | $(CXX) $(CXXFLAGS) $(LDFLAGS) $@.cpp -o $@ 17 | 18 | carray1: carray.hpp carray1.cpp 19 | $(CXX) $(CXXFLAGS) $(LDFLAGS) $@.cpp -o $@ 20 | 21 | stack2: Stack.hpp stack2.cpp 22 | $(CXX) $(CXXFLAGS) $(LDFLAGS) $@.cpp -o $@ 23 | 24 | queue2: Queue.hpp queue2.cpp 25 | $(CXX) $(CXXFLAGS) $(LDFLAGS) $@.cpp -o $@ 26 | 27 | -------------------------------------------------------------------------------- /cont/array1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | int coll[] = { 5, 6, 2, 4, 1, 3 }; 20 | 21 | // square all elements 22 | transform (coll, coll+6, // first source 23 | coll, // second source 24 | coll, // destination 25 | multiplies()); // operation 26 | 27 | // sort beginning with the second element 28 | sort (coll+1, coll+6); 29 | 30 | // print all elements 31 | copy (coll, coll+6, 32 | ostream_iterator(cout," ")); 33 | cout << endl; 34 | } 35 | -------------------------------------------------------------------------------- /cont/bitset1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | /* enumeration type for the bits 18 | * - each bit represents a color 19 | */ 20 | enum Color { red, yellow, green, blue, white, black, //..., 21 | numColors }; 22 | 23 | // create bitset for all bits/colors 24 | bitset usedColors; 25 | 26 | // set bits for two colors 27 | usedColors.set(red); 28 | usedColors.set(blue); 29 | 30 | // print some bitset data 31 | cout << "bitfield of used colors: " << usedColors 32 | << endl; 33 | cout << "number of used colors: " << usedColors.count() 34 | << endl; 35 | cout << "bitfield of unused colors: " << ~usedColors 36 | << endl; 37 | 38 | // if any color is used 39 | if (usedColors.any()) { 40 | // loop over all colors 41 | for (int c = 0; c < numColors; ++c) { 42 | // if the actual color is used 43 | if (usedColors[(Color)c]) { 44 | //... 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /cont/bitset2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | /* print some numbers in binary representation 20 | */ 21 | cout << "267 as binary short: " 22 | << bitset::digits>(267) 23 | << endl; 24 | 25 | cout << "267 as binary long: " 26 | << bitset::digits>(267) 27 | << endl; 28 | 29 | cout << "10,000,000 with 24 bits: " 30 | << bitset<24>(1e7) << endl; 31 | 32 | /* transform binary representation into integral number 33 | */ 34 | cout << "\"1000101011\" as number: " 35 | << bitset<100>(string("1000101011")).to_ulong() << endl; 36 | } 37 | -------------------------------------------------------------------------------- /cont/carray.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | template 14 | class carray { 15 | private: 16 | T v[thesize]; // fixed-size array of elements of type T 17 | 18 | public: 19 | // type definitions 20 | typedef T value_type; 21 | typedef T* iterator; 22 | typedef const T* const_iterator; 23 | typedef T& reference; 24 | typedef const T& const_reference; 25 | typedef std::size_t size_type; 26 | typedef std::ptrdiff_t difference_type; 27 | 28 | // iterator support 29 | iterator begin() { return v; } 30 | const_iterator begin() const { return v; } 31 | iterator end() { return v+thesize; } 32 | const_iterator end() const { return v+thesize; } 33 | 34 | // direct element access 35 | reference operator[](std::size_t i) { return v[i]; } 36 | const_reference operator[](std::size_t i) const { return v[i]; } 37 | 38 | // size is constant 39 | size_type size() const { return thesize; } 40 | size_type max_size() const { return thesize; } 41 | 42 | // conversion to ordinary array 43 | T* as_array() { return v; } 44 | }; 45 | 46 | -------------------------------------------------------------------------------- /cont/carray1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include "carray.hpp" 14 | #include "print.hpp" 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | carray a; 20 | 21 | for (unsigned i=0; i()); // operation 32 | PRINT_ELEMENTS(a); 33 | } 34 | 35 | -------------------------------------------------------------------------------- /cont/deque1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | // create empty deque of strings 21 | deque coll; 22 | 23 | // insert several elements 24 | coll.assign (3, string("string")); 25 | coll.push_back ("last string"); 26 | coll.push_front ("first string"); 27 | 28 | // print elements separated by newlines 29 | copy (coll.begin(), coll.end(), 30 | ostream_iterator(cout,"\n")); 31 | cout << endl; 32 | 33 | // remove first and last element 34 | coll.pop_front(); 35 | coll.pop_back(); 36 | 37 | // insert ``another'' into every element but the first 38 | for (unsigned i=1; i(cout,"\n")); 48 | } 49 | -------------------------------------------------------------------------------- /cont/newkey.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | namespace MyLib { 12 | template 13 | inline 14 | bool replace_key (Cont& c, 15 | const typename Cont::key_type& old_key, 16 | const typename Cont::key_type& new_key) 17 | { 18 | typename Cont::iterator pos; 19 | pos = c.find(old_key); 20 | if (pos != c.end()) { 21 | // insert new element with value of old element 22 | c.insert(typename Cont::value_type(new_key, 23 | pos->second)); 24 | // remove old element 25 | c.erase(pos); 26 | return true; 27 | } 28 | else { 29 | // key not found 30 | return false; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /cont/pqueue1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | priority_queue q; 18 | 19 | // insert three elements into the priority queue 20 | q.push(66.6); 21 | q.push(22.2); 22 | q.push(44.4); 23 | 24 | // read and print two elements 25 | cout << q.top() << ' '; 26 | q.pop(); 27 | cout << q.top() << endl; 28 | q.pop(); 29 | 30 | // insert three more elements 31 | q.push(11.1); 32 | q.push(55.5); 33 | q.push(33.3); 34 | 35 | // skip one element 36 | q.pop(); 37 | 38 | // pop and print remaining elements 39 | while (!q.empty()) { 40 | cout << q.top() << ' '; 41 | q.pop(); 42 | } 43 | cout << endl; 44 | } 45 | -------------------------------------------------------------------------------- /cont/print.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | /* PRINT_ELEMENTS() 14 | * - prints optional C-string optcstr followed by 15 | * - all elements of the collection coll 16 | * - separated by spaces 17 | */ 18 | template 19 | inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") 20 | { 21 | typename T::const_iterator pos; 22 | 23 | std::cout << optcstr; 24 | for (pos=coll.begin(); pos!=coll.end(); ++pos) { 25 | std::cout << *pos << ' '; 26 | } 27 | std::cout << std::endl; 28 | } 29 | -------------------------------------------------------------------------------- /cont/queue1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | queue q; 19 | 20 | // insert three elements into the queue 21 | q.push("These "); 22 | q.push("are "); 23 | q.push("more than "); 24 | 25 | // read and print two elements from the queue 26 | cout << q.front(); 27 | q.pop(); 28 | cout << q.front(); 29 | q.pop(); 30 | 31 | // insert two new elements 32 | q.push("four "); 33 | q.push("words!"); 34 | 35 | // skip one element 36 | q.pop(); 37 | 38 | // read and print two elements 39 | cout << q.front(); 40 | q.pop(); 41 | cout << q.front() << endl; 42 | q.pop(); 43 | 44 | // print number of elements in the queue 45 | cout << "number of elements in the queue: " << q.size() 46 | << endl; 47 | } 48 | -------------------------------------------------------------------------------- /cont/queue2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include "Queue.hpp" // use special queue class 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | try { 19 | Queue q; 20 | 21 | // insert three elements into the queue 22 | q.push("These "); 23 | q.push("are "); 24 | q.push("more than "); 25 | 26 | // read and print two elements from the queue 27 | cout << q.pop(); 28 | cout << q.pop(); 29 | 30 | // push two new elements 31 | q.push("four "); 32 | q.push("words!"); 33 | 34 | // skip one element 35 | q.pop(); 36 | 37 | // read and print two elements from the queue 38 | cout << q.pop(); 39 | cout << q.pop() << endl; 40 | 41 | // print number of remaining elements 42 | cout << "number of elements in the queue: " << q.size() 43 | << endl; 44 | 45 | // read and print one element 46 | cout << q.pop() << endl; 47 | } 48 | catch (const exception& e) { 49 | cerr << "EXCEPTION: " << e.what() << endl; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /cont/set2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main () 16 | { 17 | set c; 18 | 19 | c.insert(1); 20 | c.insert(2); 21 | c.insert(4); 22 | c.insert(5); 23 | c.insert(6); 24 | 25 | cout << "lower_bound(3): " << *c.lower_bound(3) << endl; 26 | cout << "upper_bound(3): " << *c.upper_bound(3) << endl; 27 | cout << "equal_range(3): " << *c.equal_range(3).first << " " 28 | << *c.equal_range(3).second << endl; 29 | cout << endl; 30 | cout << "lower_bound(5): " << *c.lower_bound(5) << endl; 31 | cout << "upper_bound(5): " << *c.upper_bound(5) << endl; 32 | cout << "equal_range(5): " << *c.equal_range(5).first << " " 33 | << *c.equal_range(5).second << endl; 34 | } 35 | -------------------------------------------------------------------------------- /cont/sortset.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | /* create a string set 21 | * - initialized by all words from standard input 22 | */ 23 | set coll((istream_iterator(cin)), 24 | istream_iterator()); 25 | 26 | // print all elements 27 | copy (coll.begin(), coll.end(), 28 | ostream_iterator(cout, "\n")); 29 | } 30 | -------------------------------------------------------------------------------- /cont/sortvec.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | /* create a string vector 21 | * - initialized by all words from standard input 22 | */ 23 | vector coll((istream_iterator(cin)), 24 | istream_iterator()); 25 | 26 | // sort elements 27 | sort (coll.begin(), coll.end()); 28 | 29 | // print all elements ignoring subsequent duplicates 30 | unique_copy (coll.begin(), coll.end(), 31 | ostream_iterator(cout, "\n")); 32 | } 33 | -------------------------------------------------------------------------------- /cont/stack1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | stack st; 18 | 19 | // push three elements into the stack 20 | st.push(1); 21 | st.push(2); 22 | st.push(3); 23 | 24 | // pop and print two elements from the stack 25 | cout << st.top() << ' '; 26 | st.pop(); 27 | cout << st.top() << ' '; 28 | st.pop(); 29 | 30 | // modify top element 31 | st.top() = 77; 32 | 33 | // push two new elements 34 | st.push(4); 35 | st.push(5); 36 | 37 | // pop one element without processing it 38 | st.pop(); 39 | 40 | // pop and print remaining elements 41 | while (!st.empty()) { 42 | cout << st.top() << ' '; 43 | st.pop(); 44 | } 45 | cout << endl; 46 | } 47 | -------------------------------------------------------------------------------- /cont/stack2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "Stack.hpp" // use special stack class 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | try { 18 | Stack st; 19 | 20 | // push three elements into the stack 21 | st.push(1); 22 | st.push(2); 23 | st.push(3); 24 | 25 | // pop and print two elements from the stack 26 | cout << st.pop() << ' '; 27 | cout << st.pop() << ' '; 28 | 29 | // modify top element 30 | st.top() = 77; 31 | 32 | // push two new elements 33 | st.push(4); 34 | st.push(5); 35 | 36 | // pop one element without processing it 37 | st.pop(); 38 | 39 | /* pop and print three elements 40 | * - ERROR: one element too many 41 | */ 42 | cout << st.pop() << ' '; 43 | cout << st.pop() << endl; 44 | cout << st.pop() << endl; 45 | } 46 | catch (const exception& e) { 47 | cerr << "EXCEPTION: " << e.what() << endl; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /fo/Makefile: -------------------------------------------------------------------------------- 1 | include ../Makefile.h 2 | 3 | OUTPROGS = sort1 genera1 genera2 foreach3 removeif \ 4 | fopow1 \ 5 | compose1 compose2 compose3 compose4 6 | 7 | CPPPROGS = memfun1 8 | 9 | HEADERS = fopow.hpp compose11.hpp compose12.hpp compose21.hpp \ 10 | compose22.hpp compose10.hpp nullary.hpp print.hpp 11 | 12 | 13 | fopow1: fopow.hpp fopow1.cpp 14 | $(CXX) $(CXXFLAGS) $(LDFLAGS) $@.cpp -o $@ 15 | 16 | compose1: print.hpp compose11.hpp compose1.cpp 17 | $(CXX) $(CXXFLAGS) $(LDFLAGS) $@.cpp -o $@ 18 | 19 | compose2: compose21.hpp print.hpp compose2.cpp 20 | $(CXX) $(CXXFLAGS) $(LDFLAGS) $@.cpp -o $@ 21 | 22 | compose3: compose3.cpp compose22.hpp 23 | $(CXX) $(CXXFLAGS) $(LDFLAGS) $@.cpp -o $@ 24 | 25 | -------------------------------------------------------------------------------- /fo/compose1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "print.hpp" 17 | #include "compose11.hpp" 18 | using namespace std; 19 | 20 | int main() 21 | { 22 | vector coll; 23 | 24 | // insert elements from 1 to 9 25 | for (int i=1; i<=9; ++i) { 26 | coll.push_back(i); 27 | } 28 | PRINT_ELEMENTS(coll); 29 | 30 | // for each element add 10 and multiply by 5 31 | transform (coll.begin(),coll.end(), 32 | ostream_iterator(cout," "), 33 | compose_f_gx(bind2nd(multiplies(),5), 34 | bind2nd(plus(),10))); 35 | cout << endl; 36 | } 37 | -------------------------------------------------------------------------------- /fo/compose10.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "nullary.hpp" 13 | 14 | /* class for the compose_f_g adapter 15 | */ 16 | template 17 | class compose_f_g_t 18 | : public boost::nullary_function 19 | { 20 | private: 21 | OP1 op1; // process: op1(op2()) 22 | OP2 op2; 23 | public: 24 | // constructor 25 | compose_f_g_t(const OP1& o1, const OP2& o2) 26 | : op1(o1), op2(o2) { 27 | } 28 | 29 | // function call 30 | typename OP1::result_type 31 | operator()() const { 32 | return op1(op2()); 33 | } 34 | }; 35 | 36 | /* convenience function for the compose_f_g adapter 37 | */ 38 | template 39 | inline compose_f_g_t 40 | compose_f_g (const OP1& o1, const OP2& o2) { 41 | return compose_f_g_t(o1,o2); 42 | } 43 | -------------------------------------------------------------------------------- /fo/compose11.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | /* class for the compose_f_gx adapter 14 | */ 15 | template 16 | class compose_f_gx_t 17 | : public std::unary_function 19 | { 20 | private: 21 | OP1 op1; // process: op1(op2(x)) 22 | OP2 op2; 23 | public: 24 | // constructor 25 | compose_f_gx_t(const OP1& o1, const OP2& o2) 26 | : op1(o1), op2(o2) { 27 | } 28 | 29 | // function call 30 | typename OP1::result_type 31 | operator()(const typename OP2::argument_type& x) const { 32 | return op1(op2(x)); 33 | } 34 | }; 35 | 36 | /* convenience function for the compose_f_gx adapter 37 | */ 38 | template 39 | inline compose_f_gx_t 40 | compose_f_gx (const OP1& o1, const OP2& o2) { 41 | return compose_f_gx_t(o1,o2); 42 | } 43 | -------------------------------------------------------------------------------- /fo/compose12.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | /* class for the compose_f_gxy adapter 14 | */ 15 | template 16 | class compose_f_gxy_t 17 | : public std::binary_function 20 | { 21 | private: 22 | OP1 op1; // process: op1(op2(x,y)) 23 | OP2 op2; 24 | public: 25 | // constructor 26 | compose_f_gxy_t (const OP1& o1, const OP2& o2) 27 | : op1(o1), op2(o2) { 28 | } 29 | 30 | // function call 31 | typename OP1::result_type 32 | operator()(const typename OP2::first_argument_type& x, 33 | const typename OP2::second_argument_type& y) const { 34 | return op1(op2(x,y)); 35 | } 36 | }; 37 | 38 | /* convenience function for the compose_f_gxy adapter 39 | */ 40 | template 41 | inline compose_f_gxy_t 42 | compose_f_gxy (const OP1& o1, const OP2& o2) { 43 | return compose_f_gxy_t(o1,o2); 44 | } 45 | -------------------------------------------------------------------------------- /fo/compose2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "print.hpp" 16 | #include "compose21.hpp" 17 | using namespace std; 18 | 19 | int main() 20 | { 21 | vector coll; 22 | 23 | // insert elements from 1 to 9 24 | for (int i=1; i<=9; ++i) { 25 | coll.push_back(i); 26 | } 27 | PRINT_ELEMENTS(coll); 28 | 29 | // remove all elements that are greater than four and less than seven 30 | // - retain new end 31 | vector::iterator pos; 32 | pos = remove_if (coll.begin(),coll.end(), 33 | compose_f_gx_hx(logical_and(), 34 | bind2nd(greater(),4), 35 | bind2nd(less(),7))); 36 | 37 | // remove ``removed'' elements in coll 38 | coll.erase(pos,coll.end()); 39 | 40 | PRINT_ELEMENTS(coll); 41 | } 42 | -------------------------------------------------------------------------------- /fo/compose21.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | /* class for the compose_f_gx_hx adapter 14 | */ 15 | template 16 | class compose_f_gx_hx_t 17 | : public std::unary_function 19 | { 20 | private: 21 | OP1 op1; // process: op1(op2(x),op3(x)) 22 | OP2 op2; 23 | OP3 op3; 24 | public: 25 | // constructor 26 | compose_f_gx_hx_t (const OP1& o1, const OP2& o2, const OP3& o3) 27 | : op1(o1), op2(o2), op3(o3) { 28 | } 29 | 30 | // function call 31 | typename OP1::result_type 32 | operator()(const typename OP2::argument_type& x) const { 33 | return op1(op2(x),op3(x)); 34 | } 35 | }; 36 | 37 | /* convenience function for the compose_f_gx_hx adapter 38 | */ 39 | template 40 | inline compose_f_gx_hx_t 41 | compose_f_gx_hx (const OP1& o1, const OP2& o2, const OP3& o3) { 42 | return compose_f_gx_hx_t(o1,o2,o3); 43 | } 44 | -------------------------------------------------------------------------------- /fo/compose22.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | /* class for the compose_f_gx_hy adapter 14 | */ 15 | template 16 | class compose_f_gx_hy_t 17 | : public std::binary_function 20 | { 21 | private: 22 | OP1 op1; // process: op1(op2(x),op3(y)) 23 | OP2 op2; 24 | OP3 op3; 25 | public: 26 | // constructor 27 | compose_f_gx_hy_t (const OP1& o1, const OP2& o2, const OP3& o3) 28 | : op1(o1), op2(o2), op3(o3) { 29 | } 30 | 31 | // function call 32 | typename OP1::result_type 33 | operator()(const typename OP2::argument_type& x, 34 | const typename OP3::argument_type& y) const { 35 | return op1(op2(x),op3(y)); 36 | } 37 | }; 38 | 39 | /* convenience function for the compose_f_gx_hy adapter 40 | */ 41 | template 42 | inline compose_f_gx_hy_t 43 | compose_f_gx_hy (const OP1& o1, const OP2& o2, const OP3& o3) { 44 | return compose_f_gx_hy_t(o1,o2,o3); 45 | } 46 | -------------------------------------------------------------------------------- /fo/compose3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "compose22.hpp" 17 | using namespace std; 18 | 19 | int main() 20 | { 21 | string s("Internationalization"); 22 | string sub("Nation"); 23 | 24 | // search substring case insensitive 25 | string::iterator pos; 26 | pos = search (s.begin(),s.end(), // string to search in 27 | sub.begin(),sub.end(), // substring to search 28 | compose_f_gx_hy(equal_to(), // compar. criterion 29 | ptr_fun(::toupper), 30 | ptr_fun(::toupper))); 31 | 32 | if (pos != s.end()) { 33 | cout << "\"" << sub << "\" is part of \"" << s << "\"" 34 | << endl; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /fo/compose4.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "print.hpp" 16 | #include "compose10.hpp" 17 | using namespace std; 18 | using namespace boost; 19 | 20 | 21 | int main() 22 | { 23 | list coll; 24 | 25 | // insert five random numbers 26 | generate_n (back_inserter(coll), // beginning of destination range 27 | 5, // count 28 | rand); // new value generator 29 | PRINT_ELEMENTS(coll); 30 | 31 | // overwrite with five new random numbers 32 | // in the range between 0 (including) and 10 (excluding) 33 | generate (coll.begin(), coll.end(), // destination range 34 | compose_f_g(bind2nd(modulus(),10), 35 | ptr_fun(rand))); 36 | PRINT_ELEMENTS(coll); 37 | } 38 | -------------------------------------------------------------------------------- /fo/fopow.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | 14 | template 15 | struct fopow : public std::binary_function 16 | { 17 | T1 operator() (T1 base, T2 exp) const { 18 | return std::pow(base,exp); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /fo/fopow1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | // include self-defined fopow<> 18 | #include "fopow.hpp" 19 | 20 | int main() 21 | { 22 | vector coll; 23 | 24 | // insert elements from 1 to 9 25 | for (int i=1; i<=9; ++i) { 26 | coll.push_back(i); 27 | } 28 | 29 | // print 3 raised to the power of all elements 30 | transform (coll.begin(), coll.end(), // source 31 | ostream_iterator(cout," "), // destination 32 | bind1st(fopow(),3)); // operation 33 | cout << endl; 34 | 35 | // print all elements raised to the power of 3 36 | transform (coll.begin(), coll.end(), // source 37 | ostream_iterator(cout," "), // destination 38 | bind2nd(fopow(),3)); // operation 39 | cout << endl; 40 | } 41 | -------------------------------------------------------------------------------- /fo/foreach3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | // function object to process the mean value 17 | class MeanValue { 18 | private: 19 | long num; // number of elements 20 | long sum; // sum of all element values 21 | public: 22 | // constructor 23 | MeanValue () : num(0), sum(0) { 24 | } 25 | 26 | // ``function call'' 27 | // - process one more element of the sequence 28 | void operator() (int elem) { 29 | num++; // increment count 30 | sum += elem; // add value 31 | } 32 | 33 | // return mean value 34 | double value () { 35 | return static_cast(sum) / static_cast(num); 36 | } 37 | }; 38 | 39 | int main() 40 | { 41 | vector coll; 42 | 43 | // insert elments from 1 to 8 44 | for (int i=1; i<=8; ++i) { 45 | coll.push_back(i); 46 | } 47 | 48 | // process and print mean value 49 | MeanValue mv = for_each (coll.begin(), coll.end(), // range 50 | MeanValue()); // operation 51 | cout << "mean value: " << mv.value() << endl; 52 | } 53 | -------------------------------------------------------------------------------- /fo/genera1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include "print.hpp" 15 | using namespace std; 16 | 17 | class IntSequence { 18 | private: 19 | int value; 20 | public: 21 | // constructor 22 | IntSequence (int initialValue) 23 | : value(initialValue) { 24 | } 25 | 26 | // ``function call'' 27 | int operator() () { 28 | return value++; 29 | } 30 | }; 31 | 32 | int main() 33 | { 34 | list coll; 35 | 36 | // insert values from 1 to 9 37 | generate_n (back_inserter(coll), // start 38 | 9, // number of elements 39 | IntSequence(1)); // generates values 40 | 41 | PRINT_ELEMENTS(coll); 42 | 43 | // replace second to last element but one with values starting at 42 44 | generate (++coll.begin(), // start 45 | --coll.end(), // end 46 | IntSequence(42)); // generates values 47 | 48 | PRINT_ELEMENTS(coll); 49 | } 50 | -------------------------------------------------------------------------------- /fo/nullary.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | namespace boost { 12 | 13 | /********************************************************** 14 | * type nullary_function 15 | * - as supplement to unary_function and binary_function 16 | **********************************************************/ 17 | template 18 | struct nullary_function { 19 | typedef Result result_type; 20 | }; 21 | 22 | /********************************************************** 23 | * ptr_fun for functions with no argument 24 | **********************************************************/ 25 | template 26 | class pointer_to_nullary_function : public nullary_function 27 | { 28 | protected: 29 | Result (*ptr)(); 30 | public: 31 | pointer_to_nullary_function() { 32 | } 33 | explicit pointer_to_nullary_function(Result (*x)()) : ptr(x) { 34 | } 35 | Result operator()() const { 36 | return ptr(); 37 | } 38 | }; 39 | 40 | template 41 | inline pointer_to_nullary_function ptr_fun(Result (*x)()) 42 | { 43 | return pointer_to_nullary_function(x); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /fo/print.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | /* PRINT_ELEMENTS() 14 | * - prints optional C-string optcstr followed by 15 | * - all elements of the collection coll 16 | * - separated by spaces 17 | */ 18 | template 19 | inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") 20 | { 21 | typename T::const_iterator pos; 22 | 23 | std::cout << optcstr; 24 | for (pos=coll.begin(); pos!=coll.end(); ++pos) { 25 | std::cout << *pos << ' '; 26 | } 27 | std::cout << std::endl; 28 | } 29 | -------------------------------------------------------------------------------- /fo/removeif.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include "print.hpp" 15 | using namespace std; 16 | 17 | class Nth { // function object that returns true for the nth call 18 | private: 19 | int nth; // call for which to return true 20 | int count; // call counter 21 | public: 22 | Nth (int n) : nth(n), count(0) { 23 | } 24 | bool operator() (int) { 25 | return ++count == nth; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | list coll; 32 | 33 | // insert elements from 1 to 9 34 | for (int i=1; i<=9; ++i) { 35 | coll.push_back(i); 36 | } 37 | PRINT_ELEMENTS(coll,"coll: "); 38 | 39 | // remove third element 40 | list::iterator pos; 41 | pos = remove_if(coll.begin(),coll.end(), // range 42 | Nth(3)); // remove criterion 43 | coll.erase(pos,coll.end()); 44 | 45 | PRINT_ELEMENTS(coll,"nth removed: "); 46 | } 47 | -------------------------------------------------------------------------------- /i18n/Makefile: -------------------------------------------------------------------------------- 1 | include ../Makefile.h 2 | 3 | # Programme fuer die make PROG.out aufgerufen wird 4 | # 5 | OUTPROGS = loc1 loc2 6 | CPPPROGS = numget 7 | 8 | -------------------------------------------------------------------------------- /i18n/loc1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | // use classic C locale to read data from standard input 18 | cin.imbue(locale::classic()); 19 | 20 | // use a German locale to write data to standard ouput 21 | cout.imbue(locale("de_DE")); 22 | 23 | // read and output floating-point values in a loop 24 | double value; 25 | while (cin >> value) { 26 | cout << value << endl; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /io/Makefile: -------------------------------------------------------------------------------- 1 | CPPPROGS = io1 sum1 sum2 charcat1 \ 2 | ignore1 ignoreparam1 charset \ 3 | cat1 cat2 charcat2 \ 4 | inbuf1 \ 5 | copy1 copy2 \ 6 | countlines 7 | 8 | OUTPROGS = rdbuf1 rdbuf2 redirect \ 9 | rw1 sstr1 \ 10 | outbuf1 outbuf1x outbuf2 outbuf3 11 | 12 | HEADERS = ignore.hpp ignoreparam.hpp \ 13 | frac1out.hpp frac2out.hpp frac1in.hpp frac2in.hpp \ 14 | outbuf1.hpp outbuf1x.hpp outbuf2.hpp outbuf3.hpp \ 15 | inbuf1.hpp 16 | 17 | include ../Makefile.h 18 | 19 | outbuf1: outbuf1.hpp outbuf1.cpp 20 | $(CXX) $(CXXFLAGS) $@.cpp $(LDFLAGS) -o $@ 21 | outbuf1x: outbuf1x.hpp outbuf1x.cpp 22 | $(CXX) $(CXXFLAGS) $@.cpp $(LDFLAGS) -o $@ 23 | outbuf2: outbuf2.hpp outbuf2.cpp 24 | $(CXX) $(CXXFLAGS) $@.cpp $(LDFLAGS) -o $@ 25 | outbuf3: outbuf3.hpp outbuf3.cpp 26 | $(CXX) $(CXXFLAGS) $@.cpp $(LDFLAGS) -o $@ 27 | 28 | ignoreparam1: ignoreparam1.cpp ignoreparam.hpp 29 | $(CXX) $(CXXFLAGS) $@.cpp $(LDFLAGS) -o $@ 30 | 31 | -------------------------------------------------------------------------------- /io/cat1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | // header files for file I/O 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | /* for all file names passed as command-line arguments 17 | * - open, print contents, and close file 18 | */ 19 | int main (int argc, char* argv[]) 20 | { 21 | ifstream file; 22 | 23 | // for all command-line arguments 24 | for (int i=1; i 13 | #include 14 | 15 | void printFileTwice (const char* filename) 16 | { 17 | // open file 18 | std::ifstream file(filename); 19 | 20 | // print contents the first time 21 | std::cout << file.rdbuf(); 22 | 23 | // seek to the beginning 24 | file.seekg(0); 25 | 26 | // print contents the second time 27 | std::cout << file.rdbuf(); 28 | } 29 | 30 | int main (int argc, char* argv[]) 31 | { 32 | // print all files passed as a command-line argument twice 33 | for (int i=1; i 12 | using namespace std; 13 | 14 | int main() 15 | { 16 | char c; 17 | 18 | // while it is possible to read a character 19 | while (cin.get(c)) { 20 | // print it 21 | cout.put(c); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /io/charcat2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | // input stream buffer iterator for cin 18 | istreambuf_iterator inpos(cin); 19 | 20 | // end-of-stream iterator 21 | istreambuf_iterator endpos; 22 | 23 | // output stream buffer iterator for cout 24 | ostreambuf_iterator outpos(cout); 25 | 26 | // while input iterator is valid 27 | while (inpos != endpos) { 28 | *outpos = *inpos; // assign its value to the output iterator 29 | ++inpos; 30 | ++outpos; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /io/copy1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | int main () 14 | { 15 | // copy all standard input to standard output 16 | std::cout << std::cin.rdbuf(); 17 | } 18 | -------------------------------------------------------------------------------- /io/copy2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | int main () 14 | { 15 | // copy all standard input to standard output 16 | std::cin >> std::noskipws >> std::cout.rdbuf(); 17 | } 18 | -------------------------------------------------------------------------------- /io/countlines.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | int countLines (std::istream& in); 17 | 18 | int main (int argc, char* argv[]) 19 | { 20 | int count; 21 | 22 | if (argc == 1) { 23 | // no argument => count lines of standard input 24 | count = countLines(std::cin); 25 | } 26 | else { 27 | // count number of lines of all files passed as argument 28 | std::ifstream in; 29 | count = 0; 30 | for (int i=1; i(in), 47 | std::istreambuf_iterator(), 48 | '\n'); 49 | } 50 | -------------------------------------------------------------------------------- /io/frac1in.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | inline 14 | std::istream& operator >> (std::istream& strm, Fraction& f) 15 | { 16 | int n, d; 17 | 18 | strm >> n; // read value of the numerator 19 | strm.ignore(); // skip '/' 20 | strm >> d; // read value of the denominator 21 | 22 | f = Fraction(n,d); // assign the whole fraction 23 | 24 | return strm; 25 | } 26 | -------------------------------------------------------------------------------- /io/frac1out.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | inline 14 | std::ostream& operator << (std::ostream& strm, const Fraction& f) 15 | { 16 | strm << f.numerator() << '/' << f.denominator(); 17 | return strm; 18 | } 19 | -------------------------------------------------------------------------------- /io/frac2in.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | template 14 | inline 15 | std::basic_istream& 16 | operator >> (std::basic_istream& strm, Fraction& f) 17 | { 18 | int n, d; 19 | 20 | // read value of numerator 21 | strm >> n; 22 | 23 | /* if available 24 | * - read '/' and value of demonimator 25 | */ 26 | if (strm.peek() == '/') { 27 | strm.ignore(); 28 | strm >> d; 29 | } 30 | else { 31 | d = 1; 32 | } 33 | 34 | /* if denominator is zero 35 | * - set failbit as I/O format error 36 | */ 37 | if (d == 0) { 38 | strm.setstate(std::ios::failbit); 39 | return strm; 40 | } 41 | 42 | /* if everything is fine so far 43 | * change the value of the fraction 44 | */ 45 | if (strm) { 46 | f = Fraction(n,d); 47 | } 48 | 49 | return strm; 50 | } 51 | -------------------------------------------------------------------------------- /io/frac2out.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | 14 | template 15 | inline 16 | std::basic_ostream& 17 | operator << (std::basic_ostream& strm, 18 | const Fraction& f) 19 | { 20 | /* string stream 21 | * - with same format 22 | * - without special field width 23 | */ 24 | std::basic_ostringstream s; 25 | s.copyfmt(strm); 26 | s.width(0); 27 | 28 | // fill string stream 29 | s << f.numerator() << '/' << f.denominator(); 30 | 31 | // print string stream 32 | strm << s.str(); 33 | 34 | return strm; 35 | } 36 | -------------------------------------------------------------------------------- /io/ignore.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | 14 | template 15 | inline 16 | std::basic_istream& 17 | ignoreLine (std::basic_istream& strm) 18 | { 19 | // skip until end-of-line 20 | strm.ignore(std::numeric_limits::max(),strm.widen('\n')); 21 | 22 | // return stream for concatenation 23 | return strm; 24 | } 25 | -------------------------------------------------------------------------------- /io/ignore1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "ignore.hpp" 13 | 14 | int main() 15 | { 16 | int i; 17 | std::cout << "read int and ignore rest of the line" << std::endl; 18 | std::cin >> i; 19 | 20 | // ignore the rest of the line 21 | std::cin >> ignoreLine; 22 | 23 | std::cout << "int: " << i << std::endl; 24 | 25 | std::cout << "read int and ignore two lines" << std::endl; 26 | std::cin >> i; 27 | 28 | // ignore two lines 29 | std::cin >> ignoreLine >> ignoreLine; 30 | 31 | std::cout << "int: " << i << std::endl; 32 | } 33 | -------------------------------------------------------------------------------- /io/ignoreparam.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | 14 | // Ignore: manipulator that ignores N lines 15 | class Ignore { 16 | public: 17 | int num; 18 | Ignore(int n) : num(n) { 19 | } 20 | }; 21 | 22 | // convenience function 23 | Ignore ignore(int n) 24 | { 25 | return Ignore(n); 26 | } 27 | 28 | std::istream& operator >> (std::istream& strm, const Ignore& manip) 29 | { 30 | for (int i=0; i::max(),'\n'); 32 | } 33 | return strm; 34 | } 35 | -------------------------------------------------------------------------------- /io/ignoreparam1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "ignoreparam.hpp" 13 | 14 | int main() 15 | { 16 | char c; 17 | std::cout << "ignore two lines and print frist character following them\n"; 18 | std::cin >> ignore(2) >> c; 19 | std::cout << "c: " << c << std::endl; 20 | } 21 | -------------------------------------------------------------------------------- /io/inbuf1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "inbuf1.hpp" 13 | 14 | int main() 15 | { 16 | inbuf ib; // create special stream buffer 17 | std::istream in(&ib); // initialize input stream with that buffer 18 | 19 | char c; 20 | for (int i=1; i<=20; i++) { 21 | // read next character (out of the buffer) 22 | in.get(c); 23 | 24 | // print that character (and flush) 25 | std::cout << c << std::flush; 26 | 27 | // after eight characters, put two characters back into the stream 28 | if (i == 8) { 29 | in.unget(); 30 | in.unget(); 31 | } 32 | } 33 | std::cout << std::endl; 34 | } 35 | -------------------------------------------------------------------------------- /io/io1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | double x, y; // operands 18 | 19 | // print header string 20 | cout << "Multiplication of two floating point values" << endl; 21 | 22 | // read first operand 23 | cout << "first operand: "; 24 | if (! (cin >> x)) { 25 | /* input error 26 | * => error message and exit program with error status 27 | */ 28 | cerr << "error while reading the first floating value" 29 | << endl; 30 | return EXIT_FAILURE; 31 | } 32 | 33 | // read second operand 34 | cout << "second operand: "; 35 | if (! (cin >> y)) { 36 | /* input error 37 | * => error message and exit program with error status 38 | */ 39 | cerr << "error while reading the second floating value" 40 | << endl; 41 | return EXIT_FAILURE; 42 | } 43 | 44 | // print operands and result 45 | cout << x << " times " << y << " equals " << x * y << endl; 46 | } 47 | -------------------------------------------------------------------------------- /io/outbuf1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "outbuf1.hpp" 13 | 14 | int main() 15 | { 16 | outbuf ob; // create special output buffer 17 | std::ostream out(&ob); // initialize output stream with that output buffer 18 | 19 | out << "31 hexadecimal: " << std::hex << 31 << std::endl; 20 | } 21 | -------------------------------------------------------------------------------- /io/outbuf1.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | 15 | class outbuf : public std::streambuf 16 | { 17 | protected: 18 | /* central output function 19 | * - print characters in uppercase mode 20 | */ 21 | virtual int_type overflow (int_type c) { 22 | if (c != EOF) { 23 | // convert lowercase to uppercase 24 | c = std::toupper(c,getloc()); 25 | 26 | // and write the character to the standard output 27 | if (putchar(c) == EOF) { 28 | return EOF; 29 | } 30 | } 31 | return c; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /io/outbuf1x.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "outbuf1x.hpp" 13 | 14 | int main() 15 | { 16 | outbuf ob; // create special output buffer 17 | std::ostream out(&ob); // initialize output stream with that output buffer 18 | 19 | out << "31 hexadecimal: " << std::hex << 31 << std::endl; 20 | } 21 | -------------------------------------------------------------------------------- /io/outbuf1x.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | 15 | template > 16 | class basic_outbuf : public std::basic_streambuf 17 | { 18 | protected: 19 | /* central output function 20 | * - print characters in uppercase mode 21 | */ 22 | virtual typename traits::int_type 23 | overflow (typename traits::int_type c) { 24 | if (!traits::eq_int_type(c,traits::eof())) { 25 | // convert lowercase to uppercase 26 | c = std::toupper(c,getloc()); 27 | 28 | // and write the character to the standard output 29 | if (putchar(c) == EOF) { 30 | return traits::eof(); 31 | } 32 | } 33 | return traits::not_eof(c); 34 | } 35 | }; 36 | 37 | typedef basic_outbuf outbuf; 38 | typedef basic_outbuf woutbuf; 39 | -------------------------------------------------------------------------------- /io/outbuf2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "outbuf2.hpp" 13 | 14 | int main() 15 | { 16 | fdostream out(1); // stream with buffer writing to file descriptor 1 17 | 18 | out << "31 hexadecimal: " << std::hex << 31 << std::endl; 19 | } 20 | -------------------------------------------------------------------------------- /io/outbuf2.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | 15 | // for write(): 16 | #ifdef _MSC_VER 17 | # include 18 | #else 19 | # include 20 | #endif 21 | 22 | class fdoutbuf : public std::streambuf { 23 | protected: 24 | int fd; // file descriptor 25 | public: 26 | // constructor 27 | fdoutbuf (int _fd) : fd(_fd) { 28 | } 29 | protected: 30 | // write one character 31 | virtual int_type overflow (int_type c) { 32 | if (c != EOF) { 33 | char z = c; 34 | if (write (fd, &z, 1) != 1) { 35 | return EOF; 36 | } 37 | } 38 | return c; 39 | } 40 | // write multiple characters 41 | virtual std::streamsize xsputn (const char* s, 42 | std::streamsize num) { 43 | return write(fd,s,num); 44 | } 45 | }; 46 | 47 | class fdostream : public std::ostream { 48 | protected: 49 | fdoutbuf buf; 50 | public: 51 | fdostream (int fd) : std::ostream(0), buf(fd) { 52 | rdbuf(&buf); 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /io/outbuf3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "outbuf3.hpp" 13 | 14 | int main() 15 | { 16 | outbuf ob; // create special output buffer 17 | std::ostream out(&ob); // initialize output stream with that output buffer 18 | 19 | out << "31 hexadecimal: " << std::hex << 31 << std::endl; 20 | } 21 | -------------------------------------------------------------------------------- /io/rdbuf1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | // stream for hexadecimal standard output 18 | ostream hexout(cout.rdbuf()); 19 | hexout.setf (ios::hex, ios::basefield); 20 | hexout.setf (ios::showbase); 21 | 22 | // switch between decimal and hexadecimal output 23 | hexout << "hexout: " << 177 << " "; 24 | cout << "cout: " << 177 << " "; 25 | hexout << "hexout: " << -49 << " "; 26 | cout << "cout: " << -49 << " "; 27 | hexout << endl; 28 | } 29 | -------------------------------------------------------------------------------- /io/rdbuf2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | 14 | void hexMultiplicationTable (std::streambuf* buffer, int num) 15 | { 16 | std::ostream hexout(buffer); 17 | hexout << std::hex << std::showbase; 18 | 19 | for (int i=1; i<=num; ++i) { 20 | for (int j=1; j<=10; ++j) { 21 | hexout << i*j << ' '; 22 | } 23 | hexout << std::endl; 24 | } 25 | 26 | } // does NOT close buffer 27 | 28 | int main() 29 | { 30 | using namespace std; 31 | int num = 5; 32 | 33 | cout << "We print " << num 34 | << " lines hexadecimal" << endl; 35 | 36 | hexMultiplicationTable(cout.rdbuf(),num); 37 | 38 | cout << "That was the output of " << num 39 | << " hexadecimal lines " << endl; 40 | } 41 | -------------------------------------------------------------------------------- /io/redirect.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | void redirect(ostream&); 16 | 17 | int main() 18 | { 19 | cout << "the first row" << endl; 20 | 21 | redirect(cout); 22 | 23 | cout << "the last row" << endl; 24 | } 25 | 26 | void redirect (ostream& strm) 27 | { 28 | ofstream file("redirect.txt"); 29 | 30 | // save output buffer of the stream 31 | streambuf* strm_buffer = strm.rdbuf(); 32 | 33 | // redirect ouput into the file 34 | strm.rdbuf (file.rdbuf()); 35 | 36 | file << "one row for the file" << endl; 37 | strm << "one row for the stream" << endl; 38 | 39 | // restore old output buffer 40 | strm.rdbuf (strm_buffer); 41 | 42 | } // closes file AND its buffer automatically 43 | -------------------------------------------------------------------------------- /io/rw1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | // open file ``example.dat'' for reading and writing 18 | filebuf buffer; 19 | ostream output(&buffer); 20 | istream input(&buffer); 21 | buffer.open ("example.dat", ios::in | ios::out | ios::trunc); 22 | 23 | for (int i=1; i<=4; i++) { 24 | // write one line 25 | output << i << ". line" << endl; 26 | 27 | // print all file contents 28 | input.seekg(0); // seek to the beginning 29 | char c; 30 | while (input.get(c)) { 31 | cout.put(c); 32 | } 33 | cout << endl; 34 | input.clear(); // clear eofbit and failbit 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /io/sstr1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | ostringstream os; 19 | 20 | // decimal and hexadecimal value 21 | os << "dec: " << 15 << hex << " hex: " << 15 << endl; 22 | cout << os.str() << endl; 23 | 24 | // append floating value and bitset 25 | bitset<15> b(5789); 26 | os << "float: " << 4.67 << " bitset: " << b << endl; 27 | 28 | // overwrite with octal value 29 | os.seekp(0); 30 | os << "oct: " << oct << 15; 31 | cout << os.str() << endl; 32 | } 33 | -------------------------------------------------------------------------------- /iter/Makefile: -------------------------------------------------------------------------------- 1 | OUTPROGS = itercat advance1 distance swap1 \ 2 | reviter1 reviter2 reviter3 reviter4 \ 3 | backins frontins inserter \ 4 | ostriter \ 5 | assoiter 6 | 7 | CPPPROGS = istriter advance2 8 | 9 | HEADERS = distance.hpp assoiter.hpp print.hpp 10 | 11 | EXPORT = istriter.in advance2.in 12 | 13 | include ../Makefile.h 14 | 15 | assoiter: assoiter.hpp assoiter.cpp 16 | $(CXX) $(CXXFLAGS) $(LDFLAGS) $@.cpp -o $@ 17 | 18 | -------------------------------------------------------------------------------- /iter/advance1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | list coll; 19 | 20 | // insert elements from 1 to 9 21 | for (int i=1; i<=9; ++i) { 22 | coll.push_back(i); 23 | } 24 | 25 | list::iterator pos = coll.begin(); 26 | 27 | // print actual element 28 | cout << *pos << endl; 29 | 30 | // step three elements forward 31 | advance (pos, 3); 32 | 33 | // print actual element 34 | cout << *pos << endl; 35 | 36 | // step one element backward 37 | advance (pos, -1); 38 | 39 | // print actual element 40 | cout << *pos << endl; 41 | } 42 | -------------------------------------------------------------------------------- /iter/advance2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | istream_iterator cinPos(cin); 20 | ostream_iterator coutPos(cout," "); 21 | 22 | /* while input is not at the end of the file 23 | * - write every third string 24 | */ 25 | while (cinPos != istream_iterator()) { 26 | // ignore the following two strings 27 | advance (cinPos, 2); 28 | 29 | // read and write the third string 30 | if (cinPos != istream_iterator()) { 31 | *coutPos++ = *cinPos++; 32 | } 33 | } 34 | cout << endl; 35 | } 36 | -------------------------------------------------------------------------------- /iter/assoiter.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | #include "print.hpp" 17 | 18 | #include "assoiter.hpp" 19 | 20 | int main() 21 | { 22 | set coll; 23 | 24 | // create inserter for coll 25 | // - inconvenient way 26 | asso_insert_iterator > iter(coll); 27 | 28 | // insert elements with the usual iterator interface 29 | *iter = 1; 30 | iter++; 31 | *iter = 2; 32 | iter++; 33 | *iter = 3; 34 | 35 | PRINT_ELEMENTS(coll); 36 | 37 | // create inserter for coll and insert elements 38 | // - convenient way 39 | asso_inserter(coll) = 44; 40 | asso_inserter(coll) = 55; 41 | 42 | PRINT_ELEMENTS(coll); 43 | 44 | // use inserter with an algorithm 45 | int vals[] = { 33, 67, -4, 13, 5, 2 }; 46 | copy (vals, vals+(sizeof(vals)/sizeof(vals[0])), // source 47 | asso_inserter(coll)); // destination 48 | 49 | PRINT_ELEMENTS(coll); 50 | } 51 | -------------------------------------------------------------------------------- /iter/backins.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include "print.hpp" 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | vector coll; 20 | 21 | // create back inserter for coll 22 | // - inconvenient way 23 | back_insert_iterator > iter(coll); 24 | 25 | // insert elements with the usual iterator interface 26 | *iter = 1; 27 | iter++; 28 | *iter = 2; 29 | iter++; 30 | *iter = 3; 31 | PRINT_ELEMENTS(coll); 32 | 33 | // create back inserter and insert elements 34 | // - convenient way 35 | back_inserter(coll) = 44; 36 | back_inserter(coll) = 55; 37 | PRINT_ELEMENTS(coll); 38 | 39 | // use back inserter to append all elements again 40 | // - reserve enough memory to avoid reallocation 41 | coll.reserve(2*coll.size()); 42 | copy (coll.begin(), coll.end(), // source 43 | back_inserter(coll)); // destination 44 | PRINT_ELEMENTS(coll); 45 | } 46 | -------------------------------------------------------------------------------- /iter/distance.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | list coll; 19 | 20 | // insert elements from -3 to 9 21 | for (int i=-3; i<=9; ++i) { 22 | coll.push_back(i); 23 | } 24 | 25 | // search element with value 5 26 | list::iterator pos; 27 | pos = find (coll.begin(), coll.end(), // range 28 | 5); // value 29 | 30 | if (pos != coll.end()) { 31 | // process and print difference from the beginning 32 | cout << "difference between beginning and 5: " 33 | << distance(coll.begin(),pos) << endl; 34 | } 35 | else { 36 | cout << "5 not found" << endl; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /iter/distance.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | template 12 | inline long distance (Iterator pos1, Iterator pos2) 13 | { 14 | long d = 0; 15 | distance (pos1, pos2, d); 16 | return d; 17 | } 18 | -------------------------------------------------------------------------------- /iter/frontins.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | 12 | #include 13 | #include 14 | #include 15 | #include "print.hpp" 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | list coll; 21 | 22 | // create front inserter for coll 23 | // - inconvenient way 24 | front_insert_iterator > iter(coll); 25 | 26 | // insert elements with the usual iterator interface 27 | *iter = 1; 28 | iter++; 29 | *iter = 2; 30 | iter++; 31 | *iter = 3; 32 | 33 | PRINT_ELEMENTS(coll); 34 | 35 | // create front inserter and insert elements 36 | // - convenient way 37 | front_inserter(coll) = 44; 38 | front_inserter(coll) = 55; 39 | 40 | PRINT_ELEMENTS(coll); 41 | 42 | // use front inserter to insert all elements again 43 | copy (coll.begin(), coll.end(), // source 44 | front_inserter(coll)); // destination 45 | 46 | PRINT_ELEMENTS(coll); 47 | } 48 | -------------------------------------------------------------------------------- /iter/istriter.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | // create istream iterator that reads integers from cin 18 | istream_iterator intReader(cin); 19 | 20 | // create end-of-stream iterator 21 | istream_iterator intReaderEOF; 22 | 23 | /* while able to read tokens with istream iterator 24 | * - write them twice 25 | */ 26 | while (intReader != intReaderEOF) { 27 | cout << "once: " << *intReader << endl; 28 | cout << "once again: " << *intReader << endl; 29 | ++intReader; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /iter/itercat.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | vector coll; 18 | 19 | // insert elements from -3 to 9 20 | for (int i=-3; i<=9; ++i) { 21 | coll.push_back (i); 22 | } 23 | 24 | /* print number of elements by processing the distance between beginning and end 25 | * - NOTE: uses operator - for iterators 26 | */ 27 | cout << "number/distance: " << coll.end()-coll.begin() << endl; 28 | 29 | /* print all elements 30 | * - NOTE: uses operator < instead of operator != 31 | */ 32 | vector::iterator pos; 33 | for (pos=coll.begin(); pos 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | // create ostream iterator for stream cout 20 | // - values are separated by a newline character 21 | ostream_iterator intWriter(cout,"\n"); 22 | 23 | // write elements with the usual iterator interface 24 | *intWriter = 42; 25 | intWriter++; 26 | *intWriter = 77; 27 | intWriter++; 28 | *intWriter = -5; 29 | 30 | // create collection with elements from 1 to 9 31 | vector coll; 32 | for (int i=1; i<=9; ++i) { 33 | coll.push_back(i); 34 | } 35 | 36 | // write all elements without any delimiter 37 | copy (coll.begin(), coll.end(), 38 | ostream_iterator(cout)); 39 | cout << endl; 40 | 41 | // write all elements with " < " as delimiter 42 | copy (coll.begin(), coll.end(), 43 | ostream_iterator(cout," < ")); 44 | cout << endl; 45 | } 46 | -------------------------------------------------------------------------------- /iter/print.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | /* PRINT_ELEMENTS() 14 | * - prints optional C-string optcstr followed by 15 | * - all elements of the collection coll 16 | * - separated by spaces 17 | */ 18 | template 19 | inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") 20 | { 21 | typename T::const_iterator pos; 22 | 23 | std::cout << optcstr; 24 | for (pos=coll.begin(); pos!=coll.end(); ++pos) { 25 | std::cout << *pos << ' '; 26 | } 27 | std::cout << std::endl; 28 | } 29 | -------------------------------------------------------------------------------- /iter/reviter1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | void print (int elem) 17 | { 18 | cout << elem << ' '; 19 | } 20 | 21 | int main() 22 | { 23 | list coll; 24 | 25 | // insert elements from 1 to 9 26 | for (int i=1; i<=9; ++i) { 27 | coll.push_back(i); 28 | } 29 | 30 | // print all elements in normal order 31 | for_each (coll.begin(), coll.end(), // range 32 | print); // operation 33 | cout << endl; 34 | 35 | // print all elements in reverse order 36 | for_each (coll.rbegin(), coll.rend(), // range 37 | print); // operations 38 | cout << endl; 39 | } 40 | -------------------------------------------------------------------------------- /iter/reviter2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | vector coll; 19 | 20 | // insert elements from 1 to 9 21 | for (int i=1; i<=9; ++i) { 22 | coll.push_back(i); 23 | } 24 | 25 | // find position of element with value 5 26 | vector::iterator pos; 27 | pos = find (coll.begin(), coll.end(), 28 | 5); 29 | 30 | // print value to which iterator pos refers 31 | cout << "pos: " << *pos << endl; 32 | 33 | // convert iterator to reverse iterator rpos 34 | vector::reverse_iterator rpos(pos); 35 | 36 | // print value to which reverse iterator rpos refers 37 | cout << "rpos: " << *rpos << endl; 38 | } 39 | -------------------------------------------------------------------------------- /iter/reviter4.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | list coll; 19 | 20 | // insert elements from 1 to 9 21 | for (int i=1; i<=9; ++i) { 22 | coll.push_back(i); 23 | } 24 | 25 | // find position of element with value 5 26 | list::iterator pos; 27 | pos = find (coll.begin(), coll.end(), // range 28 | 5); // value 29 | 30 | // print value of the element 31 | cout << "pos: " << *pos << endl; 32 | 33 | // convert iterator to reverse iterator 34 | list::reverse_iterator rpos(pos); 35 | 36 | // print value of the element to which the reverse iterator refers 37 | cout << "rpos: " << *rpos << endl; 38 | 39 | // convert reverse iterator back to normal iterator 40 | list::iterator rrpos; 41 | rrpos = rpos.base(); 42 | 43 | // print value of the element to which the normal iterator refers 44 | cout << "rrpos: " << *rrpos << endl; 45 | } 46 | -------------------------------------------------------------------------------- /iter/swap1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include "print.hpp" 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | list coll; 20 | 21 | // insert elements from 1 to 9 22 | for (int i=1; i<=9; ++i) { 23 | coll.push_back(i); 24 | } 25 | 26 | PRINT_ELEMENTS(coll); 27 | 28 | // swap first and second value 29 | iter_swap (coll.begin(), ++coll.begin()); 30 | 31 | PRINT_ELEMENTS(coll); 32 | 33 | // swap first and last value 34 | iter_swap (coll.begin(), --coll.end()); 35 | 36 | PRINT_ELEMENTS(coll); 37 | } 38 | -------------------------------------------------------------------------------- /memory/Makefile: -------------------------------------------------------------------------------- 1 | include ../Makefile.h 2 | 3 | OUTPROGS = myalloc1 4 | 5 | HEADERS = myalloc.hpp 6 | 7 | 8 | uninit.o: uninit_fill.cpp uninit_fill_n.cpp uninit_copy.cpp \ 9 | allocvoid.cpp defalloc.cpp 10 | 11 | -------------------------------------------------------------------------------- /memory/myalloc1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include "myalloc.hpp" 13 | 14 | int main() 15 | { 16 | // create a vector, using MyAlloc<> as allocator 17 | std::vector > v; 18 | 19 | // insert elements 20 | // - causes reallocations 21 | v.push_back(42); 22 | v.push_back(56); 23 | v.push_back(11); 24 | v.push_back(22); 25 | v.push_back(33); 26 | v.push_back(44); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /num/Makefile: -------------------------------------------------------------------------------- 1 | include ../Makefile.h 2 | 3 | CPPPROGS = \ 4 | complex2 5 | 6 | OUTPROGS = \ 7 | complex1 \ 8 | val1 val2 slice1 gslice1 masked1 indi1 9 | 10 | OTHERS = complextests valtests 11 | 12 | -------------------------------------------------------------------------------- /num/complex2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | complex c1, c2; 20 | 21 | while (cin.peek() != EOF) { 22 | 23 | // read first complex number 24 | cout << "complex number c1: "; 25 | cin >> c1; 26 | if (!cin) { 27 | cerr << "input error" << endl; 28 | return EXIT_FAILURE; 29 | } 30 | 31 | // read second complex number 32 | cout << "complex number c2: "; 33 | cin >> c2; 34 | if (!cin) { 35 | cerr << "input error" << endl; 36 | return EXIT_FAILURE; 37 | } 38 | 39 | if (c1 == c2) { 40 | cout << "c1 and c2 are equal !" << endl; 41 | } 42 | 43 | cout << "c1 raised to the c2: " << pow(c1,c2) 44 | << endl << endl; 45 | 46 | // skip rest of line 47 | cin.ignore(numeric_limits::max(),'\n'); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /num/masked1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | // print valarray line-by-line 16 | template 17 | void printValarray (const valarray& va, int num) 18 | { 19 | for (int i=0; i va(12); 35 | 36 | // fill valarray with values 37 | for (int i=0; i<12; i++) { 38 | va[i] = i; 39 | } 40 | 41 | printValarray (va, 3); 42 | 43 | // assign 77 to all values that are less than 5 44 | va[va<5.0] = 77.0; 45 | 46 | // add 100 to all values that are greater than 5 and less than 9 47 | va[va>5.0 && va<9.0] 48 | = valarray(va[va>5.0 && va<9.0]) + 100.0; 49 | 50 | printValarray (va, 3); 51 | } 52 | -------------------------------------------------------------------------------- /num/val2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | // print valarray 16 | template 17 | void printValarray (const valarray& va) 18 | { 19 | for (int i=0; i va(9); 29 | for (int i=0; i vb(va+10.0); 44 | 45 | // print second valarray 46 | printValarray(vb); 47 | 48 | // create third valarray as a result of processing both existing valarrays 49 | valarray vc(9); 50 | vc = sqrt(va) + vb/2.0 - 1.0; 51 | 52 | // print third valarray 53 | printValarray(vc); 54 | } 55 | -------------------------------------------------------------------------------- /stl/Makefile: -------------------------------------------------------------------------------- 1 | include ../Makefile.h 2 | 3 | OUTPROGS = vector1 deque1 list1 set1 \ 4 | mmap1 map1 algo1 riter1 \ 5 | remove1 remove2 remove3 \ 6 | foreach1 transform1 prime1 \ 7 | sort1 fo1 8 | 9 | CPPPROGS = list2 find1 copy1 copy2 copy3 ioiter1 \ 10 | remove4 foreach2 add1 iterbug1 11 | 12 | HEADERS = print.hpp 13 | 14 | -------------------------------------------------------------------------------- /stl/algo1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | vector coll; 19 | vector::iterator pos; 20 | 21 | // insert elements from 1 to 6 in arbitrary order 22 | coll.push_back(2); 23 | coll.push_back(5); 24 | coll.push_back(4); 25 | coll.push_back(1); 26 | coll.push_back(6); 27 | coll.push_back(3); 28 | 29 | // find and print minimum and maximum elements 30 | pos = min_element (coll.begin(), coll.end()); 31 | cout << "min: " << *pos << endl; 32 | pos = max_element (coll.begin(), coll.end()); 33 | cout << "max: " << *pos << endl; 34 | 35 | // sort all elements 36 | sort (coll.begin(), coll.end()); 37 | 38 | // find the first element with value 3 39 | pos = find (coll.begin(), coll.end(), // range 40 | 3); // value 41 | 42 | // reverse the order of the found element with value 3 and all following elements 43 | reverse (pos, coll.end()); 44 | 45 | // print all elements 46 | for (pos=coll.begin(); pos!=coll.end(); ++pos) { 47 | cout << *pos << ' '; 48 | } 49 | cout << endl; 50 | } 51 | -------------------------------------------------------------------------------- /stl/copy1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | list coll1; 20 | vector coll2; 21 | 22 | // insert elements from 1 to 9 23 | for (int i=1; i<=9; ++i) { 24 | coll1.push_back(i); 25 | } 26 | 27 | // RUNTIME ERROR: 28 | // - overwrites nonexisting elements in the destination 29 | copy (coll1.begin(), coll1.end(), // source 30 | coll2.begin()); // destination 31 | //... 32 | } 33 | -------------------------------------------------------------------------------- /stl/copy2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | list coll1; 21 | vector coll2; 22 | 23 | // insert elements from 1 to 9 24 | for (int i=1; i<=9; ++i) { 25 | coll1.push_back(i); 26 | } 27 | 28 | // resize destination to have enough room for the overwriting algorithm 29 | coll2.resize (coll1.size()); 30 | 31 | /* copy elements from first into second collection 32 | * - overwrites existing elements in destination 33 | */ 34 | copy (coll1.begin(), coll1.end(), // source 35 | coll2.begin()); // destination 36 | 37 | /* create third collection with enough room 38 | * - initial size is passed as parameter 39 | */ 40 | deque coll3(coll1.size()); 41 | 42 | // copy elements from first into third collection 43 | copy (coll1.begin(), coll1.end(), // source 44 | coll3.begin()); // destination 45 | } 46 | -------------------------------------------------------------------------------- /stl/copy3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | using namespace std; 18 | 19 | int main() 20 | { 21 | list coll1; 22 | 23 | // insert elements from 1 to 9 into the first collection 24 | for (int i=1; i<=9; ++i) { 25 | coll1.push_back(i); 26 | } 27 | 28 | // copy the elements of coll1 into coll2 by appending them 29 | vector coll2; 30 | copy (coll1.begin(), coll1.end(), // source 31 | back_inserter(coll2)); // destination 32 | 33 | // copy the elements of coll1 into coll3 by inserting them at the front 34 | // - reverses the order of the elements 35 | deque coll3; 36 | copy (coll1.begin(), coll1.end(), // source 37 | front_inserter(coll3)); // destination 38 | 39 | // copy elements of coll1 into coll4 40 | // - only inserter that works for associative collections 41 | set coll4; 42 | copy (coll1.begin(), coll1.end(), // source 43 | inserter(coll4,coll4.begin())); // destination 44 | } 45 | -------------------------------------------------------------------------------- /stl/deque1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | deque coll; // deque container for floating-point elements 18 | 19 | // insert elements from 1.1 to 6.6 each at the front 20 | for (int i=1; i<=6; ++i) { 21 | coll.push_front(i*1.1); // insert at the front 22 | } 23 | 24 | // print all elements followed by a space 25 | for (int i=0; i 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | // function that prints the passed argument 17 | void print (int elem) 18 | { 19 | cout << elem << ' '; 20 | } 21 | 22 | int main() 23 | { 24 | vector coll; 25 | 26 | // insert elements from 1 to 9 27 | for (int i=1; i<=9; ++i) { 28 | coll.push_back(i); 29 | } 30 | 31 | // print all elements 32 | for_each (coll.begin(), coll.end(), // range 33 | print); // operation 34 | cout << endl; 35 | } 36 | -------------------------------------------------------------------------------- /stl/foreach2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | // simple function object that prints the passed argument 17 | class PrintInt { 18 | public: 19 | void operator() (int elem) const { 20 | cout << elem << ' '; 21 | } 22 | }; 23 | 24 | int main() 25 | { 26 | vector coll; 27 | 28 | // insert elements from 1 to 9 29 | for (int i=1; i<=9; ++i) { 30 | coll.push_back(i); 31 | } 32 | 33 | // print all elements 34 | for_each (coll.begin(), coll.end(), // range 35 | PrintInt()); // operation 36 | cout << endl; 37 | } 38 | -------------------------------------------------------------------------------- /stl/ioiter1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | vector coll; 21 | 22 | /* read all words from the standard input 23 | * - source: all strings until end-of-file (or error) 24 | * - destination: coll (inserting) 25 | */ 26 | copy (istream_iterator(cin), // start of source 27 | istream_iterator(), // end of source 28 | back_inserter(coll)); // destination 29 | 30 | // sort elements 31 | sort (coll.begin(), coll.end()); 32 | 33 | /* print all elements without duplicates 34 | * - source: coll 35 | * - destination: standard output (with newline between elements) 36 | */ 37 | unique_copy (coll.begin(), coll.end(), // source 38 | ostream_iterator(cout,"\n")); // destination 39 | } 40 | -------------------------------------------------------------------------------- /stl/iterbug1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | vector coll1; // empty collection 19 | vector coll2; // empty collection 20 | 21 | /* RUNTIME ERROR: 22 | * - beginning is behind the end of the range 23 | */ 24 | vector::iterator pos = coll1.begin(); 25 | reverse (++pos, coll1.end()); 26 | 27 | // insert elements from 1 to 9 into coll2 28 | for (int i=1; i<=9; ++i) { 29 | coll2.push_back (i); 30 | } 31 | 32 | /* RUNTIME ERROR: 33 | * - overwriting nonexisting elements 34 | */ 35 | copy (coll2.begin(), coll2.end(), // source 36 | coll1.begin()); // destination 37 | 38 | /* RUNTIME ERROR: 39 | * - collections mistaken 40 | * - begin() and end() mistaken 41 | */ 42 | copy (coll1.begin(), coll2.end(), // source 43 | coll1.end()); // destination 44 | } 45 | -------------------------------------------------------------------------------- /stl/list1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | list coll; // list container for character elements 18 | 19 | // append elements from 'a' to 'z' 20 | for (char c='a'; c<='z'; ++c) { 21 | coll.push_back(c); 22 | } 23 | 24 | /* print all elements 25 | * - while there are elements 26 | * - print and remove the first element 27 | */ 28 | while (! coll.empty()) { 29 | cout << coll.front() << ' '; 30 | coll.pop_front(); 31 | } 32 | cout << endl; 33 | } 34 | -------------------------------------------------------------------------------- /stl/list2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | list coll; // list container for character elements 18 | 19 | // append elements from 'a' to 'z' 20 | for (char c='a'; c<='z'; ++c) { 21 | coll.push_back(c); 22 | } 23 | 24 | /* print all elements 25 | * - iterate over all elements 26 | */ 27 | list::const_iterator pos; 28 | for (pos = coll.begin(); pos != coll.end(); ++pos) { 29 | cout << *pos << ' '; 30 | } 31 | cout << endl; 32 | } 33 | -------------------------------------------------------------------------------- /stl/map1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | /* type of the container: 19 | * - map: elements key/value pairs 20 | * - string: keys have type string 21 | * - float: values have type float 22 | */ 23 | typedef map StringFloatMap; 24 | 25 | StringFloatMap coll; 26 | 27 | // insert some elements into the collection 28 | coll["VAT"] = 0.15; 29 | coll["Pi"] = 3.1415; 30 | coll["an arbitrary number"] = 4983.223; 31 | coll["Null"] = 0; 32 | 33 | /* print all elements 34 | * - iterate over all elements 35 | * - element member first is the key 36 | * - element member second is the value 37 | */ 38 | StringFloatMap::iterator pos; 39 | for (pos = coll.begin(); pos != coll.end(); ++pos) { 40 | cout << "key: \"" << pos->first << "\" " 41 | << "value: " << pos->second << endl; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /stl/mmap1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | // type of the collection 19 | typedef multimap IntStringMMap; 20 | 21 | IntStringMMap coll; // container for int/string values 22 | 23 | // insert some elements in arbitrary order 24 | // - a value with key 1 gets inserted twice 25 | coll.insert(make_pair(5,"tagged")); 26 | coll.insert(make_pair(2,"a")); 27 | coll.insert(make_pair(1,"this")); 28 | coll.insert(make_pair(4,"of")); 29 | coll.insert(make_pair(6,"strings")); 30 | coll.insert(make_pair(1,"is")); 31 | coll.insert(make_pair(3,"multimap")); 32 | 33 | /* print all element values 34 | * - iterate over all elements 35 | * - element member second is the value 36 | */ 37 | IntStringMMap::iterator pos; 38 | for (pos = coll.begin(); pos != coll.end(); ++pos) { 39 | cout << pos->second << ' '; 40 | } 41 | cout << endl; 42 | } 43 | -------------------------------------------------------------------------------- /stl/print.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | 13 | /* PRINT_ELEMENTS() 14 | * - prints optional C-string optcstr followed by 15 | * - all elements of the collection coll 16 | * - separated by spaces 17 | */ 18 | template 19 | inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") 20 | { 21 | typename T::const_iterator pos; 22 | 23 | std::cout << optcstr; 24 | for (pos=coll.begin(); pos!=coll.end(); ++pos) { 25 | std::cout << *pos << ' '; 26 | } 27 | std::cout << std::endl; 28 | } 29 | -------------------------------------------------------------------------------- /stl/remove1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | list coll; 20 | 21 | // insert elements from 6 to 1 and 1 to 6 22 | for (int i=1; i<=6; ++i) { 23 | coll.push_front(i); 24 | coll.push_back(i); 25 | } 26 | 27 | // print all elements of the collection 28 | cout << "pre: "; 29 | copy (coll.begin(), coll.end(), // source 30 | ostream_iterator(cout," ")); // destination 31 | cout << endl; 32 | 33 | // remove all elements with value 3 34 | remove (coll.begin(), coll.end(), // range 35 | 3); // value 36 | 37 | // print all elements of the collection 38 | cout << "post: "; 39 | copy (coll.begin(), coll.end(), // source 40 | ostream_iterator(cout," ")); // destination 41 | cout << endl; 42 | } 43 | -------------------------------------------------------------------------------- /stl/remove3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | set coll; 20 | 21 | // insert elements from 1 to 9 22 | for (int i=1; i<=9; ++i) { 23 | coll.insert(i); 24 | } 25 | 26 | // print all elements of the collection 27 | copy (coll.begin(), coll.end(), 28 | ostream_iterator(cout," ")); 29 | cout << endl; 30 | 31 | /* Remove all elements with value 3 32 | * - algorithm remove() does not work 33 | * - instead member function erase() works 34 | */ 35 | int num = coll.erase(3); 36 | 37 | // print number of removed elements 38 | cout << "number of removed elements: " << num << endl; 39 | 40 | // print all elements of the modified collection 41 | copy (coll.begin(), coll.end(), 42 | ostream_iterator(cout," ")); 43 | cout << endl; 44 | } 45 | -------------------------------------------------------------------------------- /stl/remove4.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | list coll; 19 | 20 | // insert elements from 6 to 1 and 1 to 6 21 | for (int i=1; i<=6; ++i) { 22 | coll.push_front(i); 23 | coll.push_back(i); 24 | } 25 | 26 | // remove all elements with value 3 27 | // - poor performance 28 | coll.erase (remove(coll.begin(),coll.end(), 29 | 3), 30 | coll.end()); 31 | 32 | // remove all elements with value 4 33 | // - good performance 34 | coll.remove (4); 35 | } 36 | -------------------------------------------------------------------------------- /stl/riter1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | vector coll; 20 | 21 | // insert elements from 1 to 9 22 | for (int i=1; i<=9; ++i) { 23 | coll.push_back(i); 24 | } 25 | 26 | // print all element in reverse order 27 | copy (coll.rbegin(), coll.rend(), // source 28 | ostream_iterator(cout," ")); // destination 29 | cout << endl; 30 | } 31 | -------------------------------------------------------------------------------- /stl/set1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | 14 | int main() 15 | { 16 | // type of the collection 17 | typedef std::set IntSet; 18 | 19 | IntSet coll; // set container for int values 20 | 21 | /* insert elements from 1 to 6 in arbitrary order 22 | * - value 1 gets inserted twice 23 | */ 24 | coll.insert(3); 25 | coll.insert(1); 26 | coll.insert(5); 27 | coll.insert(4); 28 | coll.insert(1); 29 | coll.insert(6); 30 | coll.insert(2); 31 | 32 | /* print all elements 33 | * - iterate over all elements 34 | */ 35 | IntSet::const_iterator pos; 36 | for (pos = coll.begin(); pos != coll.end(); ++pos) { 37 | std::cout << *pos << ' '; 38 | } 39 | std::cout << std::endl; 40 | } 41 | -------------------------------------------------------------------------------- /stl/transform1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include "print.hpp" 16 | 17 | int square (int value) 18 | { 19 | return value*value; 20 | } 21 | 22 | int main() 23 | { 24 | std::set coll1; 25 | std::vector coll2; 26 | 27 | // insert elements from 1 to 9 into coll1 28 | for (int i=1; i<=9; ++i) { 29 | coll1.insert(i); 30 | } 31 | PRINT_ELEMENTS(coll1,"initialized: "); 32 | 33 | // transform each element from coll1 to coll2 34 | // - square transformed values 35 | std::transform (coll1.begin(),coll1.end(), // source 36 | std::back_inserter(coll2), // destination 37 | square); // operation 38 | 39 | PRINT_ELEMENTS(coll2,"squared: "); 40 | } 41 | -------------------------------------------------------------------------------- /stl/vector1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | vector coll; // vector container for integer elements 18 | 19 | // append elements with values 1 to 6 20 | for (int i=1; i<=6; ++i) { 21 | coll.push_back(i); 22 | } 23 | 24 | // print all elements followed by a space 25 | for (int i=0; i 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | // create a string 20 | string s("The zip code of Hondelage in Germany is 38108"); 21 | cout << "original: " << s << endl; 22 | 23 | // lowercase all characters 24 | transform (s.begin(), s.end(), // source 25 | s.begin(), // destination 26 | tolower); // operation 27 | cout << "lowered: " << s << endl; 28 | 29 | // uppercase all characters 30 | transform (s.begin(), s.end(), // source 31 | s.begin(), // destination 32 | toupper); // operation 33 | cout << "uppered: " << s << endl; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /string/iter3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | // create constant string 19 | const string hello("Hello, how are you?"); 20 | 21 | // initialize string s with all characters of string hello 22 | string s(hello.begin(),hello.end()); 23 | 24 | // iterate through all of the characters 25 | string::iterator pos; 26 | for (pos = s.begin(); pos != s.end(); ++pos) { 27 | cout << *pos; 28 | } 29 | cout << endl; 30 | 31 | // reverse the order of all characters inside the string 32 | reverse (s.begin(), s.end()); 33 | cout << "reverse: " << s << endl; 34 | 35 | // sort all characters inside the string 36 | sort (s.begin(), s.end()); 37 | cout << "ordered: " << s << endl; 38 | 39 | /* remove adjacent duplicates 40 | * - unique() reorders and returns new end 41 | * - erase() shrinks accordingly 42 | */ 43 | s.erase (unique(s.begin(), 44 | s.end()), 45 | s.end()); 46 | cout << "no duplicates: " << s << endl; 47 | } 48 | -------------------------------------------------------------------------------- /string/string2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | int main (int argc, char** argv) 16 | { 17 | const string delims(" \t,.;"); 18 | string line; 19 | 20 | // for every line read successfully 21 | while (getline(cin,line)) { 22 | string::size_type begIdx, endIdx; 23 | 24 | // search beginning of the first word 25 | begIdx = line.find_first_not_of(delims); 26 | 27 | // while beginning of a word found 28 | while (begIdx != string::npos) { 29 | // search end of the actual word 30 | endIdx = line.find_first_of (delims, begIdx); 31 | if (endIdx == string::npos) { 32 | // end of word is end of line 33 | endIdx = line.length(); 34 | } 35 | 36 | // print characters in reverse order 37 | for (int i=endIdx-1; i>=static_cast(begIdx); --i) { 38 | cout << line[i]; 39 | } 40 | cout << ' '; 41 | 42 | // search beginning of the next word 43 | begIdx = line.find_first_not_of (delims, endIdx); 44 | } 45 | cout << endl; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /string/unique.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | using namespace std; 17 | 18 | class bothWhiteSpaces { 19 | private: 20 | const locale& loc; // locale 21 | public: 22 | /* constructor 23 | * - save the locale object 24 | */ 25 | bothWhiteSpaces (const locale& l) : loc(l) { 26 | } 27 | /* function call 28 | * - returns whether both characters are whitespaces 29 | */ 30 | bool operator() (char elem1, char elem2) { 31 | return isspace(elem1,loc) && isspace(elem2,loc); 32 | } 33 | }; 34 | 35 | int main() 36 | { 37 | string contents; 38 | 39 | // don't skip leading whitespaces 40 | cin.unsetf (ios::skipws); 41 | 42 | // read all characters while compressing whitespaces 43 | unique_copy(istream_iterator(cin), // beginning of source 44 | istream_iterator(), // end of source 45 | back_inserter(contents), // destination 46 | bothWhiteSpaces(cin.getloc())); // criterion for removing 47 | 48 | // process contents 49 | // - here: write it to the standard output 50 | cout << contents; 51 | } 52 | -------------------------------------------------------------------------------- /util/Makefile: -------------------------------------------------------------------------------- 1 | OUTPROGS = autoptr1 autoptr2 limits1 minmax1 2 | 3 | HEADERS = autoptr.hpp defalloc.hpp 4 | 5 | include ../Makefile.h 6 | 7 | -------------------------------------------------------------------------------- /util/autoptr1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | /* define output operator for auto_ptr 16 | * - print object value or NULL 17 | */ 18 | template 19 | ostream& operator<< (ostream& strm, const auto_ptr& p) 20 | { 21 | // does p own an object ? 22 | if (p.get() == NULL) { 23 | strm << "NULL"; // NO: print NULL 24 | } 25 | else { 26 | strm << *p; // YES: print the object 27 | } 28 | return strm; 29 | } 30 | 31 | int main() 32 | { 33 | auto_ptr p(new int(42)); 34 | auto_ptr q; 35 | 36 | cout << "after initialization:" << endl; 37 | cout << " p: " << p << endl; 38 | cout << " q: " << q << endl; 39 | 40 | q = p; 41 | cout << "after assigning auto pointers:" << endl; 42 | cout << " p: " << p << endl; 43 | cout << " q: " << q << endl; 44 | 45 | *q += 13; // change value of the object q owns 46 | p = q; 47 | cout << "after change and reassignment:" << endl; 48 | cout << " p: " << p << endl; 49 | cout << " q: " << q << endl; 50 | } 51 | -------------------------------------------------------------------------------- /util/autoptr2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | using namespace std; 14 | 15 | /* define output operator for auto_ptr 16 | * - print object value or NULL 17 | */ 18 | template 19 | ostream& operator<< (ostream& strm, const auto_ptr& p) 20 | { 21 | // does p own an object ? 22 | if (p.get() == NULL) { 23 | strm << "NULL"; // NO: print NULL 24 | } 25 | else { 26 | strm << *p; // YES: print the object 27 | } 28 | return strm; 29 | } 30 | 31 | int main() 32 | { 33 | const auto_ptr p(new int(42)); 34 | const auto_ptr q(new int(0)); 35 | const auto_ptr r; 36 | 37 | cout << "after initialization:" << endl; 38 | cout << " p: " << p << endl; 39 | cout << " q: " << q << endl; 40 | cout << " r: " << r << endl; 41 | 42 | *q = *p; 43 | // *r = *p; // ERROR: undefined behavior 44 | *p = -77; 45 | cout << "after assigning values:" << endl; 46 | cout << " p: " << p << endl; 47 | cout << " q: " << q << endl; 48 | cout << " r: " << r << endl; 49 | 50 | // q = p; // ERROR at compile time 51 | // r = p; // ERROR at compile time 52 | } 53 | -------------------------------------------------------------------------------- /util/limits1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | // use textual representation for bool 19 | cout << boolalpha; 20 | 21 | // print maximum of integral types 22 | cout << "max(short): " << numeric_limits::max() << endl; 23 | cout << "max(int): " << numeric_limits::max() << endl; 24 | cout << "max(long): " << numeric_limits::max() << endl; 25 | cout << endl; 26 | 27 | // print maximum of floating-point types 28 | cout << "max(float): " 29 | << numeric_limits::max() << endl; 30 | cout << "max(double): " 31 | << numeric_limits::max() << endl; 32 | cout << "max(long double): " 33 | << numeric_limits::max() << endl; 34 | cout << endl; 35 | 36 | // print whether char is signed 37 | cout << "is_signed(char): " 38 | << numeric_limits::is_signed << endl; 39 | cout << endl; 40 | 41 | // print whether numeric limits for type string exist 42 | cout << "is_specialized(string): " 43 | << numeric_limits::is_specialized << endl; 44 | } 45 | -------------------------------------------------------------------------------- /util/minmax1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 1999 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 1999. 6 | * Permission to copy, use, modify, sell and distribute this software 7 | * is granted provided this copyright notice appears in all copies. 8 | * This software is provided "as is" without express or implied 9 | * warranty, and with no claim as to its suitability for any purpose. 10 | */ 11 | #include 12 | using namespace std; 13 | 14 | /* function that compares two pointers by comparing the values to which they point 15 | */ 16 | bool int_ptr_less (int* a, int* b) 17 | { 18 | return *a < *b; 19 | } 20 | 21 | int main() 22 | { 23 | int x = 17; 24 | int y = 42; 25 | int* px = &x; 26 | int* py = &y; 27 | int* pmax; 28 | 29 | // call max() with special comparison function 30 | pmax = max (px, py, int_ptr_less); 31 | //... 32 | } 33 | --------------------------------------------------------------------------------