ch07 samples were modified

This commit is contained in:
davoudn 2024-05-07 09:31:45 +03:30
parent c5ee50737c
commit 5a612f8354
7 changed files with 74 additions and 11 deletions

View File

@ -29,6 +29,16 @@ T integrate (std::array<T, N> x, F f){
}
auto integrate_auto (auto x, auto f){
auto tmp{x[0]};
tmp = 0.0;
for (auto s: x){
tmp+= f(s);
}
return tmp;
}
typedef double (*FUNC)(double&);
@ -71,6 +81,8 @@ int main() {
std::cout << "Integration full template " << integrate(floats, f_lambda) << "\n";
std::cout << "Integration auto " << integrate_auto(floats, f_lambda) << "\n";
std::cout << "Lambda test " << g_lambda(floats) << "\n";
}

View File

@ -2,6 +2,7 @@
// Pointer operators & and *.
#include <iostream>
#include <array>
#include <memory>
using namespace std;
int main() {
@ -12,6 +13,9 @@ int main() {
<< "\nThe value of aPtr is " << aPtr;
cout << "\n\nThe value of a is " << a
<< "\nThe value of *aPtr is " << *aPtr << '\n';
std::shared_ptr < std::array<int,10> > b = std::make_shared< std::array<int,10> > ();
std::cout << (*b)[3] << " " << b->at(5) <<"\n";
}

View File

@ -1,24 +1,49 @@
// fig07_03.cpp
// Pass-by-reference with a pointer argument used to cube a
// variable's value.
#include <fmt/format.h>
#include <iostream>
void cubeByReference(int* nPtr); // prototype
void cubeByValue(int x);
void cubeByReference(int& x); // prototype
void cubeByPointer(int* nPtr);
int main() {
int number{5};
std::cout << "Original value of number is " << number << "\n";
cubeByValue(number); // pass number by value to cubeByValue
cubeByPointer(&number); // pass number by value to cubeByValue
std::cout << "New value of number is " << number <<"\n";
int number1{5};
std::cout << "Original value of number is " << number1 << "\n";
cubeByReference(number1); // pass number by value to cubeByValue
std::cout << "New value of number is " << number1 <<"\n";
int number2{5};
std::cout << "Original value of number is " << number2 << "\n";
cubeByValue(number2); // pass number by value to cubeByValue
std::cout << "New value of number is " << number2 <<"\n";
}
// calculate cube of *nPtr; modifies variable number in main
void cubeByReference(int* nPtr) {
void cubeByPointer(int* nPtr) {
*nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr
}
void cubeByValue(int x) {
x = x * x * x; // cube *nPtr
}
void cubeByReference(int& x) {
x = x * x * x; // cube *nPtr
}
/**************************************************************************
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *

View File

@ -1,6 +1,5 @@
// fig07_06.cpp
// C++20: Creating std::arrays with to_array.
#include <fmt/format.h>
#include <iostream>
#include <array>
@ -9,8 +8,9 @@ int main() {
const auto display{
[](const auto& items) {
for (const auto& item : items) {
std::cout << fmt::format("{} ", item);
std::cout << item << " ";
}
std::cout << "\n";
}
};
@ -19,14 +19,12 @@ int main() {
// creating a std::array from a built-in array
const auto array1{std::to_array(values1)};
std::cout << fmt::format("array1.size() = {}\n", array1.size())
<< "array1: ";
std::cout << "array1.size(): " << array1.size() << " array1: ";
display(array1); // use lambda to display contents
// creating a std::array from an initializer list
const auto array2{std::to_array({1, 2, 3, 4})};
std::cout << fmt::format("\n\narray2.size() = {}\n", array2.size())
<< "array2: ";
std::cout << "array2.size(): " << array2.size() << " array2: ";
display(array2); // use lambda to display contents
std::cout << '\n';

View File

@ -2,19 +2,38 @@
// Sizeof operator when used on a built-in array's name
// returns the number of bytes in the built-in array.
#include <iostream>
#include <span>
size_t getSize(double* ptr); // prototype
size_t getSize_span(std::span<double> ptr);
void print_arr(auto a){
for (auto x: a)
std::cout << x << " ";
}
void fill_arr(auto& a){
for (auto x: a)
x = 0;
}
int main() {
double numbers[20]; // 20 doubles; occupies 160 bytes on our system
auto numbers_span {std::span<double>(numbers)};
fill_arr(numbers_span);
std::cout << "Number of bytes in numbers is " << sizeof(numbers)<<"\n";
std::cout << "Actual nukber of elements : " << sizeof(numbers)/sizeof(double) << "\n";
std::cout << "Actual number of elements : " << sizeof(numbers)/sizeof(double) << "\n";
std::cout << "Number of bytes returned by getSize is " << getSize(numbers) << "\n";
std::cout << "Actual number of elements returned by getSize_span is " << getSize_span(numbers) << "\n";
print_arr(numbers_span);
}
// return size of ptr
@ -22,6 +41,11 @@ size_t getSize(double* ptr) {
return sizeof(ptr);
}
size_t getSize_span(std::span<double> ptr) {
return ptr.size();
}
/**************************************************************************