Advertisement
RobertDeMilo

YB1.5 Кортежи и пары

Oct 27th, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <tuple>
  4. #include <utility>
  5. #include <map>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     //tuple <int, string, bool> t(7, "C++", true);
  12.  
  13.     // auto t= tie(7, "C++", true);
  14.     // tie создает кортеж из ссылок
  15.     // попытались сохранить ссылку на какую-то 7. Но 7 не настоящий объект нельзя сохранить на нее ссылку
  16.     // tie используется для того чтобы взять какие-то значения, которые уже где-то хранятся связать их поиспользовать (например, сравнить) и забыть
  17.     // make_tuple создает кортеж из самих значений
  18.  
  19.     /*auto t = make_tuple(7, "C++", true);
  20.     cout << get<1>(t) << endl;*/
  21.  
  22.     // В стандарте С++17 разрешается не указывать параметры в угловых скобках (шаблонные параметры)
  23.     /*tuple t(7, "C++", true);
  24.     cout << get<1>(t) << endl;*/
  25.  
  26.     /*vector v = { 1,2,3 };
  27.     cout << v[0] << endl;*/
  28.     //pair <int, string> p(7, "C++");
  29.     //pair p(7, "C++"); // стандарт С++17
  30.  
  31.     /*auto p = make_pair(7, "C++");
  32.     cout << p.first << " " << p.second << endl;*/
  33.  
  34.     map<int, string> digits = { {1,"one"} };
  35.  
  36.     /*for (const pair<int,string>& item : digits)
  37.     {
  38.         cout << item.first << " " << item.second << endl;
  39.     }*/
  40.  
  41.     for (const auto& [key, value] : digits)
  42.     {
  43.         cout << key << " " << value << endl;
  44.     }
  45.  
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement