/* The following code example is taken from the book * "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */ #if 0 ////////// stl/vector1.cpp p77 #include #include using namespace std; int main() { vector coll; // vector container for integer elements // append elements with values 1 to 6 for (int i=1; i<=6; ++i) { coll.push_back(i); } // print all elements followed by a space for (i=0; i #include using namespace std; int main() { deque coll; // deque container for floating-point elements // insert elements from 1.1 to 6.6 each at the front for (int i=1; i<=6; ++i) { coll.push_front(i*1.1); // insert at the front } // print all elements followed by a space for (i=0; i #include using namespace std; int main() { list coll; // list container for character elements // append elements from 'a' to 'z' for (char c='a'; c<='z'; ++c) { coll.push_back(c); } /* print all elements * - while there are elements * - print and remove the first element */ while (! coll.empty()) { cout << coll.front() << ' '; coll.pop_front(); } cout << endl; return 0; } // a b c d e f g h i j k l m n o p q r s t u v w x y z ////////// stl/list2.cpp p84 #include #include using namespace std; int main() { list coll; // list container for character elements // append elements from 'a' to 'z' for (char c='a'; c<='z'; ++c) { coll.push_back(c); } /* print all elements * - iterate over all elements */ list::const_iterator pos; for (pos = coll.begin(); pos != coll.end(); ++pos) { cout << *pos << ' '; } cout << endl; return 0; } // a b c d e f g h i j k l m n o p q r s t u v w x y z ////////// stl/set1.cpp p87 #pragma warning( disable : 4786 ) #include #include int main() { // type of the collection typedef std::set IntSet; IntSet coll; // set container for int values /* insert elements from 1 to 6 in arbitray order * - value 1 gets inserted twice */ coll.insert(3); coll.insert(1); coll.insert(5); coll.insert(4); coll.insert(1); coll.insert(6); coll.insert(2); /* print all elements * - iterate over all elements */ IntSet::const_iterator pos; for (pos = coll.begin(); pos != coll.end(); ++pos) { std::cout << *pos << ' '; } std::cout << std::endl; return 0; } // 1 2 3 4 5 6 ////////// stl/mmap1.cpp p90 #pragma warning( disable : 4786 ) #include #include #include using namespace std; int main() { // type of the collection typedef multimap IntStringMMap; IntStringMMap coll; // set container for int/string values // insert some elements in arbitrary order // - a value with key 1 gets inserted twice coll.insert(make_pair(5,string("tagged"))); coll.insert(make_pair(2,string("a"))); coll.insert(make_pair(1,string("this"))); coll.insert(make_pair(4,string("of"))); coll.insert(make_pair(6,string("strings"))); coll.insert(make_pair(1,string("is"))); coll.insert(make_pair(3,string("multimap"))); /* print all element values * - iterate over all elements * - element member second is the value */ IntStringMMap::iterator pos; for (pos = coll.begin(); pos != coll.end(); ++pos) { cout << pos->second << ' '; } cout << endl; return 0; } // this is a multimap of tagged strings ////////// stl/map1.cpp p91 #pragma warning( disable : 4786 ) #include #include #include using namespace std; int main() { /* type of the container: * - map: elements key/value pairs * - string: keys have type string * - float: values have type float */ typedef map StringDoubleMap; StringDoubleMap coll; // insert some elements into the collection coll["VAT"] = 0.15; coll["Pi"] = 3.1415; coll["an arbitrary number"] = 4983.223; coll["Null"] = 0; /* print all elements * - iterate over all elements * - element member first is the key * - element member second is the value */ StringDoubleMap::iterator pos; for (pos = coll.begin(); pos != coll.end(); ++pos) { cout << "key: \"" << pos->first << "\" " << "value: " << pos->second << endl; } return 0; } // key: "Null" value: 0 // key: "Pi" value: 3.1415 // key: "VAT" value: 0.15 // key: "an arbitrary number" value: 4983.22 ////////// stl/algo1.cpp p95 #include #include #include using namespace std; int main() { vector coll; vector::iterator pos; // insert elements from 1 to 6 in arbitrary order coll.push_back(2); coll.push_back(5); coll.push_back(4); coll.push_back(1); coll.push_back(6); coll.push_back(3); // find and print minimum and maximum elements pos = min_element (coll.begin(), coll.end()); cout << "min: " << *pos << endl; pos = max_element (coll.begin(), coll.end()); cout << "max: " << *pos << endl; // sort all elements sort (coll.begin(), coll.end()); // find the first element with value 3 pos = find (coll.begin(), coll.end(), // range 3); // value // reverse the order of the found element with value 3 and all following elements reverse (pos, coll.end()); // print all elements for (pos=coll.begin(); pos!=coll.end(); ++pos) { cout << *pos << ' '; } cout << endl; return 0; } // min: 1 // max: 6 // 1 2 6 5 4 3 ////////// stl/find1.cpp p97 #include #include #include using namespace std; int main() { list coll; list::iterator pos; // insert elements from 20 to 40 for (int i=20; i<=40; ++i) { coll.push_back(i); } /* find position of element with value 3 * - there is none, so pos gets coll.end() */ pos = find (coll.begin(), coll.end(), // range 3); // value /* reverse the order of elements between found element and the end * - because pos is coll.end() it reverses an empty range */ reverse (pos, coll.end()); // find positions of values 25 and 35 list::iterator pos25, pos35; pos25 = find (coll.begin(), coll.end(), // range 25); // value pos35 = find (coll.begin(), coll.end(), // range 35); // value /* print the maximum of the corresponding range * - note: including pos25 but excluding pos35 */ cout << "max: " << *max_element (pos25, pos35) << endl; // process the elements including the last position cout << "max: " << *max_element (pos25, ++pos35) << endl; return 0; } // max: 34 // max: 35 ////////// stl/copy1.cpp p102 #include #include #include #include using namespace std; int main() { list coll1; vector coll2; // insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll1.push_back(i); } cout << "size of coll1 is " << coll1.size() << '\n'; // RUNTIME ERROR: // - overwrites nonexisting elements in the destination copy (coll1.begin(), coll1.end(), // source coll2.begin()); // destination //... cout << "size of coll2 is " << coll2.size() << '\n'; return 0; } // size of coll1 is 9 ////////// stl/copy2.cpp p103 #include #include #include #include #include using namespace std; int main() { list coll1; vector coll2; // insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll1.push_back(i); } cout << "coll1 is - "; for (list::iterator it = coll1.begin(); it != coll1.end(); ++it) cout << *it << ' '; cout << '\n'; // resize destination to have enough room for the overwriting algorithm coll2.resize (coll1.size()); /* copy elements from first into second collection * - overwrites existing elements in destination */ copy (coll1.begin(), coll1.end(), // source coll2.begin()); // destination cout << "coll2 is - "; for (vector::iterator it2 = coll2.begin(); it2 != coll2.end(); ++it2) cout << *it2 << ' '; cout << '\n'; /* create third collection with enough room * - initial size is passed as parameter */ deque coll3(coll1.size()); // copy elements from first into third collection copy (coll1.begin(), coll1.end(), // source coll3.begin()); // destination cout << "coll3 is - "; for (deque::iterator it3 = coll3.begin(); it3 != coll3.end(); ++it3) cout << *it3 << ' '; cout << '\n'; return 0; } // coll1 is - 1 2 3 4 5 6 7 8 9 // coll2 is - 1 2 3 4 5 6 7 8 9 // coll3 is - 1 2 3 4 5 6 7 8 9 ////////// stl/copy3.cpp p105 #pragma warning( disable : 4786 ) #include #include #include #include #include #include using namespace std; int main() { list coll1; // insert elements from 1 to 9 into the first collection for (int i=1; i<=9; ++i) { coll1.push_back(i); } // copy the elements of coll1 into coll2 by appending them vector coll2; copy (coll1.begin(), coll1.end(), // source back_inserter(coll2)); // destination cout << "coll2 is - "; for (vector::iterator it2 = coll2.begin(); it2 != coll2.end(); ++it2) cout << *it2 << ' '; cout << '\n'; // copy the elements of coll1 into coll3 by inserting them at the front // - reverses the order of the elements deque coll3; copy (coll1.begin(), coll1.end(), // source front_inserter(coll3)); // destination cout << "coll3 is - "; for (deque::iterator it3 = coll3.begin(); it3 != coll3.end(); ++it3) cout << *it3 << ' '; cout << '\n'; // copy elements of coll1 into coll4 // - only inserter that works for associative collections set coll4; copy (coll1.begin(), coll1.end(), // source inserter(coll4,coll4.begin())); // destination cout << "coll4 is - "; for (set::iterator it4 = coll4.begin(); it4 != coll4.end(); ++it4) cout << *it4 << ' '; cout << '\n'; return 0; } // coll2 is - 1 2 3 4 5 6 7 8 9 // coll3 is - 9 8 7 6 5 4 3 2 1 // coll4 is - 1 2 3 4 5 6 7 8 9 ////////// stl/ioiter1.cpp p107 #pragma warning( disable : 4786 ) #include #include #include #include using namespace std; int main() { vector coll; /* read all words from the standard input * - source: all strings until end-of-file (or error) * - destination: coll (inserting) */ copy (istream_iterator(cin), // start of source istream_iterator(), // end of source back_inserter(coll)); // destination // sort elements sort (coll.begin(), coll.end()); /* print all elements without duplicates * - source: coll * - destination: standard output (with newline between elements) */ unique_copy (coll.begin(), coll.end(), // source ostream_iterator(cout,"\n")); // destination return 0; } // 1 12 123 1234 123 12 1 12 1 // ^Z // ^Z // 1 // 12 // 123 // 1234 ////////// stl/riter1.cpp p109 #include #include #include using namespace std; int main() { vector coll; // insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll.push_back(i); } // print all element in reverse order copy (coll.rbegin(), coll.rend(), // source ostream_iterator(cout," ")); // destination cout << endl; return 0; } // 9 8 7 6 5 4 3 2 1 ////////// stl/remove1.cpp p111 #include #include #include using namespace std; int main() { list coll; // insert elements from 6 to 1 and 1 to 6 for (int i=1; i<=6; ++i) { coll.push_front(i); coll.push_back(i); } // print all elements of the collection cout << "pre: "; copy (coll.begin(), coll.end(), // source ostream_iterator(cout," ")); // destination cout << endl; // remove all elements with value 3 remove (coll.begin(), coll.end(), // range 3); // value // print all elements of the collection cout << "post: "; copy (coll.begin(), coll.end(), // source ostream_iterator(cout," ")); // destination cout << endl; return 0; } // pre: 6 5 4 3 2 1 1 2 3 4 5 6 // post: 6 5 4 2 1 1 2 4 5 6 5 6 ////////// stl/remove2.cpp p112 #include #include #include using namespace std; int main() { list coll; // insert elements from 6 to 1 and 1 to 6 for (int i=1; i<=6; ++i) { coll.push_front(i); coll.push_back(i); } // print all elements of the collection copy (coll.begin(), coll.end(), ostream_iterator(cout," ")); cout << endl; // remove all elements with value 3 // - retain new end list::iterator end = remove (coll.begin(), coll.end(), 3); // print resulting elements of the collection copy (coll.begin(), end, ostream_iterator(cout," ")); cout << endl; // print number of resulting elements cout << "number of removed elements: " << distance(end,coll.end()) << endl; // remove ``removed'' elements coll.erase (end, coll.end()); // print all elements of the modified collection copy (coll.begin(), coll.end(), ostream_iterator(cout," ")); cout << endl; return 0; } // 6 5 4 3 2 1 1 2 3 4 5 6 // 6 5 4 2 1 1 2 4 5 6 // number of removed elements: 2 // 6 5 4 2 1 1 2 4 5 6 ////////// stl/remove3.cpp p115 #pragma warning( disable : 4786 ) #include #include #include using namespace std; int main() { set coll; // insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll.insert(i); } // print all elements of the collection copy (coll.begin(), coll.end(), ostream_iterator(cout," ")); cout << endl; /* Remove all elements with value 3 * - algorithm remove() does not work * - instead member function erase() works */ int num = coll.erase(3); // print number of removed elements cout << "number of removed elements: " << num << endl; // print all elements of the modified collection copy (coll.begin(), coll.end(), ostream_iterator(cout," ")); cout << endl; return 0; } // 1 2 3 4 5 6 7 8 9 // number of removed elements: 1 // 1 2 4 5 6 7 8 9 ////////// stl/remove4.cpp p117 #include #include #include using namespace std; int main() { list coll; // insert elements from 6 to 1 and 1 to 6 for (int i=1; i<=6; ++i) { coll.push_front(i); coll.push_back(i); } // remove all elements with value 3 // - poor performance coll.erase (remove(coll.begin(),coll.end(), 3), coll.end()); // remove all elements with value 4 // - good performance coll.remove (4); cout << "coll is - "; for (list::iterator it = coll.begin(); it != coll.end(); ++it) cout << *it << ' '; cout << '\n'; return 0; } // coll is - 6 5 2 1 1 2 5 6 ////////// stl/foreach1.cpp p119 #include #include #include using namespace std; // function that prints the passed argument void print (int elem) { cout << elem << ' '; } int main() { vector coll; // insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll.push_back(i); } // print all elements for_each (coll.begin(), coll.end(), // range print); // operation cout << endl; return 0; } // 1 2 3 4 5 6 7 8 9 ////////// stl/transform1.cpp p120 #pragma warning( disable : 4786 ) #include #include #include #include /********* #include "print.hpp" *********/ #include /* PRINT_ELEMENTS() * - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */ template inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") { typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << ' '; } std::cout << std::endl; } /********* #include "print.hpp" *********/ int square (int value) { return value*value; } int main() { std::set coll1; std::vector coll2; // insert elements from 1 to 9 into coll1 for (int i=1; i<=9; ++i) { coll1.insert(i); } PRINT_ELEMENTS(coll1,"initialized: "); // transform each element from coll1 to coll2 // - square transformed values std::transform (coll1.begin(),coll1.end(), // source std::back_inserter(coll2), // destination square); // operation PRINT_ELEMENTS(coll2,"squared: "); return 0; } // initialized: 1 2 3 4 5 6 7 8 9 // squared: 1 4 9 16 25 36 49 64 81 ////////// stl/prime1.cpp p121 #include #include #include #include // for abs() using namespace std; // predicate, which returns whether an integer is a prime number bool isPrime (int number) { // ignore negative sign number = abs(number); // 0 and 1 are prime numbers if (number == 0 || number == 1) { return true; } // find divisor that divides without a remainder int divisor; for (divisor = number/2; number%divisor != 0; --divisor) { ; } // if no divisor greater than 1 is found, it is a prime number return divisor == 1; } int main() { list coll; // insert elements from 24 to 30 for (int i=24; i<=30; ++i) { coll.push_back(i); } // search for prime number list::iterator pos; pos = find_if (coll.begin(), coll.end(), // range isPrime); // predicate if (pos != coll.end()) { // found cout << *pos << " is first prime number found" << endl; } else { // not found cout << "no prime number found" << endl; } return 0; } // 29 is first prime number found ////////// stl/sort1.cpp p123 #include #include #include #include #include using namespace std; /* class Person */ class Person { private: string fn; // first name string ln; // last name public: Person() { } Person(const string& f, const string& n) : fn(f), ln(n) { } string firstname() const; string lastname() const; // ... }; inline string Person::firstname() const { return fn; } inline string Person::lastname() const { return ln; } ostream& operator<< (ostream& s, const Person& p) { s << "[" << p.firstname() << " " << p.lastname() << "]"; return s; } /* binary function predicate: * - returns whether a person is less than another person */ bool personSortCriterion (const Person& p1, const Person& p2) { /* a person is less than another person * - if the last name is less * - if the last name is equal and the first name is less */ return p1.lastname() < p2.lastname() || (!(p2.lastname() < p1.lastname()) && p1.firstname() < p2.firstname()); } int main() { // create some persons Person p1("nicolai","josuttis"); Person p2("ulli","josuttis"); Person p3("anica","josuttis"); Person p4("lucas","josuttis"); Person p5("lucas","otto"); Person p6("lucas","arm"); Person p7("anica","holle"); // insert person into collection coll deque coll; coll.push_back(p1); coll.push_back(p2); coll.push_back(p3); coll.push_back(p4); coll.push_back(p5); coll.push_back(p6); coll.push_back(p7); // print elements cout << "deque before sort():" << endl; deque::iterator pos; for (pos = coll.begin(); pos != coll.end(); ++pos) { cout << *pos << endl; } // sort elements sort(coll.begin(),coll.end(), // range personSortCriterion); // sort criterion // print elements cout << "deque after sort():" << endl; for (pos = coll.begin(); pos != coll.end(); ++pos) { cout << *pos << endl; } return 0; } // deque before sort(): // [nicolai josuttis] // [ulli josuttis] // [anica josuttis] // [lucas josuttis] // [lucas otto] // [lucas arm] // [anica holle] // deque after sort(): // [lucas arm] // [anica holle] // [anica josuttis] // [lucas josuttis] // [nicolai josuttis] // [ulli josuttis] // [lucas otto] ////////// stl/foreach2.cpp p125 #include #include #include using namespace std; // simple function object that prints the passed argument class PrintInt { public: void operator() (int elem) const { cout << elem << ' '; } }; int main() { vector coll; // insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll.push_back(i); } // print all elements for_each (coll.begin(), coll.end(), // range PrintInt()); // operation cout << endl; return 0; } // 1 2 3 4 5 6 7 8 9 ////////// stl/add1.cpp p128 #include #include #include #include "print.hpp" using namespace std; // function object that adds the value with which it is initialized class AddValue { private: int theValue; // the value to add public: // constructor initializes the value to add AddValue(int v) : theValue(v) { } // the ``function call'' for the element adds the value void operator() (int& elem) const { elem += theValue; } }; int main() { list coll; // insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll.push_back(i); } PRINT_ELEMENTS(coll,"initialized: "); // add value 10 to each element for_each (coll.begin(), coll.end(), // range AddValue(10)); // operation PRINT_ELEMENTS(coll,"after adding 10: "); // add value of first element to each element for_each (coll.begin(), coll.end(), // range AddValue(*coll.begin())); // operation PRINT_ELEMENTS(coll,"after adding first element: "); return 0; } // initialized: 1 2 3 4 5 6 7 8 9 // after adding 10: 11 12 13 14 15 16 17 18 19 // after adding first element: 22 23 24 25 26 27 28 29 30 ////////// stl/fo1.cpp p132 #pragma warning( disable : 4786 ) #include #include #include #include #include "print.hpp" using namespace std; int main() { // sort elements with '>' set > coll1; deque coll2; // insert elements from 1 to 9 for (int i=1; i<=9; ++i) { coll1.insert(i); } PRINT_ELEMENTS(coll1,"initialized: "); // transform all elements into coll2 by multiplying 10 transform (coll1.begin(),coll1.end(), // source back_inserter(coll2), // destination bind2nd(multiplies(),10)); // operation PRINT_ELEMENTS(coll2,"transformed: "); // replace value equal to 70 with 42 replace_if (coll2.begin(),coll2.end(), // range bind2nd(equal_to(),70), // replace criterion 42); // new value PRINT_ELEMENTS(coll2,"replaced: "); // remove all elements with values less than 50 coll2.erase(remove_if(coll2.begin(),coll2.end(), // range bind2nd(less(),50)), // remove criterion coll2.end()); PRINT_ELEMENTS(coll2,"removed: "); return 0; } // initialized: 9 8 7 6 5 4 3 2 1 // transformed: 90 80 70 60 50 40 30 20 10 // replaced: 90 80 42 60 50 40 30 20 10 // removed: 90 80 60 50 ////////// stl/iterbug1.cpp p137 #include #include #include using namespace std; int main() { vector coll1; // empty collection vector coll2; // empty collection /* RUNTIME ERROR: * - beginning is behind the end of the range */ vector::iterator pos = coll1.begin(); reverse (++pos, coll1.end()); // insert elements from 1 to 9 into coll2 for (int i=1; i<=9; ++i) { coll2.push_back (i); } /* RUNTIME ERROR: * - overwriting nonexisting elements */ copy (coll2.begin(), coll2.end(), // source coll1.begin()); // destination /* RUNTIME ERROR: * - collections mistaken * - begin() and end() mistaken */ copy (coll1.begin(), coll2.end(), // source coll1.end()); // destination return 0; } #endif