-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvect2.cpp
80 lines (75 loc) · 1.68 KB
/
vect2.cpp
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
// vect2.cpp -- moethods and iterators
#include <iostream>
#include <string>
#include <vector>
struct Review {
std::string title;
int rating;
};
bool FillReview (Review & rr);
void ShowReview (const Review & rr);
int main()
{
using std::cout;
using std::vector;
vector<Review> books;
Review temp;
while (FillReview(temp)) {
books.push_back(temp);
}
int num = books.size();
if (num > 0) {
cout << "Thank you, You entered the following:\n"
<< "Rating\tBook\n";
for (int i = 0; i < num; i++) {
ShowReview(books[i]);
}
cout << "Reprising:\n"
<< "Rating\tBook\n";
vector<Review>::iterator pr;
for (pr = books.begin(); pr != books.end(); pr++) {
ShowReview(*pr);
}
vector <Review> oldlist(books);
if (num > 3) {
books.erase(books.begin() + 1, books.begin() + 3);
cout << "After erasure:\n";
for (pr = books.begin(); pr != books.end(); pr++) {
ShowReview(*pr);
}
books.insert(books.begin(), oldlist.begin() + 1,
oldlist.begin() + 2);
cout << "After insertion:\n";
for (pr = books.begin(); pr != books.end(); pr++) {
ShowReview(*pr);
}
}
books.swap(oldlist);
cout << "Swapping oldlist with books:\n";
for (pr = books.begin(); pr != books.end(); pr++) {
ShowReview(*pr);
}
} else {
cout << "Nothing entered, nothing gained.\n";
}
return 0;
}
bool FillReview(Review & rr)
{
std::cout << "Enter book title (quit to quit): ";
std::getline(std::cin, rr.title);
if (rr.title == "quit") {
return false;
}
std::cout << "Enter book rating: ";
std::cin >> rr.rating;
if (!std::cin) {
return false;
}
std::cin.get();
return true;
}
void ShowReview(const Review & rr)
{
std::cout << rr.rating << "\t" << rr.title << std::endl;
}