Skip to content

Commit 49c79dc

Browse files
committed
Updated Chapters 7 examples
1 parent fcf6deb commit 49c79dc

File tree

9 files changed

+96
-86
lines changed

9 files changed

+96
-86
lines changed

examples/ch07/fig07_01.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int main() {
1010
cout << "The address of a is " << &a
1111
<< "\nThe value of aPtr is " << aPtr;
1212
cout << "\n\nThe value of a is " << a
13-
<< "\nThe value of *aPtr is " << *aPtr << endl;
13+
<< "\nThe value of *aPtr is " << *aPtr << '\n';
1414
}
1515

1616

examples/ch07/fig07_02.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// fig07_02.cpp
22
// Pass-by-value used to cube a variable's value.
3+
#include <fmt/format.h>
34
#include <iostream>
4-
using namespace std;
55

66
int cubeByValue(int n); // prototype
77

88
int main() {
99
int number{5};
1010

11-
cout << "The original value of number is " << number;
11+
std::cout << fmt::format("Original value of number is {}\n", number);
1212
number = cubeByValue(number); // pass number by value to cubeByValue
13-
cout << "\nThe new value of number is " << number << endl;
13+
std::cout << fmt::format("New value of number is {}\n", number);
1414
}
1515

1616
// calculate and return cube of integer argument
@@ -19,6 +19,7 @@ int cubeByValue(int n) {
1919
}
2020

2121

22+
2223
/**************************************************************************
2324
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
2425
* Pearson Education, Inc. All Rights Reserved. *

examples/ch07/fig07_03.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
// fig07_03.cpp
22
// Pass-by-reference with a pointer argument used to cube a
33
// variable's value.
4+
#include <fmt/format.h>
45
#include <iostream>
5-
using namespace std;
66

77
void cubeByReference(int* nPtr); // prototype
88

99
int main() {
1010
int number{5};
1111

12-
cout << "The original value of number is " << number;
12+
std::cout << fmt::format("Original value of number is {}\n", number);
1313
cubeByReference(&number); // pass number address to cubeByReference
14-
cout << "\nThe new value of number is " << number << endl;
14+
std::cout << fmt::format("New value of number is {}\n", number);
1515
}
1616

1717
// calculate cube of *nPtr; modifies variable number in main

examples/ch07/fig07_06.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
// fig07_06.cpp
22
// C++20: Creating std::arrays with to_array.
3+
#include <fmt/format.h>
34
#include <iostream>
45
#include <array>
5-
using namespace std;
66

77
int main() {
8-
// lambda to display a collection of items
9-
const auto display = [](const auto& items) {
10-
for (const auto& item : items) {
11-
cout << item << " ";
8+
// generic lambda to display a collection of items
9+
const auto display{
10+
[](const auto& items) {
11+
for (const auto& item : items) {
12+
std::cout << fmt::format("{} ", item);
13+
}
1214
}
1315
};
1416

15-
const int values1[3]{10, 20, 30};
17+
const int values1[]{10, 20, 30};
1618

1719
// creating a std::array from a built-in array
18-
const auto array1 = to_array(values1);
20+
const auto array1{std::to_array(values1)};
1921

20-
cout << "array1.size() = " << array1.size() << "\narray1: ";
22+
std::cout << fmt::format("array1.size() = {}\n", array1.size())
23+
<< "array1: ";
2124
display(array1); // use lambda to display contents
2225

2326
// creating a std::array from an initializer list
24-
const auto array2 = to_array({1, 2, 3, 4});
25-
cout << "\n\narray2.size() = " << array2.size() << "\narray2: ";
27+
const auto array2{std::to_array({1, 2, 3, 4})};
28+
std::cout << fmt::format("\n\narray2.size() = {}\n", array2.size())
29+
<< "array2: ";
2630
display(array2); // use lambda to display contents
2731

28-
cout << endl;
32+
std::cout << '\n';
2933
}
3034

3135

examples/ch07/fig07_10.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
// fig07_10.cpp
22
// Sizeof operator when used on a built-in array's name
33
// returns the number of bytes in the built-in array.
4+
#include <fmt/format.h>
45
#include <iostream>
5-
using namespace std;
66

77
size_t getSize(double* ptr); // prototype
88

99
int main() {
1010
double numbers[20]; // 20 doubles; occupies 160 bytes on our system
1111

12-
cout << "The number of bytes in the array is " << sizeof(numbers);
12+
std::cout << fmt::format("Number of bytes in numbers is {}\n\n",
13+
sizeof(numbers));
1314

14-
cout << "\nThe number of bytes returned by getSize is "
15-
<< getSize(numbers) << endl;
15+
std::cout << fmt::format("Number of bytes returned by getSize is {}\n",
16+
getSize(numbers));
1617
}
1718

1819
// return size of ptr
@@ -21,6 +22,7 @@ size_t getSize(double* ptr) {
2122
}
2223

2324

25+
2426
/**************************************************************************
2527
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *
2628
* Pearson Education, Inc. All Rights Reserved. *

examples/ch07/fig07_11.cpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// fig07_11.cpp
22
// sizeof operator used to determine standard data type sizes.
3+
#include <fmt/format.h>
34
#include <iostream>
4-
using namespace std;
55

66
int main() {
77
constexpr char c{}; // variable of type char
@@ -14,27 +14,26 @@ int main() {
1414
constexpr long double ld{}; // variable of type long double
1515
constexpr int array[20]{}; // built-in array of int
1616
const int* const ptr{array}; // variable of type int*
17-
18-
cout << "sizeof c = " << sizeof c
19-
<< "\tsizeof(char) = " << sizeof(char)
20-
<< "\nsizeof s = " << sizeof s
21-
<< "\tsizeof(short) = " << sizeof(short)
22-
<< "\nsizeof i = " << sizeof i
23-
<< "\tsizeof(int) = " << sizeof(int)
24-
<< "\nsizeof l = " << sizeof l
25-
<< "\tsizeof(long) = " << sizeof(long)
26-
<< "\nsizeof ll = " << sizeof ll
27-
<< "\tsizeof(long long) = " << sizeof(long long)
28-
<< "\nsizeof f = " << sizeof f
29-
<< "\tsizeof(float) = " << sizeof(float)
30-
<< "\nsizeof d = " << sizeof d
31-
<< "\tsizeof(double) = " << sizeof(double)
32-
<< "\nsizeof ld = " << sizeof ld
33-
<< "\tsizeof(long double) = " << sizeof(long double)
34-
<< "\nsizeof array = " << sizeof array
35-
<< "\nsizeof ptr = " << sizeof ptr << endl;
36-
}
3717

18+
std::cout << fmt::format("sizeof c = {}\tsizeof(char) = {}\n",
19+
sizeof c, sizeof(char));
20+
std::cout << fmt::format("sizeof s = {}\tsizeof(short) = {}\n",
21+
sizeof s, sizeof(short));
22+
std::cout << fmt::format("sizeof i = {}\tsizeof(int) = {}\n",
23+
sizeof i, sizeof(int));
24+
std::cout << fmt::format("sizeof l = {}\tsizeof(long) = {}\n",
25+
sizeof l, sizeof(long));
26+
std::cout << fmt::format("sizeof ll = {}\tsizeof(long long) = {}\n",
27+
sizeof ll, sizeof(long long));
28+
std::cout << fmt::format("sizeof f = {}\tsizeof(float) = {}\n",
29+
sizeof f, sizeof(float));
30+
std::cout << fmt::format("sizeof d = {}\tsizeof(double) = {}\n",
31+
sizeof d, sizeof(double));
32+
std::cout << fmt::format("sizeof ld = {}\tsizeof(long double) = {}\n",
33+
sizeof ld, sizeof(long double));
34+
std::cout << fmt::format("sizeof array = {}\n", sizeof array);
35+
std::cout << fmt::format("sizeof ptr = {}\n", sizeof ptr);
36+
}
3837

3938
/**************************************************************************
4039
* (C) Copyright 1992-2014 by Deitel & Associates, Inc. and *

examples/ch07/fig07_12.cpp

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,85 @@
11
// fig07_12.cpp
22
// C++20 spans: Creating views into containers.
33
#include <array>
4+
#include <fmt/format.h>
45
#include <iostream>
56
#include <numeric>
67
#include <span>
78
#include <vector>
8-
using namespace std;
99

1010
// items parameter is treated as a const int* so we also need the size to
1111
// know how to iterate over items with counter-controlled iteration
1212
void displayArray(const int items[], size_t size) {
1313
for (size_t i{0}; i < size; ++i) {
14-
cout << items[i] << " ";
14+
std::cout << fmt::format("{} ", items[i]);
1515
}
1616
}
1717

1818
// span parameter contains both the location of the first item
1919
// and the number of elements, so we can iterate using range-based for
20-
void displaySpan(span<const int> items) {
20+
void displaySpan(std::span<const int> items) {
2121
for (const auto& item : items) { // spans are iterable
22-
cout << item << " ";
22+
std::cout << fmt::format("{} ", item);
2323
}
2424
}
2525

2626
// spans can be used to modify elements in the original data structure
27-
void times2(span<int> items) {
27+
void times2(std::span<int> items) {
2828
for (int& item : items) {
2929
item *= 2;
3030
}
3131
}
3232

3333
int main() {
34-
int values1[5]{1, 2, 3, 4, 5};
35-
array<int, 5> values2{6, 7, 8, 9, 10};
36-
vector<int> values3{11, 12, 13, 14, 15};
34+
int values1[]{1, 2, 3, 4, 5};
35+
std::array values2{6, 7, 8, 9, 10};
36+
std::vector values3{11, 12, 13, 14, 15};
3737

3838
// must specify size because the compiler treats displayArray's items
3939
// parameter as a pointer to the first element of the argument
40-
cout << "values1 via displayArray: ";
41-
displayArray(values1, 5);
40+
std::cout << "values1 via displayArray: ";
41+
displayArray(values1, 5);
4242

43-
// compiler knows values' size and automatically creates a span
43+
// compiler knows values1's size and automatically creates a span
4444
// representing &values1[0] and the array's length
45-
cout << "\nvalues1 via displaySpan: ";
46-
displaySpan(values1);
45+
std::cout << "\nvalues1 via displaySpan: ";
46+
displaySpan(values1);
4747

4848
// compiler also can create spans from std::arrays and std::vectors
49-
cout << "\nvalues2 via displaySpan: ";
49+
std::cout << "\nvalues2 via displaySpan: ";
5050
displaySpan(values2);
51-
cout << "\nvalues3 via displaySpan: ";
51+
std::cout << "\nvalues3 via displaySpan: ";
5252
displaySpan(values3);
5353

5454
// changing a span's contents modifies the original data
5555
times2(values1);
56-
cout << "\n\nvalues1 after times2 modifies its span argument: ";
56+
std::cout << "\n\nvalues1 after times2 modifies its span argument: ";
5757
displaySpan(values1);
5858

5959
// spans have various array-and-vector-like capabilities
60-
span<int> mySpan{values1};
61-
cout << "\n\nmySpan's first element: " << mySpan.front()
60+
std::span mySpan{values1}; // span<int>
61+
std::cout << "\n\nmySpan's first element: " << mySpan.front()
6262
<< "\nmySpan's last element: " << mySpan.back();
6363

6464
// spans can be used with standard library algorithms
65-
cout << "\n\nSum of mySpan's elements: "
66-
<< accumulate(begin(mySpan), end(mySpan), 0);
65+
std::cout << "\n\nSum of mySpan's elements: "
66+
<< std::accumulate(std::begin(mySpan), std::end(mySpan), 0);
6767

6868
// spans can be used to create subviews of a container
69-
cout << "\n\nFirst three elements of mySpan: ";
69+
std::cout << "\n\nFirst three elements of mySpan: ";
7070
displaySpan(mySpan.first(3));
71-
cout << "\nLast three elements of mySpan: ";
71+
std::cout << "\nLast three elements of mySpan: ";
7272
displaySpan(mySpan.last(3));
73-
cout << "\nMiddle three elements of mySpan: ";
73+
std::cout << "\nMiddle three elements of mySpan: ";
7474
displaySpan(mySpan.subspan(1, 3));
75-
76-
// changing a subview's contents modifies the original data
75+
76+
// changing a subview's contents modifies the original data
7777
times2(mySpan.subspan(1, 3));
78-
cout << "\n\nvalues1 after modifying middle three elements via span: ";
78+
std::cout << "\n\nvalues1 after modifying elements via span: ";
7979
displaySpan(values1);
8080

8181
// access a span element via []
82-
cout << "\n\nThe element at index 2 is: " << mySpan[2];
83-
84-
// attempt to access an element outside the bounds
85-
cout << "\n\nThe element at index 10 is: " << mySpan[10] << endl;
82+
std::cout << "\n\nThe element at index 2 is: " << mySpan[2];
8683
}
8784

8885

examples/ch07/fig07_13.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
// fig07_13.cpp
22
// Reading in command-line arguments.
3+
#include <fmt/format.h>
34
#include <iostream>
4-
using namespace std;
55

66
int main(int argc, char* argv[]) {
7-
cout << "There were " << argc << " command-line arguments:\n";
7+
std::cout << fmt::format("Number of arguments: {}\n\n", argc);
8+
89
for (int i{0}; i < argc; ++i) {
9-
cout << argv[i] << endl;
10+
std::cout << fmt::format("{}\n", argv[i]);
1011
}
1112
}
1213

1314

15+
1416
/**************************************************************************
1517
* (C) Copyright 1992-2020 by Deitel & Associates, Inc. and *
1618
* Pearson Education, Inc. All Rights Reserved. *

examples/ch07/fig07_14.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
11
// fig07_14.cpp
22
// C++20: Creating std::arrays from string literals with to_array.
3+
#include <fmt/format.h>
34
#include <iostream>
45
#include <array>
5-
using namespace std;
66

77
int main() {
88
// lambda to display a collection of items
9-
const auto display = [](const auto& items) {
10-
for (const auto& item : items) {
11-
cout << item << " ";
12-
}
9+
const auto display{
10+
[](const auto& items) {
11+
for (const auto& item : items) {
12+
std::cout << fmt::format("{} ", item);
13+
}
14+
}
1315
};
1416

1517
// initializing an array with a string literal
1618
// creates a one-element array<const char*>
17-
const auto array1 = array{"abc"};
18-
cout << "array1.size() = " << array1.size() << "\narray1: ";
19+
const auto array1{std::array{"abc"}};
20+
std::cout << fmt::format("array1.size() = {}\narray1: ",
21+
array1.size());
1922
display(array1); // use lambda to display contents
2023

2124
// creating std::array of characters from a string literal
22-
const auto array2 = to_array("C++20");
23-
cout << "\n\narray2.size() = " << array2.size() << "\narray2: ";
25+
const auto array2{std::to_array("C++20")};
26+
std::cout << fmt::format("\n\narray2.size() = {}\narray2: ",
27+
array2.size());
2428
display(array2); // use lambda to display contents
2529

26-
cout << endl;
30+
std::cout << '\n';
2731
}
2832

2933

34+
3035
/**************************************************************************
3136
* (C) Copyright 1992-2020 by Deitel & Associates, Inc. and *
3237
* Pearson Education, Inc. All Rights Reserved. *

0 commit comments

Comments
 (0)