Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <tuple>
- #include <utility>
- #include <map>
- #include <set>
- using namespace std;
- class Cities
- {
- public:
- tuple<bool, string> FindCountry(const string& city) const
- {
- // Метод константный. У класса есть поле со словарем, а [] для словаря теоретически могут
- // менять сам словарь добавляя в него элементы, если их там нет
- // Поэтому нужно обратиться к ключу КОНСТАНТНОГО словаря
- if (city_to_country.count(city) == 1)
- {
- //return { true, city_to_country[city] };
- // at - если ключ не нашелся, то он его не добавит, а выбросит исключение
- return { true, city_to_country.at(city) };
- }
- else if (ambigious_cities.count(city) == 1)
- {
- return { false, "Ambigious" };
- }
- else
- {
- return { false, "Not exist" };
- }
- }
- private:
- map<string, string> city_to_country; // по названию города хранит название страны
- set<string> ambigious_cities;
- };
- int main()
- {
- Cities cities;
- /*bool success;
- string message;*/
- /*auto t = cities.FindCountry("Volgograd");
- cout << get<1>(t) << endl;*/
- /*tie(success,message) = cities.FindCountry("Volgograd");
- cout << success << " " << message << endl;*/
- auto [success, message] = cities.FindCountry("Volgograd");
- cout << success << " " << message << endl;
- map<string, pair<double, double>> citiess;
- for (const auto& item : citiess)
- {
- cout << item.second.first << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement