C++ 如何将txt文件读入c++;(dijkstra输入)
假设我有一个文本文件,其中包含以下文本:C++ 如何将txt文件读入c++;(dijkstra输入),c++,C++,假设我有一个文本文件,其中包含以下文本: ----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 ----------------------------------- 该文件的含义是:从A到B的距离是4,A到C的距离是2,B到D的距离是2,B到C的距离是3…等等 我可以知道我应该如何把这个文件读入C++程序变量?< /p> 我想把它做成不同的一套 (A,B,4) (A,C,2) (B,
----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
-----------------------------------
该文件的含义是:从A
到B
的距离是4
,A
到C
的距离是2
,B
到D
的距离是2
,B
到C
的距离是3
…等等
我可以知道我应该如何把这个文件读入C++程序变量?< /p>
我想把它做成不同的一套
(A,B,4)
(A,C,2)
(B,D,2)
...
...
(E,D,1)
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream输入文件(“distance.txt”);
弦线;
while(getline(inputFile,line))
{
istringstream ss(线);
字符串名称,name2;
int var1,var2,var3;
ss>>名称>>名称2>>变量1>>变量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;
}
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用std::ifstream;
使用std::map;
使用std::cout;
使用std::endl;
使用std::istringstream;
使用std::vector;
使用std::pair;
使用std::string;
typedef映射映射;
映射<字符串,映射>数据;
int main(int argc,char*argv[])
{
ifstream输入文件(“data.dat”,ifstream::binary);
串电流线;
while(getline(inputFile,currentLine))
{
如果(currentLine.length()==0)
打破
istringstream lineStream(当前线路);
字符串节点,目的地;
整数长度;
lineStream>>节点;
while(lineStream>>目的地)
{
长度=0;
如果(线流>>长度)
{
if(data.find(node)=data.end())
{
映射新映射;
insert(pair(node,newMapping));
}
数据[节点]。插入(对(目的地,长度));
}
其他的
{
不能包含
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
ifstream输入文件(“distance.txt”);
弦线;
地图映射;
地图图;
while(getline(inputFile,line)){
istringstream ss(线);
字符串名;
ss>>名称;
while(ss>>行){
int值=0;
如果(!(ss>>值))//错误
图形[名称]=直线;
映射[std::make_pair(名称、行)]=值;
}
}
}
如果您更改了文件设计,我认为这将很容易阅读,您可以在空白处断行,或者您可以使用逗号分隔此处的值设计意味着
A、B、C
A 0 3 6
b101
C007
第一行
它显示A的所有输出边。
成本0表示节点之间没有边
表A到表A的成本为0
表格A至表格B的成本为3
表格A至表格c的成本为6
和类似的其他行…搜索StackOverflow以查找“[c++]读取文件变量”。已经有太多类似的问题。标题中的“热”是什么意思?@andre的代码很好地说明了您的解释。您还应该有一个代码示例。
#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
#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;
}
}
}