Skip to content

Commit 907ae9d

Browse files
committed
Chapter 15 Examples Posted
1 parent 5c9013a commit 907ae9d

File tree

21 files changed

+1286
-0
lines changed

21 files changed

+1286
-0
lines changed
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Fig. 16.1: Stack.h
2+
// Stack class template.
3+
#pragma once
4+
#include <deque>
5+
6+
template<typename T>
7+
class Stack {
8+
public:
9+
// return the top element of the Stack
10+
const T& top() {
11+
return stack.front();
12+
}
13+
14+
// push an element onto the Stack
15+
void push(const T& pushValue) {
16+
stack.push_front(pushValue);
17+
}
18+
19+
// pop an element from the stack
20+
void pop() {
21+
stack.pop_front();
22+
}
23+
24+
// determine whether Stack is empty
25+
bool isEmpty() const {
26+
return stack.empty();
27+
}
28+
29+
// return size of Stack
30+
size_t size() const {
31+
return stack.size();
32+
}
33+
34+
private:
35+
std::deque<T> stack; // internal representation of the Stack
36+
};
37+
38+
/**************************************************************************
39+
* (C) Copyright 1992-2017 by Deitel & Associates, Inc. and *
40+
* Pearson Education, Inc. All Rights Reserved. *
41+
* *
42+
* DISCLAIMER: The authors and publisher of this book have used their *
43+
* best efforts in preparing the book. These efforts include the *
44+
* development, research, and testing of the theories and programs *
45+
* to determine their effectiveness. The authors and publisher make *
46+
* no warranty of any kind, expressed or implied, with regard to these *
47+
* programs or to the documentation contained in these books. The authors *
48+
* and publisher shall not be liable in any event for incidental or *
49+
* consequential damages in connection with, or arising out of, the *
50+
* furnishing, performance, or use of these programs. *
51+
**************************************************************************/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// fig15_02.cpp
2+
// Stack class template test program.
3+
#include <iostream>
4+
#include "Stack.h" // Stack class template definition
5+
using namespace std;
6+
7+
int main() {
8+
Stack<double> doubleStack{}; // create a Stack of double
9+
constexpr size_t doubleStackSize{5}; // stack size
10+
double doubleValue{1.1}; // first value to push
11+
12+
cout << "Pushing elements onto doubleStack\n";
13+
14+
// push 5 doubles onto doubleStack
15+
for (size_t i{0}; i < doubleStackSize; ++i) {
16+
doubleStack.push(doubleValue);
17+
cout << doubleValue << ' ';
18+
doubleValue += 1.1;
19+
}
20+
21+
cout << "\n\nPopping elements from doubleStack\n";
22+
23+
// pop elements from doubleStack
24+
while (!doubleStack.isEmpty()) { // loop while Stack is not empty
25+
cout << doubleStack.top() << ' '; // display top element
26+
doubleStack.pop(); // remove top element
27+
}
28+
29+
cout << "\nStack is empty, cannot pop.\n";
30+
31+
Stack<int> intStack{}; // create a Stack of int
32+
constexpr size_t intStackSize{10}; // stack size
33+
int intValue{1}; // first value to push
34+
35+
cout << "\nPushing elements onto intStack\n";
36+
37+
// push 10 integers onto intStack
38+
for (size_t i{0}; i < intStackSize; ++i) {
39+
intStack.push(intValue);
40+
cout << intValue++ << ' ';
41+
}
42+
43+
cout << "\n\nPopping elements from intStack\n";
44+
45+
// pop elements from intStack
46+
while (!intStack.isEmpty()) { // loop while Stack is not empty
47+
cout << intStack.top() << ' '; // display top element
48+
intStack.pop(); // remove top element
49+
}
50+
51+
cout << "\nStack is empty, cannot pop.\n";
52+
}
53+
54+
55+
/**************************************************************************
56+
* (C) Copyright 1992-2017 by Deitel & Associates, Inc. and *
57+
* Pearson Education, Inc. All Rights Reserved. *
58+
* *
59+
* DISCLAIMER: The authors and publisher of this book have used their *
60+
* best efforts in preparing the book. These efforts include the *
61+
* development, research, and testing of the theories and programs *
62+
* to determine their effectiveness. The authors and publisher make *
63+
* no warranty of any kind, expressed or implied, with regard to these *
64+
* programs or to the documentation contained in these books. The authors *
65+
* and publisher shall not be liable in any event for incidental or *
66+
* consequential damages in connection with, or arising out of, the *
67+
* furnishing, performance, or use of these programs. *
68+
**************************************************************************/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// fig15_03.cpp
2+
// Abbreviated function template.
3+
#include <array>
4+
#include <iostream>
5+
#include <string>
6+
#include <vector>
7+
8+
// abbreviated function template printContainer displays a
9+
// container's elements separated by spaces.
10+
void printContainer(const auto& items) {
11+
for (const auto& item : items) {
12+
std::cout << item << " ";
13+
}
14+
}
15+
16+
int main() {
17+
using namespace std::string_literals; // for string object literals
18+
19+
std::array ints{1, 2, 3, 4, 5};
20+
std::vector strings{"red"s, "green"s, "blue"s};
21+
22+
std::cout << "ints: ";
23+
printContainer(ints);
24+
std::cout << "\nstrings: ";
25+
printContainer(strings);
26+
std::cout << "\n";
27+
}
28+
29+
30+
/**************************************************************************
31+
* (C) Copyright 1992-2020 by Deitel & Associates, Inc. and *
32+
* Pearson Education, Inc. All Rights Reserved. *
33+
* *
34+
* DISCLAIMER: The authors and publisher of this book have used their *
35+
* best efforts in preparing the book. These efforts include the *
36+
* development, research, and testing of the theories and programs *
37+
* to determine their effectiveness. The authors and publisher make *
38+
* no warranty of any kind, expressed or implied, with regard to these *
39+
* programs or to the documentation contained in these books. The authors *
40+
* and publisher shall not be liable in any event for incidental or *
41+
* consequential damages in connection with, or arising out of, the *
42+
* furnishing, performance, or use of these programs. *
43+
**************************************************************************/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// fig15_04.cpp
2+
// Simple unconstrained multiply function template.
3+
#include <concepts>
4+
#include <iostream>
5+
6+
template<typename T>
7+
T multiply(T first, T second) {
8+
return first * second;
9+
}
10+
11+
int main() {
12+
std::cout << "Product of 5 and 3: " << multiply(5, 3)
13+
<< "\nProduct of 7.25 and 2.0: " << multiply(7.25, 2.0) << "\n";
14+
15+
// std::string s1{"hi"};
16+
// std::string s2{"bye"};
17+
// auto result{multiply(s1, s2)}; // string does not have * operator
18+
}
19+
20+
21+
/**************************************************************************
22+
* (C) Copyright 1992-2020 by Deitel & Associates, Inc. and *
23+
* Pearson Education, Inc. All Rights Reserved. *
24+
* *
25+
* DISCLAIMER: The authors and publisher of this book have used their *
26+
* best efforts in preparing the book. These efforts include the *
27+
* development, research, and testing of the theories and programs *
28+
* to determine their effectiveness. The authors and publisher make *
29+
* no warranty of any kind, expressed or implied, with regard to these *
30+
* programs or to the documentation contained in these books. The authors *
31+
* and publisher shall not be liable in any event for incidental or *
32+
* consequential damages in connection with, or arising out of, the *
33+
* furnishing, performance, or use of these programs. *
34+
**************************************************************************/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// fig15_05.cpp
2+
// Constrained multiply function template that allows
3+
// only integers and floating-point values.
4+
#include <concepts>
5+
#include <iostream>
6+
7+
template<typename T>
8+
requires std::integral<T> || std::floating_point<T>
9+
T multiply(T first, T second) {
10+
return first * second;
11+
}
12+
13+
int main() {
14+
std::cout << "Product of 5 and 3: " << multiply(5, 3)
15+
<< "\nProduct of 7.25 and 2.0: " << multiply(7.25, 2.0) << "\n";
16+
17+
//std::string s1{"hi"};
18+
//std::string s2{"bye"};
19+
//auto result{multiply(s1, s2)};
20+
}
21+
22+
23+
/**************************************************************************
24+
* (C) Copyright 1992-2020 by Deitel & Associates, Inc. and *
25+
* Pearson Education, Inc. All Rights Reserved. *
26+
* *
27+
* DISCLAIMER: The authors and publisher of this book have used their *
28+
* best efforts in preparing the book. These efforts include the *
29+
* development, research, and testing of the theories and programs *
30+
* to determine their effectiveness. The authors and publisher make *
31+
* no warranty of any kind, expressed or implied, with regard to these *
32+
* programs or to the documentation contained in these books. The authors *
33+
* and publisher shall not be liable in any event for incidental or *
34+
* consequential damages in connection with, or arising out of, the *
35+
* furnishing, performance, or use of these programs. *
36+
**************************************************************************/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// fig15_06.cpp
2+
// Using type traits to test whether types are
3+
// integral types, floating-point types or arithmetic types.
4+
#include <fmt/format.h>
5+
#include <iostream>
6+
#include <string>
7+
#include <type_traits>
8+
9+
int main() {
10+
std::cout << fmt::format("{}\n{}{}\n{}{}\n{}{}\n{}{}\n{}{}\n\n",
11+
"CHECK WITH TYPE TRAITS WHETHER TYPES ARE INTEGRAL",
12+
"std::is_integral<int>::value: ", std::is_integral<int>::value,
13+
"std::is_integral_v<int>: ", std::is_integral_v<int>,
14+
"std::is_integral_v<long>: ", std::is_integral_v<long>,
15+
"std::is_integral_v<float>: ", std::is_integral_v<float>,
16+
"std::is_integral_v<std::string>: ",
17+
std::is_integral_v<std::string>);
18+
19+
std::cout << fmt::format("{}\n{}{}\n{}{}\n{}{}\n{}{}\n{}{}\n\n",
20+
"CHECK WITH TYPE TRAITS WHETHER TYPES ARE FLOATING POINT",
21+
"std::is_floating_point<float>::value: ",
22+
std::is_floating_point<float>::value,
23+
"std::is_floating_point_v<float>: ",
24+
std::is_floating_point_v<float>,
25+
"std::is_floating_point_v<double>: ",
26+
std::is_floating_point_v<double>,
27+
"std::is_floating_point_v<int>: ",
28+
std::is_floating_point_v<int>,
29+
"std::is_floating_point_v<std::string>: ",
30+
std::is_floating_point_v<std::string>);
31+
32+
std::cout << fmt::format("{}\n{}{}\n{}{}\n{}{}\n{}{}\n",
33+
"CHECK WITH TYPE TRAITS WHETHER TYPES CAN BE USED IN ARITHMETIC",
34+
"std::is_arithmetic<int>::value: ", std::is_arithmetic<int>::value,
35+
"std::is_arithmetic_v<int>: ", std::is_arithmetic_v<int>,
36+
"std::is_arithmetic_v<double>: ", std::is_arithmetic_v<double>,
37+
"std::is_arithmetic_v<std::string>: ",
38+
std::is_arithmetic_v<std::string>);
39+
}
40+
41+
42+
/**************************************************************************
43+
* (C) Copyright 1992-2020 by Deitel & Associates, Inc. and *
44+
* Pearson Education, Inc. All Rights Reserved. *
45+
* *
46+
* DISCLAIMER: The authors and publisher of this book have used their *
47+
* best efforts in preparing the book. These efforts include the *
48+
* development, research, and testing of the theories and programs *
49+
* to determine their effectiveness. The authors and publisher make *
50+
* no warranty of any kind, expressed or implied, with regard to these *
51+
* programs or to the documentation contained in these books. The authors *
52+
* and publisher shall not be liable in any event for incidental or *
53+
* consequential damages in connection with, or arising out of, the *
54+
* furnishing, performance, or use of these programs. *
55+
**************************************************************************/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// fig15_06.cpp
2+
// Constrained multiply abbreviated function template.
3+
#include <concepts>
4+
#include <iostream>
5+
6+
// Numeric concept aggregates std::integral and std::floating_point
7+
template<typename T>
8+
concept Numeric = std::integral<T> || std::floating_point<T>;
9+
10+
// abbreviated function template with constrained auto
11+
auto multiply(Numeric auto first, Numeric auto second) {
12+
return first * second;
13+
}
14+
15+
int main() {
16+
std::cout << "Product of 5 and 3: " << multiply(5, 3)
17+
<< "\nProduct of 7.25 and 2.0: " << multiply(7.25, 2.0)
18+
<< "\nProduct of 5 and 7.25: " << multiply(5, 7.25) << "\n";
19+
}
20+
21+
/**************************************************************************
22+
* (C) Copyright 1992-2020 by Deitel & Associates, Inc. and *
23+
* Pearson Education, Inc. All Rights Reserved. *
24+
* *
25+
* DISCLAIMER: The authors and publisher of this book have used their *
26+
* best efforts in preparing the book. These efforts include the *
27+
* development, research, and testing of the theories and programs *
28+
* to determine their effectiveness. The authors and publisher make *
29+
* no warranty of any kind, expressed or implied, with regard to these *
30+
* programs or to the documentation contained in these books. The authors *
31+
* and publisher shall not be liable in any event for incidental or *
32+
* consequential damages in connection with, or arising out of, the *
33+
* furnishing, performance, or use of these programs. *
34+
**************************************************************************/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// fig15_07.cpp
2+
// Using concepts to select overloads.
3+
#include <array>
4+
#include <iostream>
5+
#include <iterator>
6+
#include <list>
7+
#include <ranges>
8+
9+
// calculate the distance (number of items) between two iterators
10+
// using input iterators; requires incrementing between iterators,
11+
// so this is an O(n) operation
12+
auto customDistance(std::input_iterator auto begin,
13+
std::input_iterator auto end) {
14+
std::cout << "Called customDistance with bidirectional iterators\n";
15+
std::ptrdiff_t count{0};
16+
17+
// increment from begin to end and count number of iterations
18+
for (auto& iter{begin}; iter != end; ++iter) {
19+
++count;
20+
}
21+
22+
return count;
23+
}
24+
25+
// calculate the distance (number of items) between two iterators
26+
// using random-access iterators and an O(1) operation
27+
auto customDistance(std::random_access_iterator auto begin,
28+
std::random_access_iterator auto end) {
29+
std::cout << "Called customDistance with random-access iterators\n";
30+
return end - begin;
31+
}
32+
33+
int main() {
34+
const std::array ints1{1, 2, 3, 4, 5}; // has random-access iterators
35+
const std::list ints2{1, 2, 3}; // has bidirectional iterators
36+
37+
auto result1{customDistance(ints1.begin(), ints1.end())};
38+
std::cout << "ints1 number of elements: " << result1 << "\n";
39+
auto result2{customDistance(ints2.begin(), ints2.end())};
40+
std::cout << "ints1 number of elements: " << result2 << "\n";
41+
}
42+
43+
/**************************************************************************
44+
* (C) Copyright 1992-2020 by Deitel & Associates, Inc. and *
45+
* Pearson Education, Inc. All Rights Reserved. *
46+
* *
47+
* DISCLAIMER: The authors and publisher of this book have used their *
48+
* best efforts in preparing the book. These efforts include the *
49+
* development, research, and testing of the theories and programs *
50+
* to determine their effectiveness. The authors and publisher make *
51+
* no warranty of any kind, expressed or implied, with regard to these *
52+
* programs or to the documentation contained in these books. The authors *
53+
* and publisher shall not be liable in any event for incidental or *
54+
* consequential damages in connection with, or arising out of, the *
55+
* furnishing, performance, or use of these programs. *
56+
**************************************************************************/

0 commit comments

Comments
 (0)