![](/img/trans.png)
[英]What is the easiest way to initialize a 2d array from a vector of strings?
[英]What is the easiest way to implement a graph of strings(cities)?
我已經實現了一個只使用整數類型的鄰接矩陣圖。(考慮到我在這里提到的所有內容的C ++)
我正在實現另一個圖形,它將使用我的舊實現接收頂點中的城市和邊緣中的距離。 我想知道這是不是一個好主意,或者我應該使用不同的實現來解決鏈接列表這個問題。 這里的想法是從txt文件中讀取所有城市及其相互之間的距離,將其添加到圖表中,然后向用戶顯示菜單,以便他/她可以查詢從城市A到B的距離並獲取列表在他們到達目標之前需要旅行的所有城市。
我打算閱讀這個城市,給它一個數字代碼,並將其添加到圖表中,而不是添加字符串“city”(需要將圖形從整數轉換為字符串)
您如何看待,有什么想法/建議嗎?
您可以使用任一地圖。 像這樣
map<pair<string,string>,int> city ;
(參見實現2)或者您可以使用向量來跟蹤城市名稱及其索引(實現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; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.