Advertisement
Guest User

Untitled

a guest
Apr 4th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6. #include <unordered_map>
  7. #include <map>
  8. #include <set>
  9. using namespace std;
  10.  
  11.  
  12. string shkilaGenerator(int n) {
  13.     string tmp = "qwertyuiopasdfghjkl ";
  14.     string result;
  15.     for (int i = 0; i < n; i++) {
  16.         int index = rand() % tmp.size();
  17.         result += tmp[index];
  18.     }
  19.     return result;
  20. }
  21.  
  22. template <typename T>
  23. void printM(const T& a) {
  24.     cout << endl;
  25.     for_each(a.cbegin(), a.cend(), [](auto p) {                   // []-захват области видимости,
  26.         cout << setw(5) << p.first << " : " << p.second << endl;
  27.     });
  28.     cout << endl << endl;
  29. }
  30.  
  31. template <typename T>
  32. void printV(const T& a) {
  33.     cout << endl;
  34.     for_each(a.cbegin(), a.cend(), [](auto p) {                  
  35.         cout << p << endl;
  36.     });
  37.     cout << endl << endl;
  38. }
  39.  
  40. int main() {
  41.     unordered_multimap<int, string> shkila;
  42.     srand(time(NULL));
  43.  
  44.     int k;
  45.     cout << "Enter quantity of elements: "; cin >> k;
  46.     for (int i = 0; i < k; i++) {
  47.         shkila.insert({ (rand() % 20) - 10,shkilaGenerator(rand() % 15) });
  48.     }
  49.     printM(shkila);
  50.  
  51.     shkila.insert({ 4,shkilaGenerator(rand() % 15) });
  52.     shkila.insert({ 4,shkilaGenerator(rand() % 15) });
  53.     printM(shkila);
  54.  
  55.     // Находим ключ 2
  56.     auto keyTwo = shkila.find(2);
  57.     if (keyTwo != shkila.end()) {
  58.         // Если нашли, то удаляем эту пару
  59.         shkila.erase(shkila.find(2));
  60.     }
  61.     printM(shkila);
  62.  
  63.     map<int, string> mapShkila(shkila.begin(), shkila.end());
  64.     printM(mapShkila);
  65.  
  66.     int countResult = count_if(mapShkila.begin(), mapShkila.end(), [](auto p) {
  67.         return p.first > 0;
  68.     });
  69.     cout << endl << countResult << endl;
  70.  
  71.     vector<int> keys;
  72.     transform(mapShkila.begin(), mapShkila.end(), back_inserter(keys), [](auto p) {
  73.         return p.first;
  74.     });
  75.     printV(keys);
  76.  
  77.     auto keyOne = find(keys.begin(), keys.end(), 1);
  78.     if (keyOne == keys.end()) {
  79.         // Если не нашли
  80.         cout << endl << "Not found" << endl;
  81.     } else {
  82.         cout << endl << "Index: " << keyOne - keys.begin() << endl;
  83.     }
  84.  
  85.     replace_if(keys.begin(), keys.end(), [](auto value) {
  86.         return value < 0;
  87.     }, 0);
  88.     printV(keys);
  89.  
  90.     sort(keys.begin(), keys.end(), [](auto value1, auto value2) {
  91.         return value1 > value2;
  92.     });
  93.     printV(keys);
  94.  
  95.     set<int> setShkila(keys.begin(), keys.end());
  96.     printV(setShkila);
  97.  
  98.  
  99.     system("pause");
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement