Skip to content

Commit 4c58ccc

Browse files
committed
ch13 draft examples uploaded
1 parent 4edb84a commit 4c58ccc

12 files changed

+672
-0
lines changed

examples/ch13/fig13_01.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// fig13_01.cpp
2+
// Demonstrating input and output with iterators.
3+
#include <iostream>
4+
#include <iterator> // ostream_iterator and istream_iterator
5+
6+
int main() {
7+
std::cout << "Enter two integers: ";
8+
9+
// create istream_iterator for reading int values from cin
10+
std::istream_iterator<int> inputInt{std::cin};
11+
12+
const int number1{*inputInt}; // read int from standard input
13+
++inputInt; // move iterator to next input value
14+
const int number2{*inputInt}; // read int from standard input
15+
16+
// create ostream_iterator for writing int values to cout
17+
std::ostream_iterator<int> outputInt{std::cout};
18+
19+
std::cout << "The sum is: ";
20+
*outputInt = number1 + number2; // output result to cout
21+
std::cout << "\n";
22+
}
23+
24+
25+
/**************************************************************************
26+
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
27+
* Pearson Education, Inc. All Rights Reserved. *
28+
* *
29+
* DISCLAIMER: The authors and publisher of this book have used their *
30+
* best efforts in preparing the book. These efforts include the *
31+
* development, research, and testing of the theories and programs *
32+
* to determine their effectiveness. The authors and publisher make *
33+
* no warranty of any kind, expressed or implied, with regard to these *
34+
* programs or to the documentation contained in these books. The authors *
35+
* and publisher shall not be liable in any event for incidental or *
36+
* consequential damages in connection with, or arising out of, the *
37+
* furnishing, performance, or use of these programs. *
38+
**************************************************************************/

examples/ch13/fig13_02.cpp

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// fig13_02.cpp
2+
// Standard library vector class template.
3+
#include <fmt/format.h> // C++20: This will be #include <format>
4+
#include <iostream>
5+
#include <ranges>
6+
#include <vector> // vector class-template definition
7+
8+
// display value appended to vector and updated vector size and capacity
9+
void showResult(int value, size_t size, size_t capacity) {
10+
std::cout << fmt::format("appended: {}; size: {}; capacity: {}\n",
11+
value, size, capacity);
12+
}
13+
14+
int main() {
15+
std::vector<int> integers{}; // create vector of ints
16+
17+
std::cout << "Size of integers: " << integers.size()
18+
<< "\nCapacity of integers: " << integers.capacity() << "\n\n";
19+
20+
// append 1-10 to integers and display updated size and capacity
21+
for (int i : std::views::iota(1, 11)) {
22+
integers.push_back(i); // push_back is in vector, deque and list
23+
showResult(i, integers.size(), integers.capacity());
24+
}
25+
26+
std::cout << "\nOutput integers using iterators: ";
27+
28+
for (auto constIterator{integers.cbegin()};
29+
constIterator != integers.cend(); ++constIterator) {
30+
std::cout << *constIterator << ' ';
31+
}
32+
33+
std::cout << "\nOutput integers in reverse using iterators: ";
34+
35+
// display vector in reverse order using const_reverse_iterator
36+
for (auto reverseIterator{integers.crbegin()};
37+
reverseIterator != integers.crend(); ++reverseIterator) {
38+
std::cout << *reverseIterator << ' ';
39+
}
40+
41+
std::cout << "\n";
42+
}
43+
44+
/**************************************************************************
45+
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
46+
* Pearson Education, Inc. All Rights Reserved. *
47+
* *
48+
* DISCLAIMER: The authors and publisher of this book have used their *
49+
* best efforts in preparing the book. These efforts include the *
50+
* development, research, and testing of the theories and programs *
51+
* to determine their effectiveness. The authors and publisher make *
52+
* no warranty of any kind, expressed or implied, with regard to these *
53+
* programs or to the documentation contained in these books. The authors *
54+
* and publisher shall not be liable in any event for incidental or *
55+
* consequential damages in connection with, or arising out of, the *
56+
* furnishing, performance, or use of these programs. *
57+
**************************************************************************/

examples/ch13/fig13_03.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// fig13_03.cpp
2+
// Testing standard library vector class template
3+
// element-manipulation functions.
4+
#include <algorithm> // copy algorithm
5+
#include <fmt/format.h> // C++20: This will be #include <format>
6+
#include <iostream>
7+
#include <ranges>
8+
#include <iterator> // ostream_iterator iterator
9+
#include <vector>
10+
11+
int main() {
12+
std::vector values{1, 2, 3, 4, 5}; // class template argument deduction
13+
std::vector<int> integers{values.cbegin(), values.cend()};
14+
std::ostream_iterator<int> output{std::cout, " "};
15+
16+
std::cout << "integers contains: ";
17+
std::copy(integers.cbegin(), integers.cend(), output);
18+
19+
std::cout << fmt::format("\nfront: {}\nback: {}\n\n",
20+
integers.front(), integers.back());
21+
22+
integers[0] = 7; // set first element to 7
23+
integers.at(2) = 10; // set element at position 2 to 10
24+
25+
// insert 22 as 2nd element
26+
integers.insert(integers.cbegin() + 1, 22);
27+
28+
std::cout << "Contents of vector integers after changes: ";
29+
std::ranges::copy(integers, output);
30+
31+
integers.erase(integers.cbegin()); // erase first element
32+
std::cout << "\n\nintegers after erasing first element: ";
33+
std::ranges::copy(integers, output);
34+
35+
// erase remaining elements
36+
integers.erase(integers.cbegin(), integers.cend());
37+
std::cout << fmt::format("\nErased all elements: integers {} empty\n",
38+
integers.empty() ? "is" : "is not");
39+
40+
// insert elements from the vector values
41+
integers.insert(integers.cbegin(), values.cbegin(), values.cend());
42+
std::cout << "\nContents of vector integers before clear: ";
43+
std::ranges::copy(integers, output);
44+
45+
// empty integers; clear calls erase to empty a collection
46+
integers.clear();
47+
std::cout << fmt::format("\nAfter clear, integers {} empty\n",
48+
integers.empty() ? "is" : "is not");
49+
}
50+
51+
52+
/**************************************************************************
53+
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
54+
* Pearson Education, Inc. All Rights Reserved. *
55+
* *
56+
* DISCLAIMER: The authors and publisher of this book have used their *
57+
* best efforts in preparing the book. These efforts include the *
58+
* development, research, and testing of the theories and programs *
59+
* to determine their effectiveness. The authors and publisher make *
60+
* no warranty of any kind, expressed or implied, with regard to these *
61+
* programs or to the documentation contained in these books. The authors *
62+
* and publisher shall not be liable in any event for incidental or *
63+
* consequential damages in connection with, or arising out of, the *
64+
* furnishing, performance, or use of these programs. *
65+
**************************************************************************/

examples/ch13/fig13_04.cpp

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+

examples/ch13/fig13_05.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// fig13_05.cpp
2+
// Standard library deque class template.
3+
#include <algorithm> // copy algorithm
4+
#include <deque> // deque class-template definition
5+
#include <iostream>
6+
#include <iterator> // ostream_iterator
7+
8+
int main() {
9+
std::deque<double> values; // create deque of doubles
10+
std::ostream_iterator<double> output{std::cout, " "};
11+
12+
// insert elements in values
13+
values.push_front(2.2);
14+
values.push_front(3.5);
15+
values.push_back(1.1);
16+
17+
std::cout << "values contains: ";
18+
19+
// use subscript operator to obtain elements of values
20+
for (size_t i{0}; i < values.size(); ++i) {
21+
std::cout << values[i] << ' ';
22+
}
23+
24+
values.pop_front(); // remove first element
25+
std::cout << "\nAfter pop_front, values contains: ";
26+
std::ranges::copy(values, output);
27+
28+
// use subscript operator to modify element at location 1
29+
values[1] = 5.4;
30+
std::cout << "\nAfter values[1] = 5.4, values contains: ";
31+
std::ranges::copy(values, output);
32+
std::cout << "\n";
33+
}
34+
35+
36+
/**************************************************************************
37+
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
38+
* Pearson Education, Inc. All Rights Reserved. *
39+
* *
40+
* DISCLAIMER: The authors and publisher of this book have used their *
41+
* best efforts in preparing the book. These efforts include the *
42+
* development, research, and testing of the theories and programs *
43+
* to determine their effectiveness. The authors and publisher make *
44+
* no warranty of any kind, expressed or implied, with regard to these *
45+
* programs or to the documentation contained in these books. The authors *
46+
* and publisher shall not be liable in any event for incidental or *
47+
* consequential damages in connection with, or arising out of, the *
48+
* furnishing, performance, or use of these programs. *
49+
**************************************************************************/

