Advertisement
smatskevich

Seminar2

Feb 17th, 2022
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4. #include <unordered_map>
  5.  
  6. void try_access_4(const std::unordered_map<int, char>& m) {
  7.   // Оператор [] недоступен.
  8.   std::cout << "Value at 4 = " << m.at(4) << std::endl;
  9. }
  10.  
  11. int main1() {
  12.   std::unordered_map<int, char> m;
  13.   m.insert({5, 'f'});
  14.   std::cout << "Count 4: " << m.count(4) << std::endl;
  15.   std::cout << "Contains 5: " << (m.contains(5) ? "YES" : "NO") << std::endl;
  16.   const auto i = m.find(5);
  17.   std::cout << (i == m.end() ? "end" : "exists") << std::endl;
  18.   std::cout << i->first << " " << i->second << std::endl;
  19.  
  20.   m.erase(5);
  21.   std::cout << "Contains 5 after erase: " << (m.contains(5) ? "YES" : "NO") << std::endl;
  22.  
  23.   // Работа со значением.
  24.   m.insert({5, 'g'});
  25.   std::cout << "Value at 5 = " << m.at(5) << std::endl;
  26.   m.at(5) = 'h';
  27.   std::cout << "Value at 5 after = " << m.at(5) << std::endl;
  28.   // at бросит исключение
  29.   // m.at(4) = 'h';
  30.  
  31.   std::cout << "Value at 5 = " << m[5] << std::endl;
  32.   try_access_4(m);
  33.   std::cout << "Value at 4 = " << m[4] << std::endl;
  34.   std::cout << "Is zero at 4? " << (m[4] == 0 ? "Zero" : "Non zero") << std::endl;
  35.   return 0;
  36. }
  37.  
  38. int main2() {
  39.   int n = 0;
  40.   std::cin >> n;
  41.   std::unordered_map<std::string, int> s;
  42.   for (int i = 0; i < n; ++i) {
  43.     std::string str;
  44.     std::cin >> str;
  45.     // Вариант 1
  46.     auto it = s.find(str);
  47.     if (it != s.end()) {
  48.       int value = ++it->second;
  49.       if (value == 2) std::cout << str << std::endl;
  50.     } else {
  51.       s.insert({str, 1});
  52.     }
  53.     // Вариант 2
  54.     // if (++s[str] == 2) std::cout << str << std::endl;
  55.   }
  56.   return 0;
  57. }
  58.  
  59. int main3() {
  60.   std::map<std::string, int> m;
  61.   m.insert({"aba", 4});
  62.   m.insert({"ac", 5});
  63.   m.insert({"aaaaa", 6});
  64.  
  65.   auto& [a, b] = *(m.find("ac"));
  66.   std::cout << a << " -> " << b << std::endl;
  67.  
  68.   for (auto& [key, value] : m) {
  69.     std::cout << key << " -> " << value << std::endl;
  70.   }
  71.   return 0;
  72. }
  73.  
  74. // Лесенка
  75. int main() {
  76.   int n = 0;
  77.   std::cin >> n;
  78.   if (n <= 1) {
  79.     std::cout << 1 << std::endl;
  80.     return 0;
  81.   }
  82.   int s0 = 1;
  83.   int s1 = 1;
  84.   int s2 = 2;
  85.  
  86.   for (int i = 0; i < n - 2; ++i) {
  87.     int result = s0 + s1 + s2;
  88.     s0 = s1;
  89.     s1 = s2;
  90.     s2 = result;
  91.     // ??? std::tie(s0, s1, s2) = std::tie(s1, s2, result);
  92.   }
  93.   std::cout << s2 << std::endl;
  94.   return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement