-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmyVectorClient.cpp
More file actions
112 lines (87 loc) · 2.18 KB
/
myVectorClient.cpp
File metadata and controls
112 lines (87 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**********************************************************
CS211
myVector client
template created by Kazumi Slott
3-13-2020
This program will test the myVector class
***********************************************************/
#include <iostream>
using namespace std;
//A. include the header file that contains myVector class definishion and function implementations
#include "myVector.h"
//B. Create a cat struct that has name (string) and id (int)
struct cat
{
string name;
int id;
};
//C. Create an operator overloading function for >> to fill a cat object
istream& operator>>(istream& i, cat& c)
{
cout << "Enter a cat name: ";
i >> c.name;
cout << "Enter a cat id: ";
i >> c.id;
return i;
}
//D. Create an operator overloading function for << to print a cat object
// e.g. Tom (1)
ostream& operator<<(ostream& o, const cat& c)
{
o << c.name << "(" << c.id << ") ";
return o;
}
template <class T>
void print(const myVector<T>& vector);
int main()
{
myVector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
cout << "---- 1 2 3 push_back ---- " << endl;
print(v);
v.erase(1);
cout << "---- 1 3 erase ----" << endl;
print(v);
cout << "--- illegal index 1 3 erase ---" << endl;
v.erase(2);
print(v);
v.erase(0);
cout << "--- 3 erase ----" << endl;
print(v);
v.insert(1, 9);
cout << "--- 3 9 insert ---" << endl;
print(v);
v.insert(1, 8);
cout << "--- 3 8 9 insert ---" << endl;
print(v);
v.insert(0, 7);
cout << "--- 7 3 8 9 insert ---" << endl;
print(v);
cout << "--- illegal index 7 3 8 9 insert ---" << endl;
v.insert(5, 6);
print(v);
cout << endl;
//E. Create a vector that will hold cats
myVector<cat> vc;
cat c; //create a cat struct
//F. Add 3 cats
for(int i = 0; i < 3; i++)
{
cin >> c; //fill the cat struct
vc.push_back(c); //add the filled cat into the vector
}
//G. print all the cats in the cat vector
print(vc);
}
//H. Create a template print function that will print all the elements from a vector of any data type
template <class T>
void print(const myVector<T>& vector)
{
for(int i = 0; i < vector.size(); i++)
{
cout << vector[i];
}
cout << endl;
}