examples/ch13/fig13_06.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// fig13_06.cpp
2+
// Standard library multiset class template
3+
#include <algorithm> // copy algorithm
4+
#include <fmt/format.h> // C++20: This will be #include <format>
5+
#include <iostream>
6+
#include <iterator> // ostream_iterator
7+
#include <ranges>
8+
#include <set> // multiset class-template definition
9+
#include <vector>
10+
11+
int main() {
12+
std::multiset<int, std::less<int>> ints{}; // multiset of int values
13+
std::cout << fmt::format("15s in ints: {}\n", ints.count(15));
14+
15+
std::cout << "\nInserting two 15s into ints\n";
16+
ints.insert(15); // insert 15 in ints
17+
ints.insert(15); // insert 15 in ints
18+
std::cout << fmt::format("15s in ints: {}\n\n", ints.count(15));
19+
20+
// search for 15 and 20 in ints; find returns an iterator
21+
for (int i : {15, 20}) {
22+
if (auto result{ints.find(i)}; result != ints.end()) {
23+
std::cout << fmt::format("Found {} in ints\n", i);
24+
}
25+
else {
26+
std::cout << fmt::format("Did not find {} in ints\n", i);
27+
}
28+
}
29+
30+
// search for 15 and 20 in ints; contains returns a bool
31+
for (int i : {15, 20}) {
32+
if (ints.contains(i)) {
33+
std::cout << fmt::format("Found {} in ints\n", i);
34+
}
35+
else {
36+
std::cout << fmt::format("Did not find {} in ints\n", i);
37+
}
38+
}
39+
40+
// insert elements of vector values into ints
41+
const std::vector values{7, 22, 9, 1, 18, 30, 100, 22, 85, 13};
42+
ints.insert(values.cbegin(), values.cend());
43+
std::cout << "\nAfter insert, ints contains:\n";
44+
std::ranges::copy(ints, std::ostream_iterator<int>{std::cout, " "});
45+
46+
// determine lower and upper bound of 22 in ints
47+
std::cout << fmt::format(
48+
"\n\nlower_bound(22): {}\nupper_bound(22): {}\n\n",
49+
*(ints.lower_bound(22)), *(ints.upper_bound(22)));
50+
51+
// use equal_range to determine lower and upper bound of 22 in ints
52+
auto p{ints.equal_range(22)};
53+
std::cout << fmt::format(
54+
"lower_bound(22): {}\nupper_bound(22): {}\n",
55+
*(p.first), *(p.second));
56+
}
57+
58+
59+
/**************************************************************************
60+
* (C) Copyright 1992-2021 by Deitel & Associates, Inc. and *
61+
* Pearson Education, Inc. All Rights Reserved. *
62+
* *
63+
* DISCLAIMER: The authors and publisher of this book have used their *
64+
* best efforts in preparing the book. These efforts include the *
65+
* development, research, and testing of the theories and programs *
66+
* to determine their effectiveness. The authors and publisher make *
67+
* no warranty of any kind, expressed or implied, with regard to these *
68+
* programs or to the documentation contained in these books. The authors *
69+
* and publisher shall not be liable in any event for incidental or *
70+
* consequential damages in connection with, or arising out of, the *
71+
* furnishing, performance, or use of these programs. *
72+
**************************************************************************/

0 commit comments

Comments
 (0)