如何输入矩阵样式的txt文件,而不是为C++定义我自己的int 2D数组

How to input an matrix style txt file instead of defining my own int 2D array for C++

本文关键字:C++ 定义 我自己 数组 2D int 自己的 文件 输入 何输入 txt      更新时间:2023-10-16

所以我对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