-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest1.cpp
More file actions
84 lines (65 loc) · 1.84 KB
/
test1.cpp
File metadata and controls
84 lines (65 loc) · 1.84 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
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
class Book {
private:
std::string mAuthor;
std::string mTitle;
uint32_t mID;
public:
Book(std::string auth, std::string title, uint32_t id) {
mAuthor = auth;
mTitle = title;
mID = id;
}
std::string getAuthor() const { return mAuthor; }
std::string getTitle() const { return mTitle; }
uint32_t getID() const { return mID; }
};
class Library {
private:
std::vector<Book> mBooks;
public:
void append_book(Book book) { mBooks.push_back(book); }
void print_list() {
std::cout << "Current list of books: " << std::endl;
for (auto book : mBooks)
std::cout << "Author: " << book.getAuthor()
<< " Title: " << book.getTitle() << " ID: " << book.getID()
<< std::endl;
}
void remove_book(uint32_t id) {
auto match_id = [id](const Book &book) { return book.getID() == id; };
auto it = std::find_if(mBooks.begin(), mBooks.end(), match_id);
if (it != mBooks.end()) {
mBooks.erase(it); // Remove the book found at iterator `it`
std::cout << "Book with ID " << id << " and title " << it->getTitle()
<< " removed." << std::endl;
} else {
std::cout << "No book with ID " << id << " found.";
}
}
void remove_book_swap(const uint32_t id) {
for (auto &book : mBooks) {
if (book.getID() == id) {
std::swap(book, mBooks.back());
mBooks.pop_back();
break;
}
}
}
};
int main() {
Library lib;
Book book1("Tovia Singer", "Let's get biblical", 124);
Book book2("Jon Erikson", "Art of exploitation", 11124);
Book book3("Tom Clancy", "Military book", 1234567);
lib.append_book(book1);
lib.append_book(book2);
lib.append_book(book3);
lib.print_list();
lib.remove_book_swap(124);
lib.print_list();
return 0;
}