Skip to content

Commit 1891081

Browse files
committed
update new implement functions
1 parent 5e532e3 commit 1891081

File tree

4 files changed

+228
-0
lines changed

4 files changed

+228
-0
lines changed

list.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// list.cpp -- using a list
2+
#include <iostream>
3+
#include <list>
4+
#include <iterator>
5+
6+
int main()
7+
{
8+
using namespace std;
9+
list<int> one (5, 2);
10+
int stuff[5] = {1, 2, 4, 8, 6};
11+
list<int> two;
12+
two.insert (two.begin(), stuff, stuff + 5);
13+
int more[6] = {6, 4, 2, 4, 6, 5};
14+
list<int> three (two);
15+
three.insert(three.end(), more, more + 6);
16+
17+
cout << "List one: ";
18+
ostream_iterator<int, char> out (cout, " ");
19+
copy (one.begin(), one.end(), out);
20+
cout << endl << "List two: ";
21+
copy (two.begin(), two.end(), out);
22+
cout << endl << "List three: ";
23+
copy (three.begin(), three.end(), out);
24+
three.remove (2);
25+
cout << endl << "List three minus 2s: ";
26+
copy (three.begin(), three.end(), out);
27+
three.splice (three.begin(), one);
28+
cout << endl << "List three after splices: ";
29+
copy (three.begin(), three.end(), out);
30+
cout << endl << "List one: ";
31+
copy (one.begin(), one.end(), out);
32+
three.unique();
33+
cout << endl << "List three after unique: ";
34+
copy (three.begin(), three.end(), out);
35+
three.sort();
36+
three.unique();
37+
cout << endl << "List three after sort & unique: ";
38+
copy (three.begin(), three.end(), out);
39+
two.sort();
40+
three.merge(two);
41+
cout << endl << "Sorted two merged into three: ";
42+
copy (three.begin(), three.end(), out);
43+
cout << endl;
44+
45+
return 0;
46+
}

memb_pt.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// memb_pt.cpp -- dereferencing pointers to class members
2+
#include <iostream>
3+
using namespace std;
4+
5+
class Example
6+
{
7+
private:
8+
int feet;
9+
int inches;
10+
public:
11+
Example();
12+
Example (int ft);
13+
~Example();
14+
void show_in()const;
15+
void show_ft()const;
16+
void use_ptr()const;
17+
};
18+
19+
Example:: Example()
20+
{
21+
feet = 0;
22+
inches = 0;
23+
}
24+
25+
Example:: Example (int ft)
26+
{
27+
feet = ft;
28+
inches = 12 * feet;
29+
}
30+
31+
Example:: ~Example()
32+
{
33+
}
34+
35+
void Example:: show_in()const
36+
{
37+
cout << inches << " inches\n";
38+
}
39+
40+
void Example:: show_ft()const
41+
{
42+
cout << feet << " feet\n";
43+
}
44+
45+
void Example:: use_ptr()const
46+
{
47+
Example yard (3);
48+
int Example:: *pt;
49+
pt = &Example:: inches;
50+
cout << "Set pt to &Example:: inches: \n";
51+
cout << "this->pt: " << this->*pt << endl;
52+
cout << "yard.*pt: " << yard.*pt << endl;
53+
pt = &Example:: feet;
54+
cout << "Set pt to &Example:: feet: \n";
55+
cout << "this->pt: " << this->*pt << endl;
56+
cout << "yard.*pt" << yard.*pt << endl;
57+
void(Example:: *pf) ()const;
58+
pf = &Example:: show_in;
59+
cout << "Set pf to &Example:: show_in: \n";
60+
cout << "Using (this->*pf) (): "; // apply static member function
61+
(this->*pf) ();
62+
cout << "Using (yard.*pf) (): ";
63+
(yard.*pf) ();
64+
}
65+
66+
int main()
67+
{
68+
Example car (15);
69+
Example van (20);
70+
Example garage;
71+
cout << "car.use_ptr()output: \n";
72+
car.use_ptr();
73+
cout << "\nvan.use_ptr()output: \n";
74+
van.use_ptr();
75+
76+
return 0;
77+
}

