Reading a Matrix txt file and storing as an array

前端 未结 5 1539
名媛妹妹
名媛妹妹 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:15

    Does it even compile? I get ~7 errors. A sample:

    strtok(cities, "\n");

    strtok()'s first argument is a char * and not a std::string.

    Does this help?

    void LoadCities()
    {
      std::vector f((std::istream_iterator
           (std::ifstream("city.txt"))), /* replace filename with your own */
        (std::istream_iterator()));
      if (!f.empty()) {
        std::cout << f.size() << "\n";
        /* print an arbitrary data point with 2 places of decimal */
        std::cout << std::setprecision(2) << f[ 0 ] << std::endl; 
    
      }
    }
    

    Working with matrices doesn't mean you need to have a multidimensional array. Especially, with 2D arrays. Of course it's easier to read and write ;)

提交回复
热议问题