Advertisement
RobertDeMilo

YB4.4 Итераторы множеств и словарей2

Nov 11th, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     vector <string> langs = { "Python", "C++", "C", "Java", "C#" };
  10.  
  11.     auto it = end(langs);
  12.  
  13.     while (it != begin(langs))
  14.     {
  15.         --it;
  16.         cout << *it << " ";
  17.     }
  18.  
  19.     //##################################################################################
  20.     // Можно ли внести декремент в условие цикла?
  21.     /*while (it-- != begin(langs))
  22.     {
  23.         cout << *it << " ";
  24.     }*/
  25.  
  26.     // Как нельзя
  27.     /**end(c);
  28.     auto it = end(c); ++it;
  29.     auto it = begin(c); --it;*/
  30.  
  31.     //##################################################################################
  32.     // Отличие итераторов от ссылок
  33.     // Итераторы могут указывать в никуда - на end
  34.     // Итераторы можно перемещать на другие элементы:
  35.    
  36.     vector<int> numbers = { 1,3,5 };
  37.     auto it = numbers.begin();
  38.     ++it; // it указывает на 3
  39.     int& ref = numbers.front();
  40.     ++ref; // теперь numbers[0] == 2
  41.  
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement