我在打印多维 map 时遇到问题。我通过
初始化它map<string, map<string, vector<double>>> mapData;
void Graph::addToGraph(string start, string next, double cos, double mile)
{
//see if the map contains the starting city string, add if not found
if (mapData.find(start) == mapData.end())
{
mapData[start][start].push_back(invalid);
mapData[start][start].push_back(invalid);
}
//see if the map contains the next city string, add if not found
if (mapData.find(next) == mapData.end())
{
mapData[next][next].push_back(invalid);
mapData[next][next].push_back(invalid);
}
mapData[start][next].push_back(cos);
mapData[start][next].push_back(mile);
double check = mapData[start][next][0];//test case
}
据我所知,在我看来 map 已正确创建。我使用了三维空间,所以我可以存储里程和价格。这将用于最短路径算法。我想打印每个城市,以及所有到该城市的转机航类及其里程和价格示例,SFA SLC $700 59miles。这甚至可能吗,如果不可能,我将如何改变它?
为了更好地理解我为什么要使用 map ,该项目的要求是我从一个文件中读入所有信息,从该文件创建一个图表,然后让用户输入缩写他们想要开始的城市。 Maps 允许我用一个字符串在数组中指定一个点,这对这个很有用。但我不确定它是否适用于该算法。
最佳答案
{
for (auto kv : mapData)
{
for (auto kvv : kv.second)
{
if (kvv.second != invalid)
{
cout << kv.first << "<-->" << kvv.first;
cout << " costs $" << kvv.second << endl;
}
}
cout << distance(mapData.begin(), mapData.find("SLC"));
}
}
这是我用来打印 map 的功能。感谢 Lucas 为我指明了正确的方向
关于c++ - 在 C++ 中打印多维 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34213819/