Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <set>
- #include <string>
- #include <ctime>
- #include <random>
- using namespace std;
- class Book
- {
- private:
- string first_name_a;
- string second_name_a;
- string titel;
- string publisher;
- int count;
- int year;
- int paper;
- int electronic;
- int audio;
- public:
- Book(string const f = " ", string const s = " ", string const t = " ",
- string const p = " ", int c = 0, int y = 0, int ta = 0, int tp = 0, int te = 0)
- {
- first_name_a = f;
- second_name_a = s;
- titel = t;
- publisher = p;
- count = c;
- year = y;
- paper = ta;
- electronic = tp;
- audio = te;
- }
- bool operator < (const Book& B) const
- {
- return ((count < B.count) + ((count != B.count) * (year < B.year)) +
- ((count != B.count) * (year != B.year) * (titel < B.titel)));
- }
- bool operator ==(const Book& B) const
- {
- return !(*this < B) && !(B < *this);
- }
- bool operator >(int P) const
- {
- return (count > P);
- }
- int count_() { return count; }
- int year_() { return year; }
- string titel_() { return titel; }
- void print() const
- {
- cout << "\nName authot: " << first_name_a << "\nSurname author: " << second_name_a <<
- "\nTitel of book: " << titel << "\nCount books: " << count << "\nPublisher: " << publisher <<
- "\nYear of publication: " << year << "\nNumber of paper books: " << paper << "\nNumber of electronic books:" <<
- electronic << "\nNumber of audio books:" << audio << '\n';
- }
- ~Book() = default;
- friend void fill(Book& V);
- };
- void fill(Book& V)
- {
- srand(time(NULL));
- string names[5] = { "Alice", "Bob", "Carla", "David", "Eva" };
- string surnames[5] = { "Smith", "Johnson", "Brown", "Davis", "Wilson" };
- string publishers[5] = { "Penguin Random House", "HarperCollins", "Simon & Schuster", "Macmillan Publishers", "Hachette Livre" };
- string books[5] = { "To Kill a Mockingbird", "1984", "The Great Gatsby", "Pride and Prejudice", "The Catcher in the Rye" };
- int i1 = rand() % 5, i2 = rand() % 5, i3 = rand() % 5, i4 = rand() % 5,
- i5 = rand() % 5000, i6 = rand() % 2023, i7 = rand() % 1000, i8 = rand() % 1000, i9 = rand() % 1000;
- V.first_name_a = names[i1];
- V.second_name_a = surnames[i2];
- V.titel = books[i3];
- V.publisher = publishers[i4];
- V.count = i5;
- V.year = i6;
- V.paper = i7;
- V.electronic = i8;
- V.audio = i9;
- }
- bool book_count(Book B, int P)
- {
- return (B > P);
- }
- template<class K, class V>
- K find_key(const map<K, V>& m, V const& val)
- {
- auto it = m.begin();
- for (it; it != m.end(); ++it)
- if (it->second == val)
- break;
- return it->first;
- }
- template <class K, class V>
- V find_val(const map<K, V>& m, K const& key)
- {
- auto it = m.begin();
- for (it; it != m.end(); ++it)
- if (it->first == key)
- break;
- return it->second;
- }
- template<class K, class V, class T>
- map<K, V> filter(const map<K, V>& m, bool(*comp)(V,T), T P)
- {
- if (comp == nullptr) cout << "nullptr";
- map<K, V> ans;
- for (auto it = m.begin(); it != m.end(); ++it)
- if (comp(it->second,P))
- ans.emplace(*it);
- return ans;
- }
- template<class K, class V>
- set<V> all_values(const map<K, V>& m)
- {
- set<V> s;
- for (auto it = m.begin(); it != m.end(); ++it)
- s.emplace(it->second);
- return s;
- }
- template<class K, class V>
- void map_print(const map<K,V>& m)
- {
- for (auto it = m.begin(); it != m.end(); ++it)
- cout << "Key: " << it->first << ", " << "Value: " << it->second << '\n';
- }
- ///////////////////////////////////////////////////////////////////////////
- template<class K, class V>
- K find_key(const multimap<K, V>& m, V const& val)
- {
- auto it = m.begin();
- for (it; it != m.end(); ++it)
- if (it->second == val)
- break;
- return it->first;
- }
- template <class K, class V>
- V find_val(const multimap<K, V>& m, K const& key)
- {
- auto it = m.begin();
- for (it; it != m.end(); ++it)
- if (it->first == key)
- break;
- return it->second;
- }
- template<class K, class V, class T>
- multimap<K, V> filter(const multimap<K, V>& m, bool(*comp)(V, T), T P)
- {
- if (comp == nullptr) cout << "nullptr";
- multimap<K, V> ans;
- for (auto it = m.begin(); it != m.end(); ++it)
- if (comp(it->second, P))
- ans.emplace(*it);
- return ans;
- }
- template<class K, class V>
- set<V> all_values(const multimap<K, V>& m)
- {
- set<V> s;
- for (auto it = m.begin(); it != m.end(); ++it)
- s.emplace(it->second);
- return s;
- }
- template<class K, class V>
- void multimap_print(const multimap<K, V>& m)
- {
- for (auto it = m.begin(); it != m.end(); ++it)
- cout << "Key: " << it->first << ", " << "Value: " << it->second << '\n';
- }
- int main()
- {
- map<string, int> marks;
- marks["Petrov"] = 5;
- marks["Ivanov"] = 4;
- marks["Sidorov"] = 5;
- marks["Nikolaev"] = 3;
- marks["Abramov"] = 4;
- map_print(marks);
- auto a = find_key(marks, 5);
- cout << '\n' << a << '\n';
- set<int> s = all_values(marks);
- for (auto it = s.begin(); it != s.end(); ++it)
- cout << *it << " ";
- cout << '\n';
- map<int, Book> books;
- for (int i = 0; i < 5; ++i)
- {
- Book A;
- fill(A);
- books[i] = A;
- }
- bool(*ptr)(Book, int) = &book_count;
- map<int, Book> ans = filter(books, ptr, 2000);
- cout << books.size() << " " << ans.size();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement