Reading a Matrix txt file and storing as an array

前端 未结 5 1541
名媛妹妹
名媛妹妹 2020-12-03 15:34

I\'m currently writing a Simulated Annealing code to solve a traveling salesman problem and have run into difficulties with storing and using my read data from a txt file. E

5条回答
  •  误落风尘
    2020-12-03 16:06

    How about this? (KISS solution)

    void LoadCities() {
      int x, y;
      ifstream in("Cities.txt");
    
      if (!in) {
        cout << "Cannot open file.\n";
        return;
      }
    
      for (y = 0; y < 15; y++) {
        for (x = 0; x < 15; x++) {
          in >> distances[x][y];
        }
      }
    
      in.close();
    }
    

    Works for me. Might not be that complex and perhaps isn't very performant, but as long as you aren't reading a 1000x1000 array, you won't see any difference.

提交回复
热议问题