Advertisement
RobertDeMilo

WB2.7 Векторы 2

Sep 3rd, 2023
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. ****************************************************************
  7. void PrintVector(const vector<int>& v)
  8. {
  9.     for (auto s : v)
  10.     {
  11.         cout << s << endl;
  12.     }
  13. }
  14. ****************************************************************
  15. void PrintVector(const vector<bool>& v)
  16. {
  17.     int i = 0;
  18.     for (auto s : v)
  19.     {
  20.         cout << i << ": " << s << endl;
  21.         ++i;
  22.     }
  23. }
  24. ****************************************************************
  25. int main()
  26. {
  27.  
  28.     vector<int> days_in_months = { 31, 28,31,30,31 };
  29.     if (true) // if year is leap
  30.     {
  31.         days_in_months[1]++;
  32.     }
  33.    
  34.     PrintVector(days_in_months);
  35. ****************************************************************
  36.     vector <bool> is_holiday(28, false); //по умолчанию заполним вектор значением false
  37.     is_holiday[22] = true;
  38.     PrintVector(is_holiday);
  39. ****************************************************************
  40.     //is_holiday.resize(31); // оставил нетронутым исходную часть вектора
  41.     is_holiday.assign(31,false);
  42.     is_holiday[7] = true;
  43.     PrintVector(is_holiday);
  44.     is_holiday.clear();
  45.  
  46.     return 0;
  47. }
  48.  
  49. assign заменяет содержимое контейнера.
  50.  
  51. Основное различие между методами `resize` и `assign` заключается в том, что `resize` меняет размер вектора, сохраняя первоначальные значения или инициализируя новые значения, а `assign` изменяет все элементы вектора, заменяя их новыми значениями.
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement