<p>由于你的坐标是成对的,为什么不为它们写一个结构?</p><pre>struct CoordinatePair{
讯享网
int x; int y;};
然后你可以为istreams编写一个重载的提取运算符:
讯享网std::istream& operator>>(std::istream& is, CoordinatePair& coordinates){ is >> coordinates.x >> coordinates.y;
讯享网 return is;}
然后你可以直接将坐标文件读入这样的矢量:
#include <fstream>#include <iterator>#include <vector>int main(){ char filename[] = "coordinates.txt"; std::vector<CoordinatePair> v; std::ifstream ifs(filename); if (ifs) { std::copy(std::istream_iterator<CoordinatePair>(ifs), std::istream_iterator<CoordinatePair>(), std::back_inserter(v)); } else { std::cerr << "Couldn‘t open " << filename << " for reading "; } // Now you can work with the contents of v}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/161610.html