テキスト ファイルを読み取って、ベクターに挿入する必要があります。vector<KeyPoint>
次のようにテキストファイルに書き込みます。
vector<KeyPoint> kp_object;
std::fstream outputFile;
outputFile.open( "myFile.txt", std::ios::out ) ;
for( size_t ii = 0; ii < kp_object.size( ); ++ii ){
outputFile << kp_object[ii].pt.x << " " << kp_object[ii].pt.y <<std::endl;
}
outputFile.close( );
ベクトルをファイルに書き込むと、次のようになります。
121.812 223.574
157.073 106.449
119.817 172.674
112.32 102.002
214.021 133.875
147.584 132.68
180.764 107.279
各行はスペースで区切られます。
しかし、それを読み取ってコンテンツをベクターに挿入することができませんでした。次のコードでは、コンテンツを読み取ってベクターを挿入するときにエラーが発生します。
std::ifstream file("myFile.txt");
std::string str;
int i = 0;
while (std::getline(file, str))
{
istringstream iss(str);
vector<string> tokens;
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter<vector<string> >(tokens));
std::string fist = tokens.front();
std::string end = tokens.back();
double dfirst = ::atof(fist.c_str());
double dend = ::atof(end.c_str());
kp_object1[i].pt.x = dfirst;
kp_object1[i].pt.y = dend;
++i;
}