├── .gitignore ├── .travis.yml ├── CMakeLists.txt ├── LICENSE.md ├── README ├── README.md ├── algo ├── accumulate1.cpp ├── adjacentdiff1.cpp ├── adjacentfind1.cpp ├── algostuff.hpp ├── allanynone1.cpp ├── binarysearch1.cpp ├── bounds1.cpp ├── copy1.cpp ├── copy2.cpp ├── copy3.cpp ├── count1.cpp ├── equal1.cpp ├── equalrange1.cpp ├── fill1.cpp ├── find1.cpp ├── find2.cpp ├── findend1.cpp ├── findof1.cpp ├── foreach1.cpp ├── foreach2.cpp ├── foreach3.cpp ├── generate1.cpp ├── heap1.cpp ├── includes1.cpp ├── innerproduct1.cpp ├── inplacemerge1.cpp ├── iota1.cpp ├── isheap1.cpp ├── ispartitioned1.cpp ├── ispermutation1.cpp ├── issorted1.cpp ├── lexicocmp1.cpp ├── merge1.cpp ├── minmax1.cpp ├── mismatch1.cpp ├── move1.cpp ├── nthelement1.cpp ├── partialsort1.cpp ├── partialsort2.cpp ├── partialsum1.cpp ├── partition1.cpp ├── partitioncopy1.cpp ├── permutation1.cpp ├── quicksort.cpp ├── randomshuffle1.cpp ├── relabs1.cpp ├── remove1.cpp ├── remove2.cpp ├── replace1.cpp ├── replace2.cpp ├── reverse1.cpp ├── rotate1.cpp ├── rotate2.cpp ├── search1.cpp ├── search2.cpp ├── searchn1.cpp ├── shuffle1.cpp ├── sort1.cpp ├── sort2.cpp ├── sorted1.cpp ├── swapranges1.cpp ├── transform1.cpp ├── transform2.cpp ├── unique1.cpp ├── unique2.cpp └── unique3.cpp ├── alloc ├── myalloc03.cpp ├── myalloc03.hpp ├── myalloc11.cpp └── myalloc11.hpp ├── concurrency ├── async1.cpp ├── async2.cpp ├── async3.cpp ├── atomics1.cpp ├── condvar1.cpp ├── condvar2.cpp ├── hardware1.cpp ├── mutex1.cpp ├── promise1.cpp ├── sharedfuture1.cpp ├── thread1.cpp └── thread2.cpp ├── cont ├── array1.cpp ├── buckets.hpp ├── cstylearray1.cpp ├── cstylearray1old.cpp ├── deque1.cpp ├── findbefore.hpp ├── forwardlist1.cpp ├── forwardlistfind1.cpp ├── forwardlistsplice1.cpp ├── hashfunc1.cpp ├── hashfunc2.cpp ├── hashval.hpp ├── list1.cpp ├── map1.cpp ├── map2.cpp ├── mapcmp1.cpp ├── mapfind1.cpp ├── multimap1.cpp ├── multiset1.cpp ├── newkey.hpp ├── print.hpp ├── refsem1.cpp ├── refwrap1.cpp ├── set1.cpp ├── setcmp1.cpp ├── setrange1.cpp ├── sortset.cpp ├── sortvec.cpp ├── unordinspect1.cpp ├── unordmultimap1.cpp ├── unordmultiset1.cpp ├── unordset1.cpp └── vector1.cpp ├── contadapt ├── Queue.hpp ├── Stack.hpp ├── bitset1.cpp ├── bitset2.cpp ├── priorityqueue1.cpp ├── queue1.cpp ├── queue2.cpp ├── stack1.cpp └── stack2.cpp ├── fo ├── bind1.cpp ├── bind2.cpp ├── compose3.cpp ├── fopow.hpp ├── fopow1.cpp ├── foreach3.cpp ├── lambda1.cpp ├── lambda2.cpp ├── lambda3.cpp ├── lambda4.cpp ├── lambda5.cpp ├── lambda6.cpp ├── print.hpp ├── removeif1.cpp ├── sequence1.cpp ├── sequence2.cpp └── sort1.cpp ├── i18n ├── germanbool.cpp ├── loc1.cpp ├── loc2.cpp ├── moneymanipulator.cpp ├── moneypunct.cpp ├── numget.cpp ├── numput.cpp ├── timeget.cpp ├── timeput.cpp ├── wbuffer.cpp ├── wstring2string.cpp ├── wstring2string.hpp ├── wstring2utf8.cpp └── wstring2utf8.hpp ├── io ├── cat1.cpp ├── cat2.cpp ├── charcat1.cpp ├── charcat2.cpp ├── copy1.cpp ├── copy2.cpp ├── countlines1.cpp ├── frac1in.hpp ├── frac1out.hpp ├── frac2in.hpp ├── frac2out.hpp ├── fstream1.cpp ├── fstream2.cpp ├── ignore1.cpp ├── ignore1.hpp ├── ignore2.cpp ├── ignore2.hpp ├── inbuf1.cpp ├── inbuf1.hpp ├── io1.cpp ├── outbuf1.cpp ├── outbuf1.hpp ├── outbuf1i18n.cpp ├── outbuf1i18n.hpp ├── outbuf2.cpp ├── outbuf2.hpp ├── outbuf3.cpp ├── outbuf3.hpp ├── sstream1.cpp ├── sstream2.cpp ├── streambuffer1.cpp ├── streambuffer2.cpp ├── streamreadwrite1.cpp ├── streamredirect1.cpp ├── sum1.cpp ├── sum2.cpp └── timemanipulator1.cpp ├── iter ├── advance1.cpp ├── advance2.cpp ├── assoiter.hpp ├── assoiter1.cpp ├── backinserter1.cpp ├── distance1.cpp ├── frontinserter1.cpp ├── inserter1.cpp ├── istreamiter1.cpp ├── itercategory1.cpp ├── iterswap1.cpp ├── ostreamiter1.cpp ├── print.hpp ├── reviter1.cpp ├── reviter2.cpp ├── reviter3.cpp └── reviter4.cpp ├── lang └── lambda1.cpp ├── num ├── complex1.cpp ├── complex2.cpp ├── dist1.cpp ├── gslice1.cpp ├── indirectarray1.cpp ├── maskarray1.cpp ├── random1.cpp ├── random2.cpp ├── slice1.cpp ├── valarray1.cpp └── valarray2.cpp ├── regex ├── regex1.cpp ├── regex2.cpp ├── regex3.cpp ├── regex4.cpp ├── regex5.cpp ├── regexexception.hpp ├── regexiter1.cpp └── regextokeniter1.cpp ├── stl ├── add1.cpp ├── algo1.cpp ├── algo1old.cpp ├── array1.cpp ├── assoarray1.cpp ├── bind1.cpp ├── copy1.cpp ├── copy2.cpp ├── copybug.cpp ├── deque1.cpp ├── find1.cpp ├── fo1.cpp ├── foreach1.cpp ├── foreach2.cpp ├── forwardlist1.cpp ├── ioiter1.cpp ├── iterbug.cpp ├── lambda1.cpp ├── list1.cpp ├── list1old.cpp ├── list2.cpp ├── multimap1.cpp ├── multiset1.cpp ├── prime1.cpp ├── print.hpp ├── remove1.cpp ├── remove2.cpp ├── remove3.cpp ├── remove4.cpp ├── reviter1.cpp ├── set1.cpp ├── sort1.cpp ├── sort2.cpp ├── transform1.cpp ├── unordmap1.cpp ├── unordmultiset1.cpp ├── unordmultiset2.cpp └── vector1.cpp ├── string ├── icstring.hpp ├── icstring1.cpp ├── string1.cpp ├── string2.cpp ├── string3.cpp ├── stringiter1.cpp ├── stringiter2.cpp └── stringnumconv1.cpp └── util ├── chrono1.cpp ├── chrono2.cpp ├── clock.hpp ├── clock1.cpp ├── enableshared1.cpp ├── exception.hpp ├── limits1.cpp ├── minmax1.cpp ├── pair1.cpp ├── printtuple.hpp ├── ratio1.cpp ├── sharedptr1.cpp ├── sharedptr2.cpp ├── sharedptr3.cpp ├── timepoint.hpp ├── timepoint1.cpp ├── tuple1.cpp ├── tuple2.cpp ├── uniqueptr1.cpp ├── weakptr1.cpp └── weakptr2.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | build/* -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Ubuntu 18.04 (Bionic Beaver) support 2 | sudo: required 3 | dist: bionic 4 | 5 | language: cpp 6 | 7 | matrix: 8 | include: 9 | - compiler: gcc 10 | addons: 11 | apt: 12 | sources: 13 | - ubuntu-toolchain-r-test 14 | packages: 15 | - g++-7 16 | env: COMPILER=g++-7 17 | - compiler: clang 18 | addons: 19 | apt: 20 | sources: 21 | - ubuntu-toolchain-r-test 22 | - llvm-toolchain-bionic-8 23 | packages: 24 | - clang++-8 25 | env: COMPILER=clang++-8 26 | before_install: 27 | # - sudo add-apt-repository -y ppa:george-edison55/cmake-3.x 28 | - sudo apt-get update -qq 29 | 30 | install: 31 | # - sudo apt-get install -y cmake 32 | 33 | script: 34 | - mkdir build 35 | - cd build 36 | - cmake --version 37 | - cmake -DCMAKE_CXX_COMPILER=$COMPILER .. && make -j4 38 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Henry Zhang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | These are the examples from the following book: 2 | 3 | Nicolai M. Josuttis 4 | The C++ Standard Library - A Tutorial and Reference - 2nd Edition 5 | Addison Wesley Longman, 2012 6 | 7 | ISBN-13 978-0-321-62321-8 8 | ISBN-10 0-321-62321-5 9 | 10 | For further informations see: 11 | http://www.cppstdlib.com 12 | 13 | Copyright 2012 by Addison Wesley Longman, Inc. and Nicolai M. Josuttis. 14 | 15 | These examples are covered under the copyright and warranty notices printed in that book. However, the publisher grants permission, without fee, for the code to be copied and used for personal and educational purposes, provided that the citation above, including the copyright notice, appears with each copy made. 16 | 17 | Note that any commercial use of this code requires the explicit written permission of the publisher. To obtain permission, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, USA, 18 | or you may fax your request to +1-201-236-3290. 19 | 20 | I welcome your feedback. 21 | The best way to reach me is by Email. 22 | See http://www.cppstdlib.com/feedback.html for details. 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://cloud.githubusercontent.com/assets/6889915/19406570/f04138e2-92ba-11e6-949f-b42755d98cb6.jpg) 2 | 3 | The C++ Standard Library - A Tutorial and Reference, 2nd Edition 4 | http://www.cppstdlib.com 5 | ================================================= 6 | 7 | [![Build Status](https://travis-ci.org/iZhangHui/cppstdlib.svg?branch=master)](https://travis-ci.org/iZhangHui/cppstdlib) ![Language](https://img.shields.io/badge/language-C%2B%2B11-orange.svg) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE.md) 8 | 9 | Installation 10 | ------------ 11 | ``` 12 | git clone https://github.com/iZhangHui/cppstdlib.git 13 | cd cppstdlib 14 | mkdir build && cd build 15 | cmake .. 16 | make -j4 17 | 18 | ``` 19 | -------------------------------------------------------------------------------- /algo/adjacentdiff1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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.cbegin(), coll.cend(), // source 23 | ostream_iterator(cout," ")); // destination 24 | cout << endl; 25 | 26 | // print all sums with the predecessors 27 | adjacent_difference (coll.cbegin(), coll.cend(), // source 28 | ostream_iterator(cout," "), // destination 29 | plus()); // operation 30 | cout << endl; 31 | 32 | // print all products between elements 33 | adjacent_difference (coll.cbegin(), coll.cend(), // source 34 | ostream_iterator(cout," "), // destination 35 | multiplies()); // operation 36 | cout << endl; 37 | } 38 | -------------------------------------------------------------------------------- /algo/allanynone1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | PRINT_ELEMENTS(coll,"coll: "); 21 | 22 | // define an object for the predicate (using a lambda) 23 | auto isEven = [](int elem) { 24 | return elem%2==0; 25 | }; 26 | 27 | // print whether all, any, or none of the elements are/is even 28 | cout << boolalpha << "all even?: " 29 | << all_of(coll.cbegin(),coll.cend(), isEven) << endl; 30 | cout << "any even?: " 31 | << any_of(coll.cbegin(),coll.cend(), isEven) << endl; 32 | cout << "none even?: " 33 | << none_of(coll.cbegin(),coll.cend(), isEven) << endl; 34 | } 35 | -------------------------------------------------------------------------------- /algo/binarysearch1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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.cbegin(), coll.cend(), 5)) { 23 | cout << "5 is present" << endl; 24 | } else { 25 | cout << "5 is not present" << endl; 26 | } 27 | 28 | // check existence of element with value 42 29 | if (binary_search(coll.cbegin(), coll.cend(), 42)) { 30 | cout << "42 is present" << endl; 31 | } else { 32 | cout << "42 is not present" << endl; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /algo/copy2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | vector source(10,'.'); 18 | for (int c='a'; c<='f'; c++) { 19 | source.push_back(c); 20 | } 21 | source.insert(source.end(),10,'.'); 22 | PRINT_ELEMENTS(source,"source: "); 23 | 24 | // copy all letters three elements in front of the 'a' 25 | vector c1(source.cbegin(),source.cend()); 26 | copy (c1.cbegin()+10, c1.cbegin()+16, // source range 27 | c1.begin()+7); // destination range 28 | PRINT_ELEMENTS(c1,"c1: "); 29 | 30 | // copy all letters three elements behind the 'f' 31 | vector c2(source.cbegin(),source.cend()); 32 | copy_backward (c2.cbegin()+10, c2.cbegin()+16, // source range 33 | c2.begin()+19); // destination range 34 | PRINT_ELEMENTS(c2,"c2: "); 35 | } 36 | -------------------------------------------------------------------------------- /algo/copy3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/equalrange1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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::const_iterator,list::const_iterator> range; 25 | range = equal_range (coll.cbegin(), coll.cend(), 26 | 5); 27 | 28 | cout << "5 could get position " 29 | << distance(coll.cbegin(),range.first) + 1 30 | << " up to " 31 | << distance(coll.cbegin(),range.second) + 1 32 | << " without breaking the sorting" << endl; 33 | } 34 | -------------------------------------------------------------------------------- /algo/find1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | list::iterator pos2; 31 | if (pos1 != coll.end()) { 32 | pos2 = find (++pos1, coll.end(), // range 33 | 4); // value 34 | } 35 | 36 | // print all elements from first to second 4 (both included) 37 | // - note: now we need the position of the first 4 again (if any) 38 | if (pos1!=coll.end() && pos2!=coll.end()) { 39 | copy (--pos1, ++pos2, 40 | ostream_iterator(cout," ")); 41 | cout << endl; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /algo/foreach1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | 20 | // call print() for each element 21 | for_each (coll.cbegin(), coll.cend(), // range 22 | [](int elem){ // operation 23 | cout << elem << ' '; 24 | }); 25 | cout << endl; 26 | } 27 | -------------------------------------------------------------------------------- /algo/foreach2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | 20 | // add 10 to each element 21 | for_each (coll.begin(), coll.end(), // range 22 | [](int& elem){ // operation 23 | elem += 10; 24 | }); 25 | PRINT_ELEMENTS(coll); 26 | 27 | // add value of first element to each element 28 | for_each (coll.begin(), coll.end(), // range 29 | [=](int& elem){ // operation 30 | elem += *coll.begin(); 31 | }); 32 | PRINT_ELEMENTS(coll); 33 | } 34 | -------------------------------------------------------------------------------- /algo/foreach3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/generate1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/includes1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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.cbegin(), coll.cend(), 29 | search.cbegin(), search.cend())) { 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/inplacemerge1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/iota1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | array coll; 17 | 18 | iota (coll.begin(), coll.end(), // destination range 19 | 42); // start value 20 | 21 | PRINT_ELEMENTS(coll,"coll: "); 22 | } 23 | -------------------------------------------------------------------------------- /algo/isheap1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 = { 9, 8, 7, 7, 7, 5, 4, 2, 1 }; 17 | vector coll2 = { 5, 3, 2, 1, 4, 7, 9, 8, 6 }; 18 | PRINT_ELEMENTS(coll1,"coll1: "); 19 | PRINT_ELEMENTS(coll2,"coll2: "); 20 | 21 | // check whether the collections are heaps 22 | cout << boolalpha << "coll1 is heap: " 23 | << is_heap (coll1.cbegin(), coll1.cend()) << endl; 24 | cout << "coll2 is heap: " 25 | << is_heap (coll2.cbegin(), coll2.cend()) << endl; 26 | 27 | // print the first element that is not a heap in coll2 28 | auto pos = is_heap_until (coll2.cbegin(), coll2.cend()); 29 | if (pos != coll2.end()) { 30 | cout << "first non-heap element: " << *pos << endl; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /algo/ispartitioned1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 = { 5, 3, 9, 1, 3, 4, 8, 2, 6 }; 17 | PRINT_ELEMENTS(coll,"coll: "); 18 | 19 | // define predicate: check whether element is odd: 20 | auto isOdd = [](int elem) { 21 | return elem%2==1; 22 | }; 23 | 24 | // check whether coll is partitioned in odd and even elements 25 | if (is_partitioned(coll.cbegin(), coll.cend(), // range 26 | isOdd)) { // predicate 27 | cout << "coll is partitioned" << endl; 28 | 29 | // find first even element: 30 | // The iterator past the end of the first partition within [first, last) or last if all elements satisfy p. 31 | auto pos = partition_point(coll.cbegin(),coll.cend(), 32 | isOdd); 33 | cout << "first even element: " << *pos << endl; 34 | } else { 35 | cout << "coll is not partitioned" << 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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.cbegin(), coll1.cend(), 29 | coll2.cbegin(), coll2.cend(), 30 | ostream_iterator(cout," ")); 31 | cout << endl; 32 | } 33 | -------------------------------------------------------------------------------- /algo/partialsum1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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.cbegin(), coll.cend(), // source range 23 | ostream_iterator(cout," ")); // destination 24 | cout << endl; 25 | 26 | // print all partial products 27 | partial_sum (coll.cbegin(), coll.cend(), // source range 28 | ostream_iterator(cout," "), // destination 29 | multiplies()); // operation 30 | cout << endl; 31 | } 32 | -------------------------------------------------------------------------------- /algo/partitioncopy1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 = { 1, 6, 33, 7, 22, 4, 11, 33, 2, 7, 0, 42, 5 }; 17 | PRINT_ELEMENTS(coll,"coll: "); 18 | 19 | // destination collections: 20 | vector evenColl; 21 | vector oddColl; 22 | 23 | // copy all elements partitioned accordingly into even and odd elements 24 | partition_copy (coll.cbegin(),coll.cend(), // source range 25 | back_inserter(evenColl), // destination for even elements 26 | back_inserter(oddColl), // destination for odd elements 27 | [](int elem){ // predicate: check for even elements 28 | return elem%2==0; 29 | }); 30 | 31 | PRINT_ELEMENTS(evenColl,"evenColl: "); 32 | PRINT_ELEMENTS(oddColl, "oddColl: "); 33 | } 34 | -------------------------------------------------------------------------------- /algo/randomshuffle1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 elements with self-written random-number generator 33 | MyRandom rd; 34 | random_shuffle (coll.begin(), coll.end(), // range 35 | rd); // random-number generator 36 | 37 | PRINT_ELEMENTS(coll,"shuffled: "); 38 | } 39 | -------------------------------------------------------------------------------- /algo/relabs1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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, -3, 22, 13, 13, -9 }; 17 | PRINT_ELEMENTS(coll,"coll: "); 18 | 19 | // convert into relative values 20 | adjacent_difference (coll.cbegin(), coll.cend(), // source 21 | coll.begin()); // destination 22 | PRINT_ELEMENTS(coll,"relative: "); 23 | 24 | // convert into absolute values 25 | partial_sum (coll.cbegin(), coll.cend(), // source 26 | coll.begin()); // destination 27 | PRINT_ELEMENTS(coll,"absolute: "); 28 | } 29 | -------------------------------------------------------------------------------- /algo/remove1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | [](int elem){ // remove criterion 37 | return elem<4; 38 | }), 39 | coll.end()); 40 | PRINT_ELEMENTS(coll,"<4 removed: "); 41 | } 42 | -------------------------------------------------------------------------------- /algo/replace1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | [](int elem){ // criterion for replacement 31 | return elem<5; 32 | }, 33 | 0); // new value 34 | PRINT_ELEMENTS(coll,"coll: "); 35 | } 36 | -------------------------------------------------------------------------------- /algo/reverse1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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.cbegin(), coll.cend(), // 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | INSERT_ELEMENTS(subcoll,3,6); 22 | 23 | PRINT_ELEMENTS(coll, "coll: "); 24 | PRINT_ELEMENTS(subcoll,"subcoll: "); 25 | 26 | // search first occurrence of subcoll in coll 27 | deque::iterator pos; 28 | pos = search (coll.begin(), coll.end(), // range 29 | subcoll.begin(), subcoll.end()); // subrange 30 | 31 | // loop while subcoll found as subrange of coll 32 | while (pos != coll.end()) { 33 | // print position of first element 34 | cout << "subcoll found starting with element " 35 | << distance(coll.begin(),pos) + 1 36 | << endl; 37 | 38 | // search next occurrence of subcoll 39 | ++pos; 40 | pos = search (pos, coll.end(), // range 41 | subcoll.begin(), subcoll.end()); // subrange 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /algo/shuffle1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "algostuff.hpp" 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | vector coll; 19 | 20 | INSERT_ELEMENTS(coll,1,9); 21 | PRINT_ELEMENTS(coll,"coll: "); 22 | 23 | // shuffle all elements randomly 24 | random_shuffle (coll.begin(), coll.end()); 25 | 26 | PRINT_ELEMENTS(coll,"shuffled: "); 27 | 28 | // sort them again 29 | sort (coll.begin(), coll.end()); 30 | PRINT_ELEMENTS(coll,"sorted: "); 31 | 32 | // shuffle elements with default engine 33 | default_random_engine dre; 34 | shuffle (coll.begin(), coll.end(), // range 35 | dre); // random-number generator 36 | 37 | PRINT_ELEMENTS(coll,"shuffled: "); 38 | } 39 | -------------------------------------------------------------------------------- /algo/sort1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/sort2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 lessLength (const string& s1, const string& s2) 15 | { 16 | return s1.length() < s2.length(); 17 | } 18 | 19 | int main() 20 | { 21 | // fill two collections with the same elements 22 | vector coll1 = { "1xxx", "2x", "3x", "4x", "5xx", "6xxxx", 23 | "7xx", "8xxx", "9xx", "10xxx", "11", "12", 24 | "13", "14xx", "15", "16", "17" }; 25 | vector coll2(coll1); 26 | 27 | PRINT_ELEMENTS(coll1,"on entry:\n "); 28 | 29 | // sort (according to the length of the strings) 30 | sort (coll1.begin(), coll1.end(), // range 31 | lessLength); // criterion 32 | stable_sort (coll2.begin(), coll2.end(), // range 33 | lessLength); // criterion 34 | 35 | PRINT_ELEMENTS(coll1,"\nwith sort():\n "); 36 | PRINT_ELEMENTS(coll2,"\nwith stable_sort():\n "); 37 | } 38 | -------------------------------------------------------------------------------- /algo/swapranges1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/unique3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | unique_copy(istream_iterator(cin), // beginning of source: cin 29 | istream_iterator(), // end of source: end-of-file 30 | ostream_iterator(cout), // destination: cout 31 | bothSpaces); // duplicate criterion 32 | } 33 | -------------------------------------------------------------------------------- /alloc/myalloc03.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "myalloc03.hpp" 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | int main() 18 | { 19 | // a vector with special allocator 20 | std::vector > v; 21 | 22 | // an int/float map with special allocator 23 | std::map, 24 | MyAlloc > > m; 25 | 26 | // a string with special allocator 27 | std::basic_string,MyAlloc > s; 28 | 29 | 30 | // special string type that uses special allocator 31 | typedef std::basic_string, 32 | MyAlloc > MyString; 33 | 34 | // special string/string map type that uses special allocator 35 | typedef std::map, 36 | MyAlloc > > MyMap; 37 | // create object of this type 38 | MyMap mymap; 39 | //... 40 | } 41 | -------------------------------------------------------------------------------- /alloc/myalloc11.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "myalloc11.hpp" 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | int main() 18 | { 19 | // a vector with special allocator 20 | std::vector> v; 21 | 22 | // an int/float map with special allocator 23 | std::map, 24 | MyAlloc>> m; 25 | 26 | // a string with special allocator 27 | std::basic_string,MyAlloc> s; 28 | 29 | 30 | // special string type that uses special allocator 31 | typedef std::basic_string, 32 | MyAlloc> MyString; 33 | 34 | // special string/string map type that uses special allocator 35 | typedef std::map, 36 | MyAlloc>> MyMap; 37 | // create object of this type 38 | MyMap mymap; 39 | //... 40 | } 41 | -------------------------------------------------------------------------------- /concurrency/hardware1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main(int argc, char const *argv[]) 6 | { 7 | auto hardware_threads = thread::hardware_concurrency(); 8 | cout << "Hardware processors = " << hardware_threads << endl; 9 | return 0; 10 | } -------------------------------------------------------------------------------- /concurrency/mutex1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | std::mutex printMutex; // enable synchronized output with print() 17 | 18 | void print(const std::string& s) 19 | { 20 | std::lock_guard l(printMutex); 21 | for (char c : s) { 22 | std::cout.put(c); 23 | } 24 | std::cout << std::endl; 25 | } 26 | 27 | int main() 28 | { 29 | auto f1 = std::async (std::launch::async, 30 | print, "Hello from a first thread"); 31 | auto f2 = std::async (std::launch::async, 32 | print, "Hello from a second thread"); 33 | print("Hello from the main thread"); 34 | } 35 | -------------------------------------------------------------------------------- /concurrency/thread2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | void doSomething(int num, char c) 10 | { 11 | try { 12 | // random-number generator (use c as seed to get different sequences) 13 | default_random_engine dre(42*c); 14 | uniform_int_distribution id(10, 1000); 15 | for (int i = 0; i < num; ++i) { 16 | this_thread::sleep_for(chrono::milliseconds(id(dre))); 17 | cout.put(c).flush(); 18 | } 19 | // make sure no exception leaves the thread and terminates the program 20 | } catch (const exception& e) { 21 | cerr << "THREAD-EXCEPTION (thread " 22 | << this_thread::get_id() << "): " << e.what() << endl; 23 | } catch (...) { 24 | cerr << "THREAD-EXCEPTION (thread " 25 | << this_thread::get_id() << ")" << endl; 26 | } 27 | } 28 | 29 | int main() 30 | { 31 | thread t1(doSomething, 5, '.'); 32 | thread t2(doSomething, 5, '+'); 33 | thread t3(doSomething, 5, '*'); 34 | 35 | cout << "t3 ID: " << t3.get_id() << endl; 36 | cout << "main ID: " << this_thread::get_id() << endl; 37 | cout << "nothread ID: " << thread::id() << endl; 38 | 39 | t1.join(); 40 | t2.join(); 41 | t3.join(); 42 | 43 | } 44 | 45 | -------------------------------------------------------------------------------- /cont/array1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | using namespace std; 17 | 18 | int main() 19 | { 20 | // create array with 10 ints 21 | array a = { 11, 22, 33, 44 }; 22 | 23 | PRINT_ELEMENTS(a); 24 | 25 | // modify last two elements 26 | a.back() = 9999999; 27 | a[a.size()-2] = 42; 28 | PRINT_ELEMENTS(a); 29 | 30 | // process sum of all elements 31 | cout << "sum: " 32 | << accumulate(a.begin(),a.end(),0) 33 | << endl; 34 | 35 | // negate all elements 36 | transform(a.begin(),a.end(), // source 37 | a.begin(), // destination 38 | negate()); // operation 39 | PRINT_ELEMENTS(a); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /cont/cstylearray1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | int main() 16 | { 17 | int vals[] = { 33, 67, -4, 13, 5, 2 }; 18 | 19 | // use begin() and end() for ordinary C arrays 20 | std::vector v(std::begin(vals), std::end(vals)); 21 | 22 | // use global begin() and end() for containers: 23 | std::copy (std::begin(v), std::end(v), 24 | std::ostream_iterator(std::cout," ")); 25 | std::cout << std::endl; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /cont/cstylearray1old.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/deque1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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.cbegin(), coll.cend(), 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/findbefore.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 InputIterator 13 | find_before (InputIterator first, InputIterator last, const Tp& val) 14 | { 15 | if (first==last) { 16 | return first; 17 | } 18 | InputIterator next(first); 19 | ++next; 20 | while (next!=last && !(*next==val)) { 21 | ++next; 22 | ++first; 23 | } 24 | return first; 25 | } 26 | 27 | template 28 | inline InputIterator 29 | find_before_if (InputIterator first, InputIterator last, Pred pred) 30 | { 31 | if (first==last) { 32 | return first; 33 | } 34 | InputIterator next(first); 35 | ++next; 36 | while (next!=last && !pred(*next)) { 37 | ++next; 38 | ++first; 39 | } 40 | return first; 41 | } 42 | -------------------------------------------------------------------------------- /cont/forwardlistfind1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "print.hpp" 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | forward_list list = { 1, 2, 3, 4, 5, 97, 98, 99 }; 18 | 19 | // find the position before the first even element 20 | auto posBefore = list.before_begin(); 21 | for (auto pos=list.begin(); pos!=list.end(); ++pos, ++posBefore) { 22 | if (*pos % 2 == 0) { 23 | break; // element found 24 | } 25 | } 26 | 27 | // and insert a new element in front of the first even element 28 | list.insert_after(posBefore,42); 29 | PRINT_ELEMENTS(list); 30 | } 31 | -------------------------------------------------------------------------------- /cont/forwardlistsplice1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "print.hpp" 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | forward_list l1 = { 1, 2, 3, 4, 5 }; 18 | forward_list l2 = { 97, 98, 99 }; 19 | 20 | // find 3 in l1 21 | auto pos1=l1.before_begin(); 22 | for (auto pb1=l1.begin(); pb1 != l1.end(); ++pb1, ++pos1) { 23 | if (*pb1 == 3) { 24 | break; // found 25 | } 26 | } 27 | 28 | // find 99 in l2 29 | auto pos2=l2.before_begin(); 30 | for (auto pb2=l2.begin(); pb2 != l2.end(); ++pb2, ++pos2) { 31 | if (*pb2 == 99) { 32 | break; // found 33 | } 34 | } 35 | 36 | // splice 3 from l1 to l2 before 99 37 | l1.splice_after(pos2, l2, // destination 38 | pos1); // source 39 | 40 | PRINT_ELEMENTS(l1,"l1: "); 41 | PRINT_ELEMENTS(l2,"l2: "); 42 | } 43 | -------------------------------------------------------------------------------- /cont/hashval.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // from boost (functional/hash): 14 | // see http://www.boost.org/doc/libs/1_35_0/doc/html/hash/combine.html 15 | template 16 | inline void hash_combine (std::size_t& seed, const T& val) 17 | { 18 | seed ^= std::hash()(val) + 0x9e3779b9 + (seed<<6) + (seed>>2); 19 | } 20 | 21 | // auxiliary generic functions to create a hash value using a seed 22 | template 23 | inline void hash_val (std::size_t& seed, const T& val) 24 | { 25 | hash_combine(seed,val); 26 | } 27 | template 28 | inline void hash_val (std::size_t& seed, 29 | const T& val, const Types&... args) 30 | { 31 | hash_combine(seed,val); 32 | hash_val(seed,args...); 33 | } 34 | 35 | // auxiliary generic function to create a hash value out of a heterogeneous list of arguments 36 | template 37 | inline std::size_t hash_val (const Types&... args) 38 | { 39 | std::size_t seed = 0; 40 | hash_val (seed, args...); 41 | return seed; 42 | } 43 | -------------------------------------------------------------------------------- /cont/map1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | map coll { { "tim", 9.9 }, 20 | { "struppi", 11.77 } 21 | } ; 22 | 23 | // square the value of each element: 24 | for_each (coll.begin(), coll.end(), 25 | [] (pair& elem) { 26 | elem.second *= elem.second; 27 | }); 28 | 29 | // print each element: 30 | for_each (coll.begin(), coll.end(), 31 | [] (const map::value_type& elem) { 32 | cout << elem.first << ": " << elem.second << endl; 33 | }); 34 | } 35 | -------------------------------------------------------------------------------- /cont/newkey.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/print.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // PRINT_ELEMENTS() 15 | // - prints optional string optstr followed by 16 | // - all elements of the collection coll 17 | // - in one line, separated by spaces 18 | template 19 | inline void PRINT_ELEMENTS (const T& coll, 20 | const std::string& optstr="") 21 | { 22 | std::cout << optstr; 23 | for (const auto& elem : coll) { 24 | std::cout << elem << ' '; 25 | } 26 | std::cout << std::endl; 27 | } 28 | -------------------------------------------------------------------------------- /cont/setrange1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | set coll((istream_iterator(cin)), 23 | istream_iterator()); 24 | 25 | // print all elements 26 | copy (coll.cbegin(), coll.cend(), 27 | ostream_iterator(cout, "\n")); 28 | } 29 | -------------------------------------------------------------------------------- /cont/sortvec.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | vector coll((istream_iterator(cin)), 23 | istream_iterator()); 24 | 25 | // sort elements 26 | sort (coll.begin(), coll.end()); 27 | 28 | // print all elements ignoring subsequent duplicates 29 | unique_copy (coll.cbegin(), coll.cend(), 30 | ostream_iterator(cout, "\n")); 31 | } 32 | -------------------------------------------------------------------------------- /cont/unordinspect1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "buckets.hpp" 14 | 15 | int main() 16 | { 17 | // create and initialize an unordered set 18 | std::unordered_set intset = { 1,2,3,5,7,11,13,17,19 }; 19 | printHashTableState(intset); 20 | 21 | // insert some additional values (might cause rehashing) 22 | intset.insert({-7,17,33,4}); 23 | printHashTableState(intset); 24 | } 25 | -------------------------------------------------------------------------------- /cont/unordmultimap1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "buckets.hpp" 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | // create and initialize an unordered multimap as dictionary 21 | std::unordered_multimap dict = { 22 | {"day","Tag"}, 23 | {"strange","fremd"}, 24 | {"car","Auto"}, 25 | {"smart","elegant"}, 26 | {"trait","Merkmal"}, 27 | {"strange","seltsam"} 28 | }; 29 | printHashTableState(dict); 30 | 31 | // insert some additional values (might cause rehashing) 32 | dict.insert({{"smart","raffiniert"}, 33 | {"smart","klug"}, 34 | {"clever","raffiniert"} 35 | }); 36 | printHashTableState(dict); 37 | 38 | // modify maximum load factor (might cause rehashing) 39 | dict.max_load_factor(0.7); 40 | printHashTableState(dict); 41 | } 42 | -------------------------------------------------------------------------------- /cont/unordmultiset1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "print.hpp" 13 | using namespace std; 14 | 15 | int main() 16 | { 17 | // create and initialize, expand, and print unordered multiset 18 | unordered_multiset coll = { 1,2,3,5,7,11,13,17,19,77 }; 19 | coll.insert({-7,17,33,-11,17,19,1,13}); 20 | PRINT_ELEMENTS(coll); 21 | 22 | // remove all elements with specific value 23 | coll.erase(17); 24 | 25 | // remove one of the elements with specific value 26 | auto pos = coll.find(13); 27 | if (pos != coll.end()) { 28 | coll.erase(pos); 29 | } 30 | PRINT_ELEMENTS(coll); 31 | } 32 | -------------------------------------------------------------------------------- /contadapt/bitset1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | enum Color { red, yellow, green, blue, white, black, //..., 20 | numColors }; 21 | 22 | // create bitset for all bits/colors 23 | bitset usedColors; 24 | 25 | // set bits for two colors 26 | usedColors.set(red); 27 | usedColors.set(blue); 28 | 29 | // print some bitset data 30 | cout << "bitfield of used colors: " << usedColors << endl; 31 | cout << "number of used colors: " << usedColors.count() << endl; 32 | cout << "bitfield of unused colors: " << ~usedColors << endl; 33 | 34 | // if any color is used 35 | if (usedColors.any()) { 36 | // loop over all colors 37 | for (int c = 0; c < numColors; ++c) { 38 | // if the actual color is used 39 | if (usedColors[(Color)c]) { 40 | //... 41 | } 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /contadapt/bitset2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | cout << "267 as binary short: " 21 | << bitset::digits>(267) 22 | << endl; 23 | 24 | cout << "267 as binary long: " 25 | << bitset::digits>(267) 26 | << endl; 27 | 28 | cout << "10,000,000 with 24 bits: " 29 | << bitset<24>(1e7) << endl; 30 | 31 | // write binary representation into string 32 | string s = bitset<42>(12345678).to_string(); 33 | cout << "12,345,678 with 42 bits: " << s << endl; 34 | 35 | // transform binary representation into integral number 36 | cout << "\"1000101011\" as number: " 37 | << bitset<100>("1000101011").to_ullong() << endl; 38 | } 39 | -------------------------------------------------------------------------------- /contadapt/priorityqueue1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | -------------------------------------------------------------------------------- /contadapt/queue1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | -------------------------------------------------------------------------------- /contadapt/stack1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | -------------------------------------------------------------------------------- /contadapt/stack2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "Stack.hpp" // use special stack class 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | try { 19 | Stack st; 20 | 21 | // push three elements into the stack 22 | st.push(1); 23 | st.push(2); 24 | st.push(3); 25 | 26 | // pop and print two elements from the stack 27 | cout << st.pop() << ' '; 28 | cout << 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 three elements 41 | // - ERROR: one element too many 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/compose3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | using namespace std::placeholders; 18 | 19 | char myToupper(char c) 20 | { 21 | std::locale loc; 22 | return std::use_facet>(loc).toupper(c); 23 | } 24 | 25 | int main() 26 | { 27 | string s("Internationalization"); 28 | string sub("NaTiOn"); 29 | 30 | // search substring case insensitive 31 | string::iterator pos; 32 | pos = search(s.begin(),s.end(), // string to search in 33 | sub.begin(),sub.end(), // substring to search 34 | bind(equal_to(), // compar. criterion 35 | bind(myToupper,_1), 36 | bind(myToupper,_2))); 37 | if (pos != s.end()) { 38 | cout << "\"" << sub << "\" is part of \"" << s << "\"" 39 | << endl; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /fo/fopow.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | struct fopow 15 | { 16 | T1 operator()(T1 base, T2 exp) const { 17 | return std::pow(base, exp); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /fo/fopow1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "fopow.hpp" 17 | using namespace std; 18 | using namespace std::placeholders; 19 | 20 | int main() 21 | { 22 | vector coll = {1, 2, 3, 4, 5, 6, 7, 8, 9}; 23 | 24 | // print 3 raised to the power of all elements 25 | transform(coll.begin(), coll.end(), // source 26 | ostream_iterator(cout, " "), // destination 27 | bind(fopow(), 3, _1)); // operation 28 | cout << endl; 29 | 30 | // print all elements raised to the power of 3 31 | transform(coll.begin(), coll.end(), // source 32 | ostream_iterator(cout, " "), // destination 33 | bind(fopow(), _1, 3)); // operation 34 | cout << endl; 35 | } 36 | -------------------------------------------------------------------------------- /fo/foreach3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | public: 19 | // constructor 20 | MeanValue () : num(0), sum(0) { 21 | } 22 | 23 | // "function call" 24 | // - process one more element of the sequence 25 | void operator()(int elem) { 26 | ++num; // increment count 27 | sum += elem; // add value 28 | } 29 | 30 | // return mean value 31 | double value () { 32 | return static_cast(sum) / static_cast(num); 33 | } 34 | 35 | private: 36 | long num; // number of elements 37 | long sum; // sum of all element values 38 | }; 39 | 40 | int main() 41 | { 42 | vector coll = {1, 2, 3, 4, 5, 6, 7, 8}; 43 | 44 | // process and print mean value 45 | MeanValue mv = for_each(coll.begin(), coll.end(), // range 46 | MeanValue()); // operation 47 | cout << "mean value: " << mv.value() << endl; 48 | } 49 | -------------------------------------------------------------------------------- /fo/lambda1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | auto plus10 = [] (int i) { 17 | return i + 10; 18 | }; 19 | std::cout << "+10: " << plus10(7) << std::endl; 20 | 21 | auto plus10times2 = [] (int i) { 22 | return (i + 10) * 2; 23 | }; 24 | std::cout << "+10 *2: " << plus10times2(7) << std::endl; 25 | 26 | auto pow3 = [] (int i) { 27 | return i * i * i; 28 | }; 29 | std::cout << "x*x*x: " << pow3(7) << std::endl; 30 | 31 | auto inversDivide = [] (double d1, double d2) { 32 | return d2 / d1; 33 | }; 34 | std::cout << "invdiv: " << inversDivide(49,7) << std::endl; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /fo/lambda2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 = {1, 2, 3, 4, 5, 6, 7, 8}; 19 | 20 | // process and print mean value 21 | long sum = 0; 22 | for_each(coll.begin(), coll.end(), // range 23 | [&sum] (int elem) { 24 | sum += elem; 25 | }); 26 | 27 | double mv = static_cast(sum) / static_cast(coll.size()); 28 | 29 | cout << "mean value: " << mv << endl; 30 | } 31 | -------------------------------------------------------------------------------- /fo/lambda3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 20 | PRINT_ELEMENTS(coll,"coll: "); 21 | 22 | // remove third element 23 | list::iterator pos; 24 | int count=0; // call counter 25 | pos = remove_if(coll.begin(), coll.end(), // range 26 | [count] (int) mutable { // remove criterion 27 | return ++count == 3; 28 | }); 29 | coll.erase(pos,coll.end()); 30 | 31 | PRINT_ELEMENTS(coll,"3rd removed: "); 32 | } 33 | -------------------------------------------------------------------------------- /fo/lambda4.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | char myToupper(char c) 18 | { 19 | std::locale loc; 20 | return std::use_facet>(loc).toupper(c); 21 | } 22 | 23 | int main() 24 | { 25 | string s("Internationalization"); 26 | string sub("Nation"); 27 | 28 | // search substring case insensitive 29 | string::iterator pos; 30 | pos = search(s.begin(), s.end(), // string to search in 31 | sub.begin(),sub.end(), // substring to search 32 | [] (char c1, char c2) { // compar. criterion 33 | return myToupper(c1)==myToupper(c2); 34 | }); 35 | if (pos != s.end()) { 36 | cout << "\"" << sub << "\" is part of \"" << s << "\"" 37 | << endl; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /fo/print.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // PRINT_ELEMENTS() 15 | // - prints optional string optstr followed by 16 | // - all elements of the collection coll 17 | // - in one line, separated by spaces 18 | template 19 | inline void PRINT_ELEMENTS(const T& coll, 20 | const std::string& optstr="") 21 | { 22 | std::cout << optstr; 23 | for (const auto& elem : coll) { 24 | std::cout << elem << ' '; 25 | } 26 | std::cout << std::endl; 27 | } 28 | -------------------------------------------------------------------------------- /fo/removeif1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | public: 19 | Nth(int n) : nth(n), count(0) { 20 | } 21 | bool operator()(int) { 22 | return ++count == nth; 23 | } 24 | private: 25 | int nth; // call for which to return true 26 | int count; // call counter 27 | }; 28 | 29 | int main() 30 | { 31 | list coll = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 32 | PRINT_ELEMENTS(coll,"coll: "); 33 | 34 | // remove third element 35 | list::iterator pos; 36 | pos = remove_if(coll.begin(),coll.end(), // range 37 | Nth(3)); // remove criterion 38 | coll.erase(pos,coll.end()); 39 | 40 | PRINT_ELEMENTS(coll,"3rd removed: "); 41 | } 42 | -------------------------------------------------------------------------------- /fo/sequence1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | using namespace std; 17 | 18 | class IntSequence { 19 | public: 20 | IntSequence(int initialValue) // constructor 21 | : value(initialValue) { 22 | } 23 | 24 | int operator()() { // "function call" 25 | return value++; 26 | } 27 | private: 28 | int value; 29 | }; 30 | 31 | int main() 32 | { 33 | list coll; 34 | 35 | // insert values from 1 to 9 36 | generate_n(back_inserter(coll), // start 37 | 9, // number of elements 38 | IntSequence(1)); // generates values, starting with 1 39 | 40 | PRINT_ELEMENTS(coll); 41 | 42 | // replace second to last element but one with values starting at 42 43 | generate(next(coll.begin()), // start 44 | prev(coll.end()), // end 45 | IntSequence(42)); // generates values, starting with 42 46 | 47 | PRINT_ELEMENTS(coll); 48 | } 49 | -------------------------------------------------------------------------------- /i18n/germanbool.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 germanBoolNames : public std::numpunct_byname { 16 | public: 17 | germanBoolNames (const std::string& name) 18 | : std::numpunct_byname(name) { 19 | } 20 | protected: 21 | virtual std::string do_truename () const { 22 | return "wahr"; 23 | } 24 | virtual std::string do_falsename () const { 25 | return "falsch"; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | std::locale loc (std::locale(""), new germanBoolNames("")); 32 | std::cout.imbue(loc); 33 | std::cout << std::boolalpha << true << std::endl; 34 | } 35 | -------------------------------------------------------------------------------- /i18n/loc1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | try { 20 | // use classic C locale to read data from standard input 21 | cin.imbue(locale::classic()); 22 | 23 | // use a German locale to write data to standard output 24 | // - use different locale names for Windows and POSIX 25 | #ifdef _MSC_VER 26 | cout.imbue(locale("deu_deu.1252")); 27 | #else 28 | cout.imbue(locale("de_DE")); 29 | #endif 30 | 31 | // read and output floating-point values in a loop 32 | cout << "input floating-point values (classic notation): " << endl; 33 | double value; 34 | while (cin >> value) { 35 | cout << value << endl; 36 | } 37 | } 38 | catch (const std::exception& e) { 39 | cerr << "Exception: " << e.what() << endl; 40 | return EXIT_FAILURE; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /i18n/numput.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | try { 22 | // print floating-point value with the global classic locale: 23 | locale locC; 24 | cout.imbue(locC); 25 | use_facet>(locC).put (cout, cout, ' ', 26 | 1234.5678); 27 | cout << endl; 28 | 29 | // print floating-point value with German locale: 30 | #ifdef _MSC_VER 31 | locale locG("deu_deu.1252"); 32 | #else 33 | locale locG("de_DE"); 34 | #endif 35 | cout.imbue(locG); 36 | use_facet>(locG).put (cout, cout, ' ', 37 | 1234.5678); 38 | cout << endl; 39 | } 40 | catch (const std::exception& e) { 41 | cerr << "Exception: " << e.what() << endl; 42 | return EXIT_FAILURE; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /i18n/wbuffer.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 // std::cout, std::ios, std::wostream 13 | #include // std::codecvt_utf8 14 | #include // std::wbuffer_convert 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | // create input stream reading UTF-8 sequences: 20 | wbuffer_convert> utf8inBuf(cin.rdbuf()); 21 | wistream utf8in(&utf8inBuf); 22 | 23 | // create output stream writing UTF-16 sequences: 24 | wbuffer_convert> 27 | utf16outBuf(cout.rdbuf()); 28 | wostream utf16out(&utf16outBuf); 29 | 30 | // write each character read: 31 | wchar_t c; 32 | while (utf8in.get(c)) { 33 | utf16out.put(c); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /i18n/wstring2string.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "wstring2string.hpp" 14 | 15 | int main() 16 | { 17 | std::string s = "hello, world\n"; 18 | std::wstring ws = to_wstring(s); 19 | std::wcout << ws; 20 | std::cout << to_string(ws); 21 | } 22 | -------------------------------------------------------------------------------- /i18n/wstring2string.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // convert string to wstring 16 | std::wstring to_wstring (const std::string& str, 17 | const std::locale& loc = std::locale()) 18 | { 19 | std::vector buf(str.size()); 20 | std::use_facet>(loc).widen(str.data(), 21 | str.data()+str.size(), 22 | buf.data()); 23 | return std::wstring(buf.data(),buf.size()); 24 | } 25 | 26 | // convert wstring to string with '?' as default character 27 | std::string to_string (const std::wstring& str, 28 | const std::locale& loc = std::locale()) 29 | { 30 | std::vector buf(str.size()); 31 | std::use_facet>(loc).narrow(str.data(), 32 | str.data()+str.size(), 33 | '?', buf.data()); 34 | return std::string(buf.data(),buf.size()); 35 | } 36 | -------------------------------------------------------------------------------- /i18n/wstring2utf8.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // convert UTF-8 string to wstring 15 | std::wstring utf8_to_wstring (const std::string& str) 16 | { 17 | std::wstring_convert> myconv; 18 | return myconv.from_bytes(str); 19 | } 20 | 21 | // convert wstring to UTF-8 string 22 | std::string wstring_to_utf8 (const std::wstring& str) 23 | { 24 | std::wstring_convert> myconv; 25 | return myconv.to_bytes(str); 26 | } 27 | -------------------------------------------------------------------------------- /io/cat1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 filenames passed as command-line arguments 17 | // - open, print contents, and close file 18 | int main (int argc, char* argv[]) 19 | { 20 | ifstream file; 21 | 22 | // for all command-line arguments 23 | 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/countlines1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | return std::count(std::istreambuf_iterator(in), 19 | std::istreambuf_iterator(), 20 | '\n'); 21 | } 22 | 23 | int main (int argc, char* argv[]) 24 | { 25 | int count; 26 | 27 | if (argc == 1) { 28 | // no argument => count lines of standard input 29 | count = countLines(std::cin); 30 | } 31 | else { 32 | // count number of lines of all files passed as argument 33 | std::ifstream in; 34 | count = 0; 35 | for (int i=1; i 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | if (strm.peek() == '/') { 26 | strm.ignore(); 27 | strm >> d; 28 | } 29 | else { 30 | d = 1; 31 | } 32 | 33 | // if denominator is zero 34 | // - set failbit as I/O format error 35 | if (d == 0) { 36 | strm.setstate(std::ios::failbit); 37 | return strm; 38 | } 39 | 40 | // if everything is fine so far 41 | // - change the value of the fraction 42 | if (strm) { 43 | f = Fraction(n,d); 44 | } 45 | 46 | return strm; 47 | } 48 | -------------------------------------------------------------------------------- /io/frac2out.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | std::basic_ostringstream s; 24 | s.copyfmt(strm); 25 | s.width(0); 26 | 27 | // fill string stream 28 | s << f.numerator() << '/' << f.denominator(); 29 | 30 | // print string stream 31 | strm << s.str(); 32 | 33 | return strm; 34 | } 35 | -------------------------------------------------------------------------------- /io/fstream2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | int main() 16 | { 17 | // write string to a temporarily created file stream (since C++11) 18 | std::string s("hello"); 19 | std::ofstream("fstream2.tmp") << s << std::endl; 20 | 21 | // write C-string to a temporarily created file stream 22 | // - NOTE: wrote a pointer value before C++11 23 | std::ofstream("fstream2.tmp", std::ios::app) << "world" << std::endl; 24 | } 25 | -------------------------------------------------------------------------------- /io/ignore1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "ignore1.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/ignore1.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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(), 21 | strm.widen('\n')); 22 | 23 | // return stream for concatenation 24 | return strm; 25 | } 26 | -------------------------------------------------------------------------------- /io/ignore2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "ignore2.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(2); 30 | 31 | std::cout << "int: " << i << std::endl; 32 | 33 | std::cout << "read int: " << std::endl; 34 | std::cin >> i; 35 | std::cout << "int: " << i << std::endl; 36 | } 37 | -------------------------------------------------------------------------------- /io/ignore2.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | class ignoreLine 15 | { 16 | private: 17 | int num; 18 | public: 19 | explicit ignoreLine (int n=1) : num(n) { 20 | } 21 | 22 | template 23 | friend std::basic_istream& 24 | operator>> (std::basic_istream& strm, 25 | const ignoreLine& ign) 26 | { 27 | // skip until end-of-line num times 28 | for (int i=0; i::max(), 30 | strm.widen('\n')); 31 | } 32 | 33 | // return stream for concatenation 34 | return strm; 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /io/inbuf1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | cerr << "error while reading the first floating value" 28 | << endl; 29 | return EXIT_FAILURE; 30 | } 31 | 32 | // read second operand 33 | cout << "second operand: "; 34 | if (! (cin >> y)) { 35 | // input error 36 | // => error message and exit program with error status 37 | cerr << "error while reading the second floating value" 38 | << endl; 39 | return EXIT_FAILURE; 40 | } 41 | 42 | // print operands and result 43 | cout << x << " times " << y << " equals " << x * y << endl; 44 | } 45 | -------------------------------------------------------------------------------- /io/outbuf1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | virtual int_type overflow (int_type c) { 21 | if (c != EOF) { 22 | // convert lowercase to uppercase 23 | c = std::toupper(c,getloc()); 24 | 25 | // and write the character to the standard output 26 | if (std::putchar(c) == EOF) { 27 | return EOF; 28 | } 29 | } 30 | return c; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /io/outbuf1i18n.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "outbuf1i18n.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/outbuf2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/outbuf3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/sstream1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | -------------------------------------------------------------------------------- /io/sstream2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | tuple parseName(string name) 19 | { 20 | string s1, s2, s3; 21 | istringstream(name) >> s1 >> s2 >> s3; 22 | if (s3.empty()) { 23 | return tuple(move(s1),"",move(s2)); 24 | } 25 | else { 26 | return tuple(move(s1),move(s2),move(s3)); 27 | } 28 | } 29 | 30 | int main() 31 | { 32 | auto t1 = parseName("Nicolai M. Josuttis"); 33 | cout << "firstname: " << get<0>(t1) << endl; 34 | cout << "middle: " << get<1>(t1) << endl; 35 | cout << "lastname: " << get<2>(t1) << endl; 36 | auto t2 = parseName("Nico Josuttis"); 37 | cout << "firstname: " << get<0>(t2) << endl; 38 | cout << "middle: " << get<1>(t2) << endl; 39 | cout << "lastname: " << get<2>(t2) << endl; 40 | } 41 | -------------------------------------------------------------------------------- /io/streambuffer1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/streambuffer2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/streamreadwrite1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/streamredirect1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 redirect(ostream&); 17 | 18 | int main() 19 | { 20 | cout << "the first row" << endl; 21 | 22 | redirect(cout); 23 | 24 | cout << "the last row" << endl; 25 | } 26 | 27 | void redirect (ostream& strm) 28 | { 29 | // save output buffer of the stream 30 | // - use unique pointer with deleter that ensures to restore 31 | // the original output buffer at the end of the function 32 | auto del = [&](streambuf* p) { 33 | strm.rdbuf(p); 34 | }; 35 | unique_ptr origBuffer(strm.rdbuf(),del); 36 | 37 | // redirect ouput into the file redirect.txt 38 | ofstream file("redirect.txt"); 39 | strm.rdbuf (file.rdbuf()); 40 | 41 | file << "one row for the file" << endl; 42 | strm << "one row for the stream" << endl; 43 | } // closes file AND its buffer automatically 44 | -------------------------------------------------------------------------------- /io/timemanipulator1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // process and print current date and time: 21 | auto now = chrono::system_clock::now(); 22 | time_t t = chrono::system_clock::to_time_t(now); 23 | tm* nowTM = localtime(&t); 24 | cout << put_time(nowTM,"date: %x\ntime: %X\n") << endl; 25 | 26 | // read new time (same date) 27 | tm time(*nowTM); // copy date for new time 28 | cout << "new time [HH:MM]: "; 29 | cin >> get_time(&time,"%H:%M"); // read new time 30 | if (!cin) { 31 | cerr << "invalid format read" << endl; 32 | exit(EXIT_FAILURE); 33 | } 34 | 35 | // process difference in minutes: 36 | auto tp = chrono::system_clock::from_time_t(mktime(&time)); 37 | auto diff = chrono::duration_cast(tp-now); 38 | cout << "difference: " << diff.count() << " minutes" << endl; 39 | } 40 | -------------------------------------------------------------------------------- /iter/advance1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 1 to 9 22 | for (int i=1; i<=9; ++i) { 23 | coll.push_back(i); 24 | } 25 | 26 | list::iterator pos = coll.begin(); 27 | 28 | // print actual element 29 | cout << *pos << endl; 30 | 31 | // step three elements forward 32 | advance (pos, 3); 33 | 34 | // print actual element 35 | cout << *pos << endl; 36 | 37 | // step one element backward 38 | advance (pos, -1); 39 | 40 | // print actual element 41 | cout << *pos << endl; 42 | } 43 | -------------------------------------------------------------------------------- /iter/advance2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | while (cinPos != istream_iterator()) { 25 | // ignore the following two strings 26 | advance (cinPos, 2); 27 | 28 | // read and write the third string 29 | if (cinPos != istream_iterator()) { 30 | *coutPos++ = *cinPos++; 31 | } 32 | } 33 | cout << endl; 34 | } 35 | -------------------------------------------------------------------------------- /iter/assoiter1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "assoiter.hpp" 17 | 18 | int main() 19 | { 20 | std::unordered_set coll; 21 | 22 | // create inserter for coll 23 | // - inconvenient way 24 | asso_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 inserter for coll and insert elements 36 | // - convenient way 37 | asso_inserter(coll) = 44; 38 | asso_inserter(coll) = 55; 39 | 40 | PRINT_ELEMENTS(coll); 41 | 42 | // use inserter with an algorithm 43 | std::vector vals = { 33, 67, -4, 13, 5, 2 }; 44 | std::copy (vals.begin(), vals.end(), // source 45 | asso_inserter(coll)); // destination 46 | 47 | PRINT_ELEMENTS(coll); 48 | } 49 | -------------------------------------------------------------------------------- /iter/backinserter1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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/distance1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 -3 to 9 22 | for (int i=-3; i<=9; ++i) { 23 | coll.push_back(i); 24 | } 25 | 26 | // search element with value 5 27 | list::iterator pos; 28 | pos = find (coll.begin(), coll.end(), // range 29 | 5); // value 30 | 31 | if (pos != coll.end()) { 32 | // process and print difference from the beginning 33 | cout << "difference between beginning and 5: " 34 | << distance(coll.begin(),pos) << endl; 35 | } 36 | else { 37 | cout << "5 not found" << endl; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /iter/frontinserter1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // create front inserter for coll 22 | // - inconvenient way 23 | front_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 | 32 | PRINT_ELEMENTS(coll); 33 | 34 | // create front inserter and insert elements 35 | // - convenient way 36 | front_inserter(coll) = 44; 37 | front_inserter(coll) = 55; 38 | 39 | PRINT_ELEMENTS(coll); 40 | 41 | // use front inserter to insert all elements again 42 | copy (coll.begin(), coll.end(), // source 43 | front_inserter(coll)); // destination 44 | 45 | PRINT_ELEMENTS(coll); 46 | } 47 | -------------------------------------------------------------------------------- /iter/istreamiter1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | while (intReader != intReaderEOF) { 26 | cout << "once: " << *intReader << endl; 27 | cout << "once again: " << *intReader << endl; 28 | ++intReader; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /iter/iterswap1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | using namespace std; 17 | 18 | int main() 19 | { 20 | list coll; 21 | 22 | // insert elements from 1 to 9 23 | for (int i=1; i<=9; ++i) { 24 | coll.push_back(i); 25 | } 26 | 27 | PRINT_ELEMENTS(coll); 28 | 29 | // swap first and second value 30 | iter_swap (coll.begin(), next(coll.begin())); 31 | 32 | PRINT_ELEMENTS(coll); 33 | 34 | // swap first and last value 35 | iter_swap (coll.begin(), prev(coll.end())); 36 | 37 | PRINT_ELEMENTS(coll); 38 | } 39 | -------------------------------------------------------------------------------- /iter/ostreamiter1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // 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 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 32 | 33 | // write all elements without any delimiter 34 | copy (coll.cbegin(), coll.cend(), 35 | ostream_iterator(cout)); 36 | cout << endl; 37 | 38 | // write all elements with " < " as delimiter 39 | copy (coll.cbegin(), coll.cend(), 40 | ostream_iterator(cout," < ")); 41 | cout << endl; 42 | } 43 | -------------------------------------------------------------------------------- /iter/print.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // PRINT_ELEMENTS() 15 | // - prints optional string optstr followed by 16 | // - all elements of the collection coll 17 | // - in one line, separated by spaces 18 | template 19 | inline void PRINT_ELEMENTS (const T& coll, 20 | const std::string& optstr="") 21 | { 22 | std::cout << optstr; 23 | for (const auto& elem : coll) { 24 | std::cout << elem << ' '; 25 | } 26 | std::cout << std::endl; 27 | } 28 | -------------------------------------------------------------------------------- /iter/reviter1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // create list with elements from 1 to 9 24 | list coll = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 25 | 26 | // print all elements in normal order 27 | for_each (coll.begin(), coll.end(), // range 28 | print); // operation 29 | cout << endl; 30 | 31 | // print all elements in reverse order 32 | for_each (coll.rbegin(), coll.rend(), // range 33 | print); // operations 34 | cout << endl; 35 | } 36 | -------------------------------------------------------------------------------- /iter/reviter2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // create list with elements from 1 to 9 20 | vector coll = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 21 | 22 | // find position of element with value 5 23 | vector::const_iterator pos; 24 | pos = find (coll.cbegin(), coll.cend(), 25 | 5); 26 | 27 | // print value to which iterator pos refers 28 | cout << "pos: " << *pos << endl; 29 | 30 | // convert iterator to reverse iterator rpos 31 | vector::const_reverse_iterator rpos(pos); 32 | 33 | // print value to which reverse iterator rpos refers 34 | cout << "rpos: " << *rpos << endl; 35 | } 36 | -------------------------------------------------------------------------------- /iter/reviter4.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // create list with elements from 1 to 9 20 | list coll = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 21 | 22 | // find position of element with value 5 23 | list::const_iterator pos; 24 | pos = find (coll.cbegin(), coll.cend(), // range 25 | 5); // value 26 | 27 | // print value of the element 28 | cout << "pos: " << *pos << endl; 29 | 30 | // convert iterator to reverse iterator 31 | list::const_reverse_iterator rpos(pos); 32 | 33 | // print value of the element to which the reverse iterator refers 34 | cout << "rpos: " << *rpos << endl; 35 | 36 | // convert reverse iterator back to normal iterator 37 | list::const_iterator rrpos; 38 | rrpos = rpos.base(); 39 | 40 | // print value of the element to which the normal iterator refers 41 | cout << "rrpos: " << *rrpos << endl; 42 | } 43 | -------------------------------------------------------------------------------- /lang/lambda1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | std::function returnLambda () 15 | { 16 | return [] (int x, int y) { 17 | return x*y; 18 | }; 19 | } 20 | 21 | int main() 22 | { 23 | auto lf = returnLambda(); 24 | std::cout << lf(6,7) << std::endl; 25 | } 26 | -------------------------------------------------------------------------------- /num/complex2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | complex c1, c2; 21 | 22 | while (cin.peek() != EOF) { 23 | 24 | // read first complex number 25 | cout << "complex number c1: "; 26 | cin >> c1; 27 | if (!cin) { 28 | cerr << "input error" << endl; 29 | return EXIT_FAILURE; 30 | } 31 | 32 | // read second complex number 33 | cout << "complex number c2: "; 34 | cin >> c2; 35 | if (!cin) { 36 | cerr << "input error" << endl; 37 | return EXIT_FAILURE; 38 | } 39 | 40 | if (c1 == c2) { 41 | cout << "c1 and c2 are equal !" << endl; 42 | } 43 | 44 | cout << "c1 raised to the c2: " << pow(c1,c2) 45 | << endl << endl; 46 | 47 | // skip rest of line 48 | cin.ignore(numeric_limits::max(),'\n'); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /num/maskarray1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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); 34 | 35 | // fill valarray with values 36 | for (int i=0; i<12; i++) { 37 | va[i] = i; 38 | } 39 | 40 | printValarray (va, 3); 41 | 42 | // assign 77 to all values that are less than 5 43 | va[va<5.0] = 77.0; 44 | 45 | // add 100 to all values that are greater than 5 and less than 9 46 | va[va>5.0 && va<9.0] 47 | = valarray(va[va>5.0 && va<9.0]) + 100.0; 48 | 49 | printValarray (va, 3); 50 | } 51 | -------------------------------------------------------------------------------- /regex/regex3.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | string data = "\n" 19 | " Nico\n" 20 | " Josuttis\n" 21 | "\n"; 22 | 23 | regex reg("<(.*)>(.*)"); 24 | 25 | // iterate over all matches 26 | auto pos=data.cbegin(); 27 | auto end=data.cend(); 28 | smatch m; 29 | for ( ; regex_search(pos,end,m,reg); pos=m.suffix().first) { 30 | cout << "match: " << m.str() << endl; 31 | cout << " tag: " << m.str(1) << endl; 32 | cout << " value: " << m.str(2) << endl; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /regex/regex4.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // case-insensitive find LaTeX index entries 19 | string pat1 = R"(\\.*index\{([^}]*)\})"; // first capture group 20 | string pat2 = R"(\\.*index\{(.*)\}\{(.*)\})"; // 2nd and 3rd capture group 21 | regex pat (pat1+"\n"+pat2, 22 | regex_constants::egrep|regex_constants::icase); 23 | 24 | // initialize string with characters from standard input: 25 | string data((istreambuf_iterator(cin)), 26 | istreambuf_iterator()); 27 | 28 | // search and print matching index entries: 29 | smatch m; 30 | auto pos = data.cbegin(); 31 | auto end = data.cend(); 32 | for ( ; regex_search (pos,end,m,pat); pos=m.suffix().first) { 33 | cout << "match: " << m.str() << endl; 34 | cout << " val: " << m.str(1)+m.str(2) << endl; 35 | cout << " see: " << m.str(3) << endl; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /regex/regex5.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "regexexception.hpp" 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | try { 19 | // initialize regular expression with invalid syntax: 20 | regex pat ("\\\\.*index\\{([^}]*)\\}", 21 | regex_constants::grep|regex_constants::icase); 22 | //... 23 | } 24 | catch (const regex_error& e) { 25 | cerr << "regex_error: \n" 26 | << " what(): " << e.what() << "\n" 27 | << " code(): " << regexCode(e.code()) << endl; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /stl/array1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // array container of 5 string elements: 19 | array coll = { "hello", "world" }; 20 | 21 | // print each element with its index on a line 22 | for (int i=0; i 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | int main() 17 | { 18 | // type of the container: 19 | // - unordered_map: elements are key/value pairs 20 | // - string: keys have type string 21 | // - float: values have type float 22 | unordered_map coll; 23 | 24 | // insert some elements into the collection 25 | // - using the syntax of an associative array 26 | coll["VAT1"] = 0.16; 27 | coll["VAT2"] = 0.07; 28 | coll["Pi"] = 3.1415; 29 | coll["an arbitrary number"] = 4983.223; 30 | coll["Null"] = 0; 31 | 32 | // change value 33 | coll["VAT1"] += 0.03; 34 | 35 | // print difference of VAT values 36 | cout << "VAT difference: " << coll["VAT1"] - coll["VAT2"] << endl; 37 | } 38 | -------------------------------------------------------------------------------- /stl/copy1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 20 | vector coll2; 21 | 22 | // resize destination to have enough room for the overwriting algorithm 23 | coll2.resize(coll1.size()); 24 | 25 | // copy elements from first into second collection 26 | // - overwrites existing elements in destination 27 | copy(coll1.cbegin(), coll1.cend(), // source 28 | coll2.begin()); // destination 29 | 30 | // create third collection with enough room 31 | // - initial size is passed as parameter 32 | deque coll3(coll1.size()); 33 | 34 | // copy elements from first into third collection 35 | copy(coll1.cbegin(), coll1.cend(), // source 36 | coll3.begin()); // destination 37 | } 38 | -------------------------------------------------------------------------------- /stl/copybug.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 coll1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 19 | vector coll2; 20 | 21 | // RUNTIME ERROR: 22 | // - overwrites nonexisting elements in the destination 23 | copy(coll1.cbegin(), coll1.cend(), // source 24 | coll2.begin()); // destination 25 | //... 26 | } 27 | -------------------------------------------------------------------------------- /stl/deque1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | #include 15 | #include "print.hpp" 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | deque coll = {1, 2, 3, 5, 7, 11, 13, 17, 19}; 21 | 22 | PRINT_ELEMENTS(coll,"initialized: "); 23 | 24 | // negate all values in coll 25 | transform(coll.cbegin(), coll.cend(), // source 26 | coll.begin(), // destination 27 | negate()); // operation 28 | PRINT_ELEMENTS(coll,"negated: "); 29 | 30 | // square all values in coll 31 | transform(coll.cbegin(), coll.cend(), // first source 32 | coll.cbegin(), // second source 33 | coll.begin(), // destination 34 | multiplies()); // operation 35 | PRINT_ELEMENTS(coll,"squared: "); 36 | } 37 | -------------------------------------------------------------------------------- /stl/foreach1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // function that prints the passed argument 18 | void print(int elem) 19 | { 20 | cout << elem << ' '; 21 | } 22 | 23 | // void print(const string& elem) 24 | // { 25 | // cout << elem << ' '; 26 | // } 27 | 28 | int main() 29 | { 30 | vector coll; 31 | // vector str_coll = {"c++", "std", "lib"}; 32 | 33 | // insert elements from 1 to 9 34 | for (int i = 1; i <= 9; ++i) { 35 | coll.push_back(i); 36 | } 37 | 38 | // print all elements 39 | for_each(coll.cbegin(), coll.cend(), // range 40 | print); // operation 41 | cout << endl; 42 | 43 | // print all elements 44 | // for_each(str_coll.cbegin(), str_coll.cend(), // range 45 | // print); // operation 46 | // cout << endl; 47 | } 48 | -------------------------------------------------------------------------------- /stl/foreach2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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.cbegin(), coll.cend(), // range 35 | PrintInt()); // operation 36 | cout << endl; 37 | } 38 | -------------------------------------------------------------------------------- /stl/forwardlist1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 forward-list container for some prime numbers 18 | forward_list coll = { 2, 3, 5, 7, 11, 13, 17 }; 19 | 20 | // resize two times 21 | // - note: poor performance 22 | coll.resize(9); 23 | coll.resize(10,99); 24 | 25 | // print all elements: 26 | for (auto elem : coll) { 27 | cout << elem << ' '; 28 | } 29 | cout << endl; 30 | } 31 | -------------------------------------------------------------------------------- /stl/ioiter1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | copy(istream_iterator(cin), // start of source 26 | istream_iterator(), // end of source 27 | back_inserter(coll)); // destination 28 | 29 | // sort elements 30 | sort(coll.begin(), coll.end()); 31 | 32 | // print all elements without duplicates 33 | // - source: coll 34 | // - destination: standard output (with newline between elements) 35 | unique_copy(coll.cbegin(), coll.cend(), // source 36 | ostream_iterator(cout,"\n")); // destination 37 | } 38 | -------------------------------------------------------------------------------- /stl/iterbug.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 coll1; // empty collection 18 | vector coll2; // empty collection 19 | 20 | // RUNTIME ERROR: 21 | // - beginning is behind the end of the range 22 | vector::iterator pos = coll1.begin(); 23 | reverse(++pos, coll1.end()); 24 | 25 | // insert elements from 1 to 9 into coll1 26 | for (int i = 1; i <= 9; ++i) { 27 | coll1.push_back (i); 28 | } 29 | 30 | // RUNTIME ERROR: 31 | // - overwriting nonexisting elements 32 | copy(coll1.cbegin(), coll1.cend(), // source 33 | coll2.begin()); // destination 34 | 35 | // RUNTIME ERROR: 36 | // - collections mistaken 37 | // - cbegin() and cend() refer to different collections 38 | copy(coll1.cbegin(), coll2.cend(), // source 39 | coll1.end()); // destination 40 | } 41 | -------------------------------------------------------------------------------- /stl/lambda1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | deque coll = {1, 3, 19, 5, 13, 7, 11, 2, 17}; 19 | 20 | int x = 5; 21 | int y = 12; 22 | auto pos = find_if(coll.cbegin(), coll.cend(), // range 23 | [=](int i) { // search criterion 24 | return i > x && i < y; 25 | }); 26 | cout << "first elem >5 and <12: " << *pos << endl; 27 | } 28 | -------------------------------------------------------------------------------- /stl/list1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // - use range-based for loop 26 | for (auto elem : coll) { 27 | cout << elem << ' '; 28 | } 29 | cout << endl; 30 | } 31 | -------------------------------------------------------------------------------- /stl/list1old.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | list::const_iterator pos; 27 | for (pos = coll.begin(); pos != coll.end(); ++pos) { 28 | cout << *pos << ' '; 29 | } 30 | cout << endl; 31 | } 32 | -------------------------------------------------------------------------------- /stl/list2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | while (! coll.empty()) { 28 | cout << coll.front() << ' '; 29 | coll.pop_front(); 30 | } 31 | cout << endl; 32 | } 33 | -------------------------------------------------------------------------------- /stl/multiset1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | multiset cities { 19 | "Braunschweig", "Hanover", "Frankfurt", "New York", 20 | "Chicago", "Toronto", "Paris", "Frankfurt" 21 | }; 22 | 23 | // print each element: 24 | for (const auto& elem : cities) { 25 | cout << elem << " "; 26 | } 27 | cout << endl; 28 | 29 | // insert additional values: 30 | cities.insert( {"London", "Munich", "Hanover", "Braunschweig"} ); 31 | 32 | // print each element: 33 | for (const auto& elem : cities) { 34 | cout << elem << " "; 35 | } 36 | cout << endl; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /stl/print.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // PRINT_ELEMENTS() 15 | // - prints optional string optstr followed by 16 | // - all elements of the collection coll 17 | // - in one line, separated by spaces 18 | template 19 | inline void PRINT_ELEMENTS (const T& coll, 20 | const std::string& optstr="") 21 | { 22 | std::cout << optstr; 23 | for (const auto& elem : coll) { 24 | std::cout << elem << ' '; 25 | } 26 | std::cout << std::endl; 27 | } 28 | -------------------------------------------------------------------------------- /stl/remove1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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.cbegin(), coll.cend(), // 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.cbegin(), coll.cend(), // 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // unordered set with elements from 1 to 9 20 | set coll = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 21 | 22 | // print all elements of the collection 23 | copy(coll.cbegin(), coll.cend(), 24 | ostream_iterator(cout," ")); 25 | cout << endl; 26 | 27 | // Remove all elements with value 3 28 | // - algorithm remove() does not work 29 | // - instead member function erase() works 30 | int num = coll.erase(3); 31 | 32 | // print number of removed elements 33 | cout << "number of removed elements: " << num << endl; 34 | 35 | // print all elements of the modified collection 36 | copy(coll.cbegin(), coll.cend(), 37 | ostream_iterator(cout," ")); 38 | cout << endl; 39 | } 40 | -------------------------------------------------------------------------------- /stl/remove4.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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; 18 | 19 | // insert elements from 6 to 1 and 1 to 6 20 | for (int i = 1; i <= 6; ++i) { 21 | coll.push_front(i); 22 | coll.push_back(i); 23 | } 24 | 25 | // remove all elements with value 3 (poor performance) 26 | coll.erase(remove(coll.begin(),coll.end(), 3), coll.end()); 27 | 28 | // remove all elements with value 4 (good performance) 29 | coll.remove(4); 30 | } 31 | -------------------------------------------------------------------------------- /stl/reviter1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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.crbegin(), coll.crend(), // 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, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // - note that there are two calls of insert() with value 1 23 | coll.insert(3); 24 | coll.insert(1); 25 | coll.insert(5); 26 | coll.insert(4); 27 | coll.insert(1); 28 | coll.insert(6); 29 | coll.insert(2); 30 | 31 | // print all elements 32 | // - iterate over all elements 33 | IntSet::const_iterator pos; 34 | for (pos = coll.begin(); pos != coll.end(); ++pos) { 35 | std::cout << *pos << ' '; 36 | } 37 | std::cout << std::endl; 38 | } 39 | -------------------------------------------------------------------------------- /stl/transform1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | 18 | int square (int value) 19 | { 20 | return value*value; 21 | } 22 | 23 | int main() 24 | { 25 | std::set coll1; 26 | std::vector coll2; 27 | 28 | // insert elements from 1 to 9 into coll1 29 | for (int i = 1; i <= 9; ++i) { 30 | coll1.insert(i); 31 | } 32 | PRINT_ELEMENTS(coll1,"initialized: "); 33 | 34 | // transform each element from coll1 to coll2 35 | // - square transformed values 36 | std::transform(coll1.cbegin(),coll1.cend(), // source 37 | std::back_inserter(coll2), // destination 38 | square); // operation 39 | 40 | PRINT_ELEMENTS(coll2,"squared: "); 41 | } 42 | -------------------------------------------------------------------------------- /stl/unordmap1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | unordered_map coll { { "tim", 9.9 }, 19 | { "struppi", 11.77 } 20 | }; 21 | 22 | // square the value of each element: 23 | for (pair& elem : coll) { 24 | elem.second *= elem.second; 25 | } 26 | 27 | // print each element (key and value): 28 | for (const auto& elem : coll) { 29 | cout << elem.first << ": " << elem.second << endl; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /stl/unordmultiset1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | unordered_multiset cities { 19 | "Braunschweig", "Hanover", "Frankfurt", "New York", 20 | "Chicago", "Toronto", "Paris", "Frankfurt" 21 | }; 22 | 23 | // print each element: 24 | for (const auto& elem : cities) { 25 | cout << elem << " "; 26 | } 27 | cout << endl; 28 | 29 | // insert additional values: 30 | cities.insert( {"London", "Munich", "Hanover", "Braunschweig"} ); 31 | 32 | // print each element: 33 | for (const auto& elem : cities) { 34 | cout << elem << " "; 35 | } 36 | cout << endl; 37 | } 38 | -------------------------------------------------------------------------------- /stl/unordmultiset2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // unordered multiset container for int values 17 | std::unordered_multiset coll; 18 | 19 | // insert some elements 20 | coll.insert({1,3,5,7,11,13,17,19,23,27,1}); 21 | 22 | // print all elements 23 | for (auto elem : coll) { 24 | std::cout << elem << ' '; 25 | } 26 | std::cout << std::endl; 27 | 28 | // insert one more element 29 | coll.insert(25); 30 | 31 | // print all elements again 32 | for (auto elem : coll) { 33 | std::cout << elem << ' '; 34 | } 35 | std::cout << std::endl; 36 | } 37 | -------------------------------------------------------------------------------- /stl/vector1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | #include 16 | using namespace std; 17 | 18 | int main() 19 | { 20 | string input; 21 | 22 | // don't skip leading whitespaces 23 | cin.unsetf (ios::skipws); 24 | 25 | // read all characters while compressing whitespaces 26 | const locale& loc(cin.getloc()); // locale 27 | unique_copy(istream_iterator(cin), // beginning of source 28 | istream_iterator(), // end of source 29 | back_inserter(input), // destination 30 | [=] (char c1, char c2) { // criterion for adj. duplicates 31 | return isspace(c1,loc) && isspace(c2,loc); 32 | }); 33 | 34 | // process input 35 | // - here: write it to the standard output 36 | cout << input; 37 | } 38 | -------------------------------------------------------------------------------- /string/stringiter2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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.cbegin(),hello.cend()); 23 | 24 | // ranged-based for loop that iterates through all the characters 25 | for (char c : s) { 26 | cout << c; 27 | } 28 | cout << endl; 29 | 30 | // reverse the order of all characters inside the string 31 | reverse (s.begin(), s.end()); 32 | cout << "reverse: " << s << endl; 33 | 34 | // sort all characters inside the string 35 | sort (s.begin(), s.end()); 36 | cout << "ordered: " << s << endl; 37 | 38 | // remove adjacent duplicates 39 | // - unique() reorders and returns new end 40 | // - erase() shrinks accordingly 41 | s.erase (unique(s.begin(), 42 | s.end()), 43 | s.end()); 44 | cout << "no duplicates: " << s << endl; 45 | } 46 | -------------------------------------------------------------------------------- /util/clock.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | void printClockData() 17 | { 18 | using namespace std; 19 | 20 | cout << "- precision: "; 21 | // if time unit is less than or equal to one millisecond 22 | typedef typename C::period P; // type of time unit 23 | if (ratio_less_equal::value) { 24 | // convert to and print as milliseconds 25 | typedef typename ratio_multiply::type TT; 26 | cout << fixed << double(TT::num)/TT::den 27 | << " milliseconds" << endl; 28 | 29 | } else { 30 | // print as seconds 31 | cout << fixed << double(P::num)/P::den << " seconds" << endl; 32 | } 33 | cout << "- is_steady: " << boolalpha << C::is_steady << endl; 34 | } 35 | -------------------------------------------------------------------------------- /util/clock1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "clock.hpp" 13 | 14 | int main() 15 | { 16 | std::cout << "system_clock: " << std::endl; 17 | printClockData(); 18 | std::cout << "\nhigh_resolution_clock: " << std::endl; 19 | printClockData(); 20 | std::cout << "\nsteady_clock: " << std::endl; 21 | printClockData(); 22 | } 23 | -------------------------------------------------------------------------------- /util/minmax1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // function that compares two pointers by comparing the values to which they point 14 | bool int_ptr_less (int* a, int* b) 15 | { 16 | return *a < *b; 17 | } 18 | 19 | int main() 20 | { 21 | int x = 17; 22 | int y = 42; 23 | int z = 33; 24 | int* px = &x; 25 | int* py = &y; 26 | int* pz = &z; 27 | 28 | // call max() with special comparison function 29 | int* pmax = std::max (px, py, int_ptr_less); 30 | 31 | // call minmax() for initializer list with special comparison function 32 | std::pair extremes = std::minmax ({px, py, pz}, 33 | int_ptr_less); 34 | //... 35 | } 36 | -------------------------------------------------------------------------------- /util/pair1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | class Foo { 17 | public: 18 | Foo (tuple) { 19 | cout << "Foo::Foo(tuple)" << endl; 20 | } 21 | template 22 | Foo (Args... args) { 23 | cout << "Foo::Foo(args...)" << endl; 24 | } 25 | }; 26 | 27 | int main() 28 | { 29 | // create tuple t: 30 | tuple t(1,2.22); 31 | 32 | // pass the tuple as a whole to the constructor of Foo: 33 | pair p1 (42, t); 34 | 35 | // pass the elements of the tuple to the constructor of Foo: 36 | pair p2 (piecewise_construct, make_tuple(42), t); 37 | } 38 | 39 | -------------------------------------------------------------------------------- /util/printtuple.hpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | // helper: print elements with index IDX and higher of tuple t having MAX elements 15 | template 16 | struct PRINT_TUPLE { 17 | static void print (std::ostream& strm, const std::tuple& t) { 18 | strm << std::get(t) << (IDX+1==MAX ? "" : ","); 19 | PRINT_TUPLE::print(strm,t); 20 | } 21 | }; 22 | 23 | // partial specialization to end the recursion 24 | template 25 | struct PRINT_TUPLE { 26 | static void print (std::ostream& strm, const std::tuple& t) { 27 | } 28 | }; 29 | 30 | // output operator for tuples 31 | template 32 | std::ostream& operator << (std::ostream& strm, 33 | const std::tuple& t) 34 | { 35 | strm << "["; 36 | PRINT_TUPLE<0,sizeof...(Args),Args...>::print(strm,t); 37 | return strm << "]"; 38 | } 39 | -------------------------------------------------------------------------------- /util/ratio1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 | typedef ratio<5,3> FiveThirds; 18 | cout << FiveThirds::num << "/" << FiveThirds::den << endl; 19 | 20 | typedef ratio<25,15> AlsoFiveThirds; 21 | cout << AlsoFiveThirds::num << "/" << AlsoFiveThirds::den << endl; 22 | 23 | ratio<42,42> one; 24 | cout << one.num << "/" << one.den << endl; 25 | 26 | ratio<0> zero; 27 | cout << zero.num << "/" << zero.den << endl; 28 | 29 | // ratio<1,0> error; 30 | // cout << error.num << "/" << error.den << endl; 31 | 32 | typedef ratio<7,-3> Neg; 33 | cout << Neg::num << "/" << Neg::den << endl; 34 | } 35 | -------------------------------------------------------------------------------- /util/sharedptr2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 // for ofstream 13 | #include // for shared_ptr 14 | #include // for remove() 15 | #include 16 | 17 | using namespace std; 18 | 19 | class FileDeleter 20 | { 21 | private: 22 | std::string filename; 23 | public: 24 | FileDeleter (const std::string& fn) 25 | : filename(fn) { 26 | cout << "FileDeleter constructor." << endl; 27 | } 28 | void operator () (std::ofstream* fp) { 29 | delete fp; // close file 30 | std::remove(filename.c_str()); // delete file 31 | cout << "delet file: " << filename << endl; 32 | } 33 | }; 34 | 35 | int main() 36 | { 37 | // create and open temporary file: 38 | std::shared_ptr fp(new std::ofstream("tmpfile.txt"), 39 | FileDeleter("tmpfile.txt")); 40 | //... 41 | cout << "===========================" << endl; 42 | 43 | auto deleter = FileDeleter("hello_world.cpp"); 44 | deleter(new std::ofstream("hello_world.cpp")); 45 | } 46 | -------------------------------------------------------------------------------- /util/timepoint1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "timepoint.hpp" 14 | 15 | int main() 16 | { 17 | auto tp1 = makeTimePoint(2010,01,01,00,00); 18 | std::cout << asString(tp1) << std::endl; 19 | 20 | auto tp2 = makeTimePoint(2011,05,23,13,44); 21 | std::cout << asString(tp2) << std::endl; 22 | } 23 | -------------------------------------------------------------------------------- /util/tuple2.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 "printtuple.hpp" 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | int main() 18 | { 19 | tuple t(77,1.1,"more light"); 20 | cout << "io: " << t << endl; 21 | } 22 | -------------------------------------------------------------------------------- /util/uniqueptr1.cpp: -------------------------------------------------------------------------------- 1 | /* The following code example is taken from the book 2 | * "The C++ Standard Library - A Tutorial and Reference, 2nd Edition" 3 | * by Nicolai M. Josuttis, Addison-Wesley, 2012 4 | * 5 | * (C) Copyright Nicolai M. Josuttis 2012. 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 // for unique_ptr 14 | #include // for opendir(), ... 15 | #include // for strerror() 16 | #include // for errno 17 | using namespace std; 18 | 19 | class DirCloser 20 | { 21 | public: 22 | void operator () (DIR* dp) { 23 | if (closedir(dp) != 0) { 24 | std::cerr << "OOPS: closedir() failed" << std::endl; 25 | } 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | // open current directory: 32 | unique_ptr pDir(opendir(".")); 33 | 34 | // process each directory entry: 35 | struct dirent *dp; 36 | while ((dp = readdir(pDir.get())) != nullptr) { 37 | string filename(dp->d_name); 38 | cout << "process " << filename << endl; 39 | //... 40 | } 41 | } 42 | --------------------------------------------------------------------------------