Завершить вызов после создания экземпляра 'std::out_of_range' What(): _Map_base::atC++

Программы на C++. Форум разработчиков
Ответить
Гость
 Завершить вызов после создания экземпляра 'std::out_of_range' What(): _Map_base::at

Сообщение Гость »

Я использую неупорядоченную карту, чтобы создать список смежности для хранения информации о рейсах.
Когда я запускаю свой код, я получаю сообщение об ошибке:

Код: Выделить всё

terminate called after throwing an instance of 'std::out_of_range'
what():  _Map_base::at
Я новичок в C++ и не понимаю, что это значит. Я знаю, что это связано с использованием .at() в функции getDestinations(), но как это исправить?
Код ниже:
graph.h

Код: Выделить всё

#include 
#include 
#include 
using namespace std;

//creates a struct for cities with name and isInternational variables
struct City {
string name; //name stored in variable of type string
bool isInternational; //boolean variable for if city is international or not
};

//creates graph class with function prototypes
class Graph {
public:
//function prototypes for adding cities, adding flights, and determining flight possibility
void addCity(const string& name, bool isInternational);
void addFlight(const string& from, const string& to);
bool canReach(const string& from, const string& to);

//function prototypes for retrieving cities & destinations
vector getCities() const;
vector getDestinations(const string& from) const;
private:
//uses unordered map to create lists for both cities & flights
unordered_map cities;
unordered_map flights;
};

Код: Выделить всё

//function definition for getDestinations function
vector Graph::getDestinations(const string& from) const {
vector result;
for (const auto& destination : flights.at(from)) {
result.push_back(cities.at(destination));
}
return result;
}
Я пробовал использовать разные методы для доступа к атрибутам from и назначения, например, contains() или find() code>, но они не работают.

Источник: https://stackoverflow.com/questions/760 ... e-what-map
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»