DimovIvan

C++Advanced-Map and Set - Lab-Cities by Continent and Country

Jul 11th, 2021 (edited)
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.39 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<unordered_set>
  4. #include<unordered_map>
  5. #include<string>
  6. #include<sstream>
  7. #include<array>
  8.  
  9. const int LINE_ATTRIBUTES_COUNT = 3;
  10. const int CONTINENT_IDX = 0;
  11. const int COUNTRY_IDX = 1;
  12. const int CITY_IDX = 2;
  13.  
  14.  
  15. std::vector<std::array<std::string, LINE_ATTRIBUTES_COUNT>> readAttributes(int& numbLines) {
  16.     std::vector<std::array<std::string, LINE_ATTRIBUTES_COUNT>> attributes(numbLines);
  17.  
  18.     for (size_t i = 0; i < numbLines; i++)
  19.     {
  20.         for (size_t j = 0; j < LINE_ATTRIBUTES_COUNT; j++)
  21.         {
  22.             std::cin >> attributes[i][j];
  23.  
  24.         }
  25.     }
  26.  
  27.     return attributes;
  28. }
  29.  
  30. std::vector<std::string> getContinents(const std::vector<std::array<std::string, LINE_ATTRIBUTES_COUNT>>& attributes,
  31.                                        const int numbLines) {
  32.     std::vector<std::string> continents{};
  33.  
  34.     std::unordered_set<std::string> uniqueContinents{};
  35.     for (size_t i = 0; i < numbLines; i++)
  36.     {
  37.         for (size_t j = 0; j < LINE_ATTRIBUTES_COUNT; j++)
  38.         {
  39.             if (j == CONTINENT_IDX)
  40.             {
  41.                 auto it = uniqueContinents.find(attributes[i][CONTINENT_IDX]);
  42.                 if (it == uniqueContinents.end())
  43.                 {
  44.                     continents.push_back(attributes[i][CONTINENT_IDX]);
  45.                     uniqueContinents.insert(attributes[i][CONTINENT_IDX]);
  46.                 }
  47.                 break;
  48.             }
  49.         }
  50.     }
  51.  
  52.     return continents;
  53. }
  54.  
  55. std::unordered_map<std::string, std::vector<std::string>> getCountries(const std::vector<std::array<std::string, LINE_ATTRIBUTES_COUNT>>& attributes,
  56.                                                    const int numbLines,
  57.                                                    const std::vector<std::string>& continents) {
  58.     std::unordered_map<std::string, std::vector<std::string>> countries{};
  59.  
  60.     std::unordered_set<std::string> uniqueCountries{};
  61.     for (const auto& continent : continents)
  62.     {
  63.         std::vector<std::string> currContCountries{};
  64.         for (size_t i = 0; i < numbLines; i++)
  65.         {
  66.             for (size_t j = 0; j < LINE_ATTRIBUTES_COUNT; j++)
  67.             {
  68.                 if (attributes[i][CONTINENT_IDX] == continent)
  69.                 {
  70.                     auto it = uniqueCountries.find(attributes[i][COUNTRY_IDX]);
  71.                     if (it == uniqueCountries.end())
  72.                     {
  73.                         currContCountries.push_back(attributes[i][COUNTRY_IDX]);
  74.                         uniqueCountries.insert(attributes[i][COUNTRY_IDX]);
  75.                         //std::cout << attributes[i][COUNTRY_IDX] << std::endl;
  76.                     }
  77.                     break;
  78.                 }
  79.             }
  80.         }
  81.         if (!currContCountries.empty())
  82.         {
  83.             countries[continent] = currContCountries;
  84.         }
  85.     }
  86.  
  87.     return countries;
  88. }
  89.  
  90. std::unordered_map<std::string, std::vector<std::string>> getCities(const std::vector<std::array<std::string, LINE_ATTRIBUTES_COUNT>>& attributes,
  91.                                                 const int numbLines,
  92.                                                 const std::unordered_map<std::string, std::vector<std::string>>& countries) {
  93.     std::unordered_map<std::string, std::vector<std::string>> cities{};
  94.  
  95.     for (auto it = countries.begin(); it != countries.end(); ++it)
  96.     {
  97.         for (auto& country : it->second)
  98.         {
  99.             std::vector<std::string> currCountryCities{};
  100.             for (size_t i = 0; i < numbLines; i++)
  101.             {
  102.                 for (size_t j = 0; j < LINE_ATTRIBUTES_COUNT; j++)
  103.                 {
  104.                     if (attributes[i][COUNTRY_IDX] == country) {
  105.                         currCountryCities.push_back(attributes[i][CITY_IDX]);
  106.                     }
  107.                     break;
  108.                 }
  109.             }      
  110.             cities[country] = currCountryCities;
  111.         }
  112.     }
  113.  
  114.     return cities;
  115. }
  116.  
  117. void printSolution(const std::vector<std::string>& continents,
  118.                    const std::unordered_map<std::string, std::vector<std::string>>& countries,
  119.                    const std::unordered_map<std::string, std::vector<std::string>>& cities) {
  120.     const size_t continentsCount = continents.size();
  121.     for (size_t i = 0; i < continentsCount; i++)
  122.     {
  123.         std::cout << continents[i] << ":" << std::endl;
  124.         const auto itCountries = countries.find(continents[i]);
  125.         if (itCountries != countries.end())
  126.         {
  127.             for (const auto& country : itCountries->second)
  128.             {
  129.                 std::cout << "  " << country << " -> ";
  130.                 const auto itCities = cities.find(country);
  131.                 if (itCities != cities.end())
  132.                 {
  133.                     std::string result;
  134.                     for (const auto& city : itCities->second)
  135.                     {
  136.                         result.append(city).append(", ");
  137.                     }
  138.                     result.pop_back();
  139.                     result.pop_back();
  140.                     std::cout << result << std::endl;
  141.                 }
  142.             }
  143.         }
  144.        
  145.     }
  146. }
  147.  
  148. int main() {
  149.     int numbLines = 0;
  150.     std::cin >> numbLines;
  151.     std::cin.ignore();
  152.     const std::vector<std::array<std::string, LINE_ATTRIBUTES_COUNT>> attributes = readAttributes(numbLines);
  153.     const std::vector<std::string> continents = getContinents(attributes, numbLines);
  154.     const std::unordered_map<std::string, std::vector<std::string>> countries = getCountries(attributes, numbLines, continents);
  155.     const std::unordered_map<std::string, std::vector<std::string>> cities = getCities(attributes, numbLines, countries);
  156.     printSolution(continents, countries, cities);
  157.     /*for (const auto& continent : continents)
  158.     {
  159.         std::cout << continent << std::endl;
  160.     }
  161.     std::cout << std::endl;*/
  162.  
  163.     /*for (auto it = cities.begin(); it != cities.end(); ++it)
  164.     {
  165.         for (auto& country : it->second)
  166.         {
  167.             std::cout << country << ' ';
  168.         }
  169.         std::cout << std::endl;
  170.     }*/
  171.  
  172.     /*for (auto it = cities.begin(); it != cities.end(); ++it)
  173.     {
  174.         std::queue<std::string> ccc = it->second;
  175.         while (!ccc.empty())
  176.         {
  177.             const auto& city = ccc.front();
  178.             std::cout << city << ' ';
  179.             ccc.pop();
  180.         }
  181.         std::cout << std::endl;
  182.     }*/
  183.  
  184.     //std::cout << continents.size() << std::endl;
  185.     return 0;
  186. }
Add Comment
Please, Sign In to add comment