newplace.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
There are four methods to use new operation
3+
#include <new>
4+
struct chaff
5+
{
6+
char dross[20];
7+
int slag;
8+
};
9+
char buffer1[50];
10+
char buffer2[500];
11+
int main()
12+
{
13+
chaff *p1, *p2;
14+
int *p3, *p4;
15+
// first, the reuglar forms of new
16+
p1 = new chaff; // place a structure in heap
17+
p3 = new int[20]; // place int array in heap
18+
// now, the two forms of placement new
19+
p2 = new (buffer1) chaff; // place structure in buffer1
20+
p4 = new (buffer2) int[20]; // place int array in buffer2
21+
}
22+
*/
23+
24+
// newplace.cpp -- using placement new
25+
#include <iostream>
26+
#include <new>
27+
const int BUF = 512;
28+
const int N = 5;
29+
char buffer[BUF];
30+
int main()
31+
{
32+
using namespace std;
33+
double *pd1, *pd2;
34+
int i;
35+
cout << "Calling new and placement new:\n";
36+
pd1 = new double[N]; // use heap
37+
pd2 = new (buffer) double[N]; // use buffer array
38+
for (i = 0; i< N; i++) {
39+
pd2[i] = pd1[i] = 1000 + 20.0 * i;
40+
}
41+
cout << "Buffer addresses:\n" << " heap: " << pd1
42+
<< " static: " << (void *) buffer << endl;
43+
cout << "Buffer contents:\n";
44+
for (i = 0; i < N; i++) {
45+
cout << pd1[i] << " at " << &pd1[i] << "; ";
46+
cout << pd2[i] << " at " << &pd2[i] << endl;
47+
}
48+
49+
cout << "\nCalling new and placement new a second time:\n";
50+
double *pd3, *pd4;
51+
pd3 = new double[N];
52+
pd4 = new (buffer) double[N];
53+
for (i = 0; i < N; i++) {
54+
pd4[i] = pd3[i] = 1000 + 20.0 * i;
55+
}
56+
cout << "Buffer contents:\n";
57+
for (i = 0; i < N; i++) {
58+
cout << pd3[i] << " at " << &pd3[i] << "; ";
59+
cout << pd4[i] << " at " << &pd4[i] << endl;
60+
}
61+
62+
cout << "\nCalling new and placement new a third time:\n";
63+
delete [] pd1;
64+
pd1 = new double[N];
65+
pd2 = new (buffer + N * sizeof(double)) double[N];
66+
for (i = 0; i < N; i++) {
67+
pd2[i] = pd1[i] = 1000 + 20.0 * i;
68+
}
69+
cout << "Buffer contents:\n";
70+
for (i = 0; i < N; i++) {
71+
cout << pd1[i] << " at " << &pd1[i] << "; ";
72+
cout << pd2[i] << " at " << &pd2[i] << endl;
73+
}
74+
delete [] pd1;
75+
delete [] pd3;
76+
77+
return 0;
78+
}

stfile.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// strfile.cpp -- read strings from a file
2+
#include <iostream>
3+
#include <fstream>
4+
#include <string>
5+
#include <cstdlib>
6+
int main()
7+
{
8+
using namespace std;
9+
ifstream fin;
10+
fin.open("tobuy.txt");
11+
if (fin.is_open() == false) {
12+
cerr << "Can't open file. Bye.\n";
13+
exit(EXIT_FAILURE);
14+
}
15+
string item;
16+
int count = 0;
17+
18+
getline(fin, item, ':');
19+
while (fin) {
20+
++count;
21+
cout << count << ": " << item << endl;
22+
getline(fin, item, ':');
23+
}
24+
cout << "Done\n";
25+
fin.close();
26+
return 0;
27+
}

0 commit comments

Comments
 (0)