For the data file as shown below:
274 means that there are 274 point pairs, each of the following lines represents a point pair, and the four numbers in each line are the x-coordinate and y-coordinate of the first point, the x-coordinate and the y-coordinate of the second point, from left to right. , Now we need to read and store the point pair number and each point pair. The specific code is as follows:
#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<vector>
#include<assert.h>
using namespace std;
struct Correspondence2D2D
{
double p1[2];
double p2[2];
};
typedef vector<Correspondence2D2D> Correspondences2D2D;
int main(){
Correspondences2D2D corr_all;
int n=0;
ifstream in("correspondences.txt");
assert(in.is_open());
string line, word;
int n_line = 0;
while(getline(in, line)){
stringstream stream(line); //等价于stringstream stream; stream<<line; 向流中传值
if(n_line==0){
int n_corrs = 0;
stream>> n_corrs; //将流中的值读取到n_corrs中
corr_all.resize(n_corrs);
cout<<"Total line is "<<n_corrs<<endl;
n_line ++;
continue;
}
if(n_line>0){
stream>>corr_all[n_line-1].p1[0]>>corr_all[n_line-1].p1[1]
>>corr_all[n_line-1].p2[0]>>corr_all[n_line-1].p2[1];
cout<<n_line<<" line is "<<corr_all[n_line-1].p1[0]<<" "<<corr_all[n_line-1].p1[1]<<" "
<<corr_all[n_line-1].p2[0]<<" "<<corr_all[n_line-1].p2[1]<<endl;
}
n_line++;
}
return 0;
}
The results are as follows: