如何输入矩阵样式的txt文件,而不是为C++定义我自己的int 2D数组
How to input an matrix style txt file instead of defining my own int 2D array for C++
所以我对C++还很陌生,但我觉得我已经掌握了一些窍门。
作为excersize的一部分,我必须获取一个输入文本文件,并将其应用于"最短距离算法"中,最终我想输出所有最短距离和路线,但我还没有做到这一点。我使用了Floyd-Warshall算法。现在我的问题是,如何用文本输入替换自写的int数组。输入数组只是数字,但实际上表示节点之间的距离。我现在使用的测试数组只有3个节点,但我希望能够将其扩展到更大的节点数量,比如100。
示例测试矩阵:
0 1234567 100
1234567 0 400
100 400 0
应读作:
node1 node2 node3
node 1 0 999999 100
node 2 999999 0 400
node 3 100 400 0
较大的数字:999999表示一个距离太大而不能算作边。
到目前为止,我的代码看起来像这样:
#include<stdio.h>
// Number of vertices
#define V 3
// Define 999999 as a distance that is too large to represent a edge connection
#define TooLarge 999999
// The print function
void printSolution(int dist[][V]);
// Distance algorithm
void Distance (int distgraph[][V])
{
// output matrix that will have the shortest distance for every vertice
int dist[V][V], i, j, k;
// initial values for shortest distance are based on shortest paths.
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
dist[i][j] = distgraph[i][j];
// Add all vertices to the set of intermediate vertices.
for (k = 0; k < V; k++)
{
// use all vertices as seperate source
for (i = 0; i < V; i++)
{
// use all vertices as destination for the earlier determined source
for (j = 0; j < V; j++)
{
// If vertex k is on the shortest path from i to j, then update the value of dist[i][j]
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
// Print the shortest distance matrix
printSolution(dist);
}
// The print function
void printSolution(int dist[][V])
{
printf ("Shortest distance matrix n");
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i][j] == 999999)
printf("%7s", "TooLarge");
else
printf ("%7d", dist[i][j]);
}
printf("n");
}
}
// driver program to test above function
int main()
{
int distgraph[V][V] = { {0, 1234567, 100},
{1234567, 0, 400},
{100, 400, 0,},
};
// Print the solution
Distance(distgraph);
return 0;
}
希望有人能帮助我,我觉得我只是忘记了一些愚蠢的事情。我尝试使用以下类型的代码导入文本文件:
using namespace std;
double distances [3][3];
int main () {
int x, y;
ifstream in("citytest.txt");
if (!in) {
cout << "Cannot open file.n";
return 0;
}
for (y = 0; y < 3; y++) {
for (x = 0; x < 3; x++) {
in >> distances[x][y];
}
}
cout << distances[3][3] << " " << endl;
in.close();
我知道它有效,但只输入矩阵的预定部分,而我想输入整个数组。(cout函数只是用来测试是否给出了正确的距离作为输入)
除非您知道外部数据文件中的工作量很大,否则您无法有效地分配容器。
因此:
- 标记文件的第一行,并从中获取维度N
- 相应地分配您的容器
- 然后使用文件的其余部分并将数据放入容器中;如果一行的长度与N不匹配,或者如果没有N行,则可能是
throw
你可以考虑
- 用全邻接矩阵表示图是一个有争议的概念;稀疏图的空间效率和时间效率
- 2D c阵列不是矩阵的唯一可能表示;您可以考虑一个扁平的std容器,并在其上实现切片式访问
- 最后,你可能想看看boost::graph
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- #定义c-预处理器常量..我做错了什么
- 用C++中的一个变量定义一个常量
- 部分定义/别名模板模板参数
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- #为""定义宏;静态";针对不同的上下文
- 如何确保C++函数在定义之前声明(如override关键字)
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在命名空间中定义函数还是限定函数
- 此代码是否违反一个定义规则
- 编译C++时未定义的引用
- 不同翻译单元中不可重载的非内联函数定义
- 为什么在定义函数之前先声明它
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- 在类定义之后定义一个私有方法
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- vscode g++链路故障:体系结构x86_64的未定义符号
- 如何定义一个纯抽象基类