Skip to content

Commit

Permalink
提交原书剩余代码
Browse files Browse the repository at this point in the history
  • Loading branch information
ShujiaHuang committed Aug 16, 2021
1 parent ab5b8fc commit 16010f0
Show file tree
Hide file tree
Showing 22 changed files with 1,109 additions and 0 deletions.
9 changes: 9 additions & 0 deletions source/Appendix_E/align.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>
using namespace std;

int main()
{
cout << alignof(double) << endl;
cin.get();
return 0;
}
78 changes: 78 additions & 0 deletions source/Appendix_E/memb_pt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// memb_pt.cpp -- dereferencing pointers to class members
#include <iostream>
using namespace std;

class Example
{
private:
int feet;
int inches;
public:
Example();
Example(int ft);
~Example();
void show_in() const;
void show_ft() const;
void use_ptr() const;
};

Example::Example()
{
feet = 0;
inches = 0;
}

Example::Example(int ft)
{
feet = ft;
inches = 12 * feet;
}

Example::~Example()
{
}

void Example::show_in() const
{
cout << inches << " inches\n";
}

void Example::show_ft() const
{
cout << feet << " feet\n";
}

void Example::use_ptr() const
{
Example yard(3);
int Example::*pt;
pt = &Example::inches;
cout << "Set pt to &Example::inches:\n";
cout << "this->pt: " << this->*pt << endl;
cout << "yard.*pt: " << yard.*pt << endl;
pt = &Example::feet;
cout << "Set pt to &Example::feet:\n";
cout << "this->pt: " << this->*pt << endl;
cout << "yard.*pt: " << yard.*pt << endl;
void (Example::*pf)() const;
pf = &Example::show_in;
cout << "Set pf to &Example::show_in:\n";
cout << "Using (this->*pf)(): ";
(this->*pf)();
cout << "Using (yard.*pf)(): ";
(yard.*pf)();
}

int main()
{
Example car(15);
Example van(20);
Example garage;

cout << "car.use_ptr() output:\n";
car.use_ptr();
cout << "\nvan.use_ptr() output:\n";
van.use_ptr();
cin.get();
return 0;
}
40 changes: 40 additions & 0 deletions source/Appendix_E/noex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
#include <string>
#include <cmath>
#include <numeric>
#include <vector>
#include <algorithm>
#include <unordered_set>
using namespace std;
struct S {double a; double b;};
int main()
{
S a = {1.1,2.2};
S b = {2.1,4.2};
S c = {3.1,5.2};

unordered_set<S*> us;
us.insert(&a);
us.insert(&b);
us.insert(&c);
for_each(us.begin(),us.end(),[](S* i) {cout << (*i).a << ", ";});
cout << "\nNext\n";


/* if(pt != vi.end())
cout << *pt << endl;
else
cout << "not found\n";
cout << vi.count("cow") << endl;
cout << vi.count("fondu") << endl;
cout << endl;
cout << *vi.lower_bound("cow") << endl;
cout << *vi.upper_bound("hen") << endl;
cout << vi.bucket_count() << endl;
cout << vi.bucket("cow") << endl;
cout << vi.bucket("starkA") << endl;
cout << vi.bucket("stark") << endl;*/

cin.get();

}
23 changes: 23 additions & 0 deletions source/Appendix_G/appg01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
vector<string> input;
string temp;
while (cin >> temp && temp != "quit")
input.push_back(temp);
vector<string>::iterator want=
find(input.begin(), input.end(), string("bonus"));
if (want != input.end())
{
vector<string>::reference r = *want;
r = "bogus";
}
for_each(input.begin(), input.end(), [](string s){cout << s << ", ";});
cin.get();
cin.get();
}
29 changes: 29 additions & 0 deletions source/Appendix_G/appg02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

template<typename Bag>
typename Bag::value_type min(const Bag & b)
{
typename Bag::const_iterator it;
typename Bag::value_type m = *b.begin();
for (it = b.begin(); it != b.end(); ++it)
if (*it < m)
m = *it;
return m;
}

int main()
{
vector<int> temperatures;
int temp;
while (cin >> temp && temp >-274)
temperatures.push_back(temp);
int coldest = min(temperatures);
for_each(temperatures.begin(), temperatures.end(), [](int s){cout << s << ", ";});
cout << endl << coldest << endl;
cin.get();
cin.get();
}
57 changes: 57 additions & 0 deletions source/Appendix_G/appg04.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// setops.cpp -- some set operations
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <iterator>

int main()
{
using namespace std;
const int N = 6;
string s1[N] = {"buffoon", "thinkers", "for", "heavy", "can", "for"};
string s2[N] = {"metal", "any", "food", "elegant", "deliver","for"};

set<string> A(s1, s1 + N);
set<string> B(s2, s2 + N);
A.insert("buffalo");
ostream_iterator<string, char> out(cout, " ");
cout << "Set A: ";
copy(A.begin(), A.end(), out);
cout << endl;
cout << "Set B: ";
copy(B.begin(), B.end(), out);
cout << endl;

cout << "Union of A and B:\n";
set_union(A.begin(), A.end(), B.begin(), B.end(), out);
cout << endl;

cout << "Intersection of A and B:\n";
set_intersection(A.begin(), A.end(), B.begin(), B.end(), out);
cout << endl;

cout << "Difference of A and B:\n";
set_difference(A.begin(), A.end(), B.begin(), B.end(), out);
cout << endl;

set<string> C;
cout << "Set C:\n";
set_union(A.begin(), A.end(), B.begin(), B.end(),
insert_iterator<set<string> >(C, C.begin()));
copy(C.begin(), C.end(), out);
cout << endl;

string s3("grungy");
C.insert(s3);
cout << "Set C after insertion:\n";
copy(C.begin(), C.end(),out);
cout << endl;

cout << "Showing a range:\n";
copy(C.lower_bound("ghost"),C.upper_bound("spook"), out);
cout << endl;
cin.get();
cin.get();
return 0;
}
27 changes: 27 additions & 0 deletions source/Appendix_G/appg3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Items
{
double x;
double y;
int m;
public:
Items() : x(0),y(0), m(0){}; // #1
Items (double xx, double yy, int mm): x(xx),y(yy), m(mm){}; // #2
void Show() const { cout << x << ", " << y << ", " << m << endl;}
};
int main()
{
vector<Items> vt(5);

for_each( vt.begin(), vt.end(), [](const Items & i){i.Show();});

vt.push_back( Items(8.2, 2.8, 3)); //
for_each( vt.begin(), vt.end(), [](const Items & i){i.Show();});
vt.emplace_back( 8.2, 2.8, 3); //
for_each( vt.begin(), vt.end(), [](const Items & i){i.Show();});
cin.get();
}
26 changes: 26 additions & 0 deletions source/Appendix_G/perfectf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Items
{
double x;
double y;
int m;
public:
Items() : x(0),y(0), m(0){}; // #1
Items (double xx, double yy, int mm): x(xx),y(yy), m(mm){}; // #2
void Show() const { cout << x << ", " << y << ", " << m << endl;}
};

template<typename...Args>
void dumb(int i, Args... args)
{
cout << i << endl;
Items(args...).Show();
}
int main()
{
dumb(10, 2.2,4.4,1);
}
52 changes: 52 additions & 0 deletions source/chapter18/callable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// callable.cpp -- callable types and templates
#include <iostream>
#include <math.h>
using namespace std;

template <typename T, typename F>
T use_f(T v, F f)
{
static int count = 0;
count++;
cout << " use_f count = " << count << ", &count = " << &count << endl;
return f(v);
}

class Fp
{
private:
double z_;
public:
Fp(double z = 1.0) : z_(z) {}
double operator()(double p) { return z_*p; }
};
class Fq
{
private:
double z_;
public:
Fq(double z = 1.0) : z_(z) {}
double operator()(double q) { return z_+ q; }
};

double dub(double x) {return 2.0*x;}

int main()
{
double y = 1.21;
cout << "Function pointer dub:\n";
cout << " " << use_f(y, dub) << endl;
cout << "Function pointer sqrt:\n";
cout << " " << use_f(y, sqrt) << endl;
cout << "Function object Fp:\n";
cout << " " << use_f(y, Fp(5.0)) << endl;
cout << "Function object Fq:\n";
cout << " " << use_f(y, Fq(5.0)) << endl;
cout << "Lambda expression 1:\n";
cout << " " << use_f(y, [](double u) {return u*u;}) << endl;
cout << "Lambda expresson 2:\n";
cout << " " << use_f(y, [](double u) {return u+u/2.0;}) << endl;

cin.get();
return 0;
}
Loading

0 comments on commit 16010f0

Please sign in to comment.