|
| 1 | +// fig13_04.cpp |
| 2 | +// Standard library list class template. |
| 3 | +#include <algorithm> // copy algorithm |
| 4 | +#include <iostream> |
| 5 | +#include <iterator> // ostream_iterator |
| 6 | +#include <list> // list class-template definition |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +// printList function template definition; uses |
| 10 | +// ostream_iterator and copy algorithm to output list elements |
| 11 | +template <typename T> |
| 12 | +void printList(const std::list<T>& items) { |
| 13 | + if (items.empty()) { // list is empty |
| 14 | + std::cout << "List is empty"; |
| 15 | + } |
| 16 | + else { |
| 17 | + std::ostream_iterator<T> output{std::cout, " "}; |
| 18 | + std::ranges::copy(items, output); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +int main() { |
| 23 | + std::list<int> values{}; // create list of ints |
| 24 | + |
| 25 | + // insert items in values |
| 26 | + values.push_front(1); |
| 27 | + values.push_front(2); |
| 28 | + values.push_back(4); |
| 29 | + values.push_back(3); |
| 30 | + |
| 31 | + std::cout << "values contains: "; |
| 32 | + printList(values); |
| 33 | + |
| 34 | + values.sort(); // sort values |
| 35 | + std::cout << "\nvalues after sorting contains: "; |
| 36 | + printList(values); |
| 37 | + |
| 38 | + // insert elements of ints into otherValues |
| 39 | + std::vector ints{2, 6, 4, 8}; |
| 40 | + std::list<int> otherValues{}; // create list of ints |
| 41 | + otherValues.insert(otherValues.cbegin(), ints.cbegin(), ints.cend()); |
| 42 | + std::cout << "\nAfter insert, otherValues contains: "; |
| 43 | + printList(otherValues); |
| 44 | + |
| 45 | + // remove otherValues elements and insert at end of values |
| 46 | + values.splice(values.cend(), otherValues); |
| 47 | + std::cout << "\nAfter splice, values contains: "; |
| 48 | + printList(values); |
| 49 | + |
| 50 | + values.sort(); // sort values |
| 51 | + std::cout << "\nAfter sort, values contains: "; |
| 52 | + printList(values); |
| 53 | + |
| 54 | + // insert elements of ints into otherValues |
| 55 | + otherValues.insert(otherValues.cbegin(), ints.cbegin(), ints.cend()); |
| 56 | + otherValues.sort(); // sort the list |
| 57 | + std::cout << "\nAfter insert and sort, otherValues contains: "; |
| 58 | + printList(otherValues); |
| 59 | + |
| 60 | + // remove otherValues elements and insert into values in sorted order |
| 61 | + values.merge(otherValues); |
| 62 | + std::cout << "\nAfter merge:\n values contains: "; |
| 63 | + printList(values); |
| 64 | + std::cout << "\n otherValues contains: "; |
| 65 | + printList(otherValues); |
| 66 | + |
| 67 | + values.pop_front(); // remove element from front |
| 68 | + values.pop_back(); // remove element from back |
| 69 | + std::cout << "\nAfter pop_front and pop_back:\n values contains: "; |
| 70 | + printList(values); |
| 71 | + |
| 72 | + values.unique(); // remove duplicate elements |
| 73 | + std::cout << "\nAfter unique, values contains: "; |
| 74 | + printList(values); |
| 75 | + |
| 76 | + values.swap(otherValues); // swap elements of values and otherValues |
| 77 | + std::cout << "\nAfter swap:\n values contains: "; |
| 78 | + printList(values); |
| 79 | + std::cout << "\n otherValues contains: "; |
| 80 | + printList(otherValues); |
| 81 | + |
| 82 | + // replace contents of values with elements of otherValues |
| 83 | + values.assign(otherValues.cbegin(), otherValues.cend()); |
| 84 | + std::cout << "\nAfter assign, values contains: "; |
| 85 | + printList(values); |
| 86 | + |
| 87 | + // remove otherValues elements and insert into values in sorted order |
| 88 | + values.merge(otherValues); |
| 89 | + std::cout << "\nAfter merge, values contains: "; |
| 90 | + printList(values); |
| 91 | + |
| 92 | + values.remove(4); // remove all 4s |
| 93 | + std::cout << "\nAfter remove(4), values contains: "; |
| 94 | + printList(values); |
| 95 | + std::cout << "\n"; |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +/************************************************************************** |
| 100 | + * (C) Copyright 1992-2021 by Deitel & Associates, Inc. and * |
| 101 | + * Pearson Education, Inc. All Rights Reserved. * |
| 102 | + * * |
| 103 | + * DISCLAIMER: The authors and publisher of this book have used their * |
| 104 | + * best efforts in preparing the book. These efforts include the * |
| 105 | + * development, research, and testing of the theories and programs * |
| 106 | + * to determine their effectiveness. The authors and publisher make * |
| 107 | + * no warranty of any kind, expressed or implied, with regard to these * |
| 108 | + * programs or to the documentation contained in these books. The authors * |
| 109 | + * and publisher shall not be liable in any event for incidental or * |
| 110 | + * consequential damages in connection with, or arising out of, the * |
| 111 | + * furnishing, performance, or use of these programs. * |
| 112 | + **************************************************************************/ |
| 113 | + |
0 commit comments