2014-01-10 40 views
-2

可以說,我有以下文本內的文本文件:如何閱讀txt文件到C++(Dijkstra算法輸入)

----Text file---------------------- 
A B 4 C 2 
B D 2 C 3 E 3 
C B 1 D 4 E 5 
D 
E D 1 
----------------------------------- 

這個文件的含義是:距離AB4AC2BD2BC3 ...等

可我知道我應該怎麼看這個文件到C++親克變量?

我想將它做成不同的一組

(A,B,4) 
(A,C,2) 
(B,D,2) 
... 
... 
(E,D,1) 

#include <iostream> 
#include <sstream> 
#include <fstream> 

using namespace std; 


int main() 
{ 
    ifstream inputFile("distance.txt"); 
    string line; 

    while (getline(inputFile, line)) 
    { 
     istringstream ss(line); 

     string name,name2; 
     int var1, var2, var3; 

     ss >> name >>name2>> var1 >> var2; 
     //how do i scan the second alphabet in the same line? 
    } 
} 
+0

搜索StackOverflow上的 「[C++]讀取文件變量」。已經有太多類似的問題了。 –

+0

標題中「熱」是什麼意思? – sashoalm

回答

2

您可以分割由空格整條生產線,採取的第一個元素作爲你的價值和對解析休息。對於每一對,請在數據結構中添加一個新條目。

樣本程序做所有你問:使用精確的輸入文件

#include <iostream> 
#include <fstream> 
#include <sstream> 
#include <cstdlib> 
#include <string> 
#include <map> 
#include <vector> 

using std::ifstream; 
using std::map; 
using std::cout; 
using std::endl; 
using std::istringstream; 
using std::vector; 
using std::pair; 
using std::string; 

typedef map< string, int > mapping; 

map< string, mapping > data; 

int main(int argc, char* argv[]) 
{ 
    ifstream inputFile("data.dat", ifstream::binary); 
    string currentLine; 
    while (getline(inputFile, currentLine)) 
    { 
     if (currentLine.length() == 0) 
      break; 
     istringstream lineStream(currentLine); 
     string node, destination; 
     int length; 
     lineStream >> node; 
     while (lineStream >> destination) 
     { 
      length = 0; 
      if (lineStream >> length) 
      { 
       if (data.find(node) == data.end()) 
       { 
        mapping newMapping; 
        data.insert(pair< string, mapping >(node, newMapping)); 
       } 
       data[node].insert(pair< string, int >(destination, length)); 
      } 
      else 
      { 
       cout << "dijkstra format error. exit." << endl; 
       return 1; 
      } 
     } 
    } 
    inputFile.close(); 
    for (auto dataIt = data.begin(); dataIt != data.end(); ++dataIt) 
    { 
     mapping& currentMapping = dataIt->second; 
     for (auto mappingIt = currentMapping.begin(); mappingIt != currentMapping.end(); ++mappingIt) 
     { 
      cout << dataIt->first << " <- (" << mappingIt->second << ") -> " << mappingIt->first << endl; 
     } 
    } 
} 

輸出:

A <- (4) -> B 
A <- (2) -> C 
B <- (3) -> C 
B <- (2) -> D 
B <- (3) -> E 
C <- (1) -> B 
C <- (4) -> D 
C <- (5) -> E 
E <- (1) -> D 
+0

@安德烈的代碼很好地說明了你的解釋。你也應該有一個代碼示例。 –

+0

@πάνταῥεῖ全部完成:-) – poljpocket

0
#include <iostream> 
#include <sstream> 
#include <fstream> 
#include <map> 
#include <string> 

using namespace std; 

int main() { 
    ifstream inputFile("distance.txt"); 
    string line; 
    std::map<std::pair<std::string, std::string>, int> mapping; 
    std::map<std::string, std::string> graph; 

    while (getline(inputFile, line)) { 
     istringstream ss(line); 
     string name; 
     ss >> name; 
     while(ss >> line) { 
      int value = 0; 
      if(!(ss >> value)) //error 
      graph[name] = line; 
      mapping[std::make_pair(name, line)] = value; 
     } 
    } 
}