Advertisement
chevengur

Вводный курс: основы C++ | Урок 4: Меняем размер вектора 2/2

Aug 28th, 2023 (edited)
219
1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 1 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>                                   //для count
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.     int q;
  10.     cin >> q;
  11.  
  12.     vector<bool>queue;
  13.  
  14.     for (int i = 0; i < q; ++i) {
  15.         string operation_code;
  16.         cin >> operation_code;
  17.  
  18.         if (operation_code == "WORRY"s) {
  19.             int index;
  20.             cin >> index;
  21.             queue[index] = true;
  22.         }
  23.  
  24.         else if (operation_code == "HAPPY"s) {
  25.             int index;
  26.             cin >> index;
  27.             queue[index] = false;
  28.         }
  29.  
  30.         else if (operation_code == "COME"s) {
  31.             int count;
  32.             cin >> count;
  33.             queue.resize(queue.size()+count, false);
  34.         }
  35.  
  36.         else if (operation_code == "LAST_WORRY"s) {
  37.             if (!queue.empty()) {
  38.                 if (queue.back() == true) {
  39.                     cout << "worry" << endl;
  40.                 }
  41.                 if (queue.back() == false) {
  42.                     cout << "happy" << endl;
  43.                 }
  44.             }
  45.         }
  46.  
  47.         else if (operation_code == "WORRY_COUNT") {
  48.             int count_ = count(queue.begin(), queue.end(), true); //функция count для подсчета необходимых значений в векторе
  49.                                                                   //https://en.cppreference.com/w/cpp/algorithm/count
  50.             cout << count_ << endl;                
  51.         }
  52.     }
  53. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement