实现字符串(城市)图表的最简单方法是什么?

时间:2016-06-22 14:05:02

标签: c++ graph

我已经实现了一个只使用整数类型的邻接矩阵图。(考虑到我在这里提到的所有内容的C ++)

我实现了另一个图形,它将使用我的旧实现接收顶点中的城市和边缘中的距离。我想知道这是不是一个好主意,或者我应该使用不同的实现来解决链接列表这个问题。这里的想法是从txt文件中读取所有城市及其相互之间的距离,将其添加到图表中,然后向用户显示菜单,以便他/她可以查询从城市A到B的距离并获取列表他们到达目标之前需要旅行的所有城市。

我计划阅读城市,给它一个数字代码,然后将其添加到图表中,而不是添加字符串" city" (需要将图形从整数转换为字符串)

您如何看待,有什么想法/建议吗?

1 个答案:

答案 0 :(得分:0)

您可以使用任一地图。像这样

  

map<pair<string,string>,int> city ;

(请参阅实现2) 或者,您可以使用vector跟踪城市名称及其索引(实现1)

实施1

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;

class Graph
{
    int V; // start vertice 
    map<pair<string,string>,int> city ;
    vector<string> city1;
    vector<string> city2;
    public:
    Graph(int start_V)
    {
        V = start_V;
    }
    void addEdge(string start,string end,int wt);
    void display();
};
void Graph::addEdge(string start,string end,int wt)
{   
   city[make_pair(start,end)] = wt;
}
void Graph::display()
{   
    int flag=0;
    for(auto it:city)
    {
        city1.push_back(it.first.first);
        city2.push_back(it.first.second);
    }

    sort(city1.begin(), city1.end()); 
    auto last = unique(city1.begin(), city1.end());
    city1.erase(last, city1.end());

    sort(city2.begin(), city2.end()); 
    auto last2 = unique(city2.begin(), city2.end());
    city2.erase(last2, city2.end());

    for(auto col:city2)
    {
        cout<<"\t"<<col;
    }
    cout<<"\n";

    for(auto row:city1)
    {
    int flag =0;   //for printing row for once 
    for(auto col:city2)
    {
        if (!flag)
            cout<<row;
        cout<<"\t"<<city[make_pair(row,col)];
        flag = 1;
    }

    cout<<"\n";
    }

}
int main()
{
    Graph g(2);

    g.addEdge("A","B",1);
    g.addEdge("C","A",5);
    g.addEdge("D","E",7);
    g.addEdge("E","A",5);
    g.addEdge("D","B",7);
    g.addEdge("D","L",7);
    g.addEdge("W","L",7);
    g.display();
    return 0;
}

实施2

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<map> 
using namespace std;

class Graph {
    int V; // start vertice 
    map<pair<string,string>,int> city ;
    vector<string> city1;
    vector<string> city2;
    public:
    Graph(int start_V)
    {
        V = start_V;
    }
    void addEdge(string start,string end,int wt);
    void display(); }; void Graph::addEdge(string start,string end,int wt) {       city[make_pair(start,end)] = wt; } void Graph::display() {

    int flag=0;
    for(auto it:city)
    {
        city1.push_back(it.first.first);
        city2.push_back(it.first.second);
    }

    sort(city1.begin(), city1.end()); 
    auto last = unique(city1.begin(), city1.end());
    city1.erase(last, city1.end());

    sort(city2.begin(), city2.end()); 
    auto last2 = unique(city2.begin(), city2.end());
    city2.erase(last2, city2.end());

    for(auto col:city2)
    {
        cout<<"\t"<<col;
    }
    cout<<"\n";

    for(auto row:city1)
    {
    int flag =0;   //for printing row for once 
    for(auto col:city2)
    {
        if (!flag)
            cout<<row;
        cout<<"\t"<<city[make_pair(row,col)];
        flag = 1;
    }

    cout<<"\n";
    }
     } int main() {
    Graph g(2);

    g.addEdge("A","B",1);
    g.addEdge("C","A",5);
    g.addEdge("D","E",7);
    g.addEdge("E","A",5);
    g.addEdge("D","B",7);
    g.addEdge("D","L",7);
    g.addEdge("W","L",7);
    g.display();
    return 0; }