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

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

所以我是 C++ 的新手,但我想我已经掌握了一些窍门。

作为练习的一部分,我必须获取一个输入文本文件并将其应用到 "shortest distance algorithm" 中,最终我想输出所有最短距离和路线,但我还没有做到这一点.我使用了 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
  • 相应地分配容器
  • 然后消费文件的剩余部分,将数据放入容器;可能 throw 如果行的长度与 N 不匹配,或者如果没有 N 行。

您可能会认为

  • 用全邻接矩阵表示图是一个有争议的概念; space-对于稀疏图来说效率低且时间效率低
  • 二维 c 数组不是矩阵的唯一可能表示;你可以考虑一个扁平的 std 容器并在其上实现 slice 风格的访问
  • 最后,您可能想看看 boost::graph