仅使用 C++ 从文本文件中读取同一行中特定搜索字符串后的 string/character/data

read string/character/data after specific searched string in same line only from a text file using C++

我有一个文本文件,内容是:

# Details for object 1 ("PASperson")
# Center point -- not available in other PASCAL databases -- refers
# to person head center
Original label for object 1 "PASperson" : "UprightPerson"
Center point on object 1 "PASperson" (X, Y) : (281, 169)
Bounding box for object 1 "PASperson" (Xmin, Ymin) - (Xmax, Ymax) : (250, 151) - (299, 294)

# Details for object 2 ("PASperson")
# Center point -- not available in other PASCAL databases -- refers
# to person head center
Original label for object 2 "PASperson" : "UprightPerson"
Center point on object 2 "PASperson" (X, Y) : (247, 373)
Bounding box for object 2 "PASperson" (Xmin, Ymin) - (Xmax, Ymax) : (215, 354) - (274, 466)

现在我想获取 Xmin Ymin Xmax Ymax 值并在我的 C++ 程序中使用这些坐标与我新生成的坐标进行比较。那么如何做到这一点。我可以从文本文件中获取字符串 "Xmax, Ymax" 但我不知道如何读取之后获取坐标。我的代码是搜索字符串是:

int main()
    {
        std::string data;
        ifstream read_file("crop001501.txt",ios::in);
        char *search="Xmax, Ymax";
        int offset;
    while(std::getline(read_file ,data))
{
    //read_file>>data;
    //cout<<data<<endl;
    if ((offset = data.find(search, 0)) != string::npos) {
    cout << "found '" << search << endl;}
}
cin.get();
return 0;
}

此程序在每一行中搜索字符串 Xmax、Ymax,只要找到就打印。但我想要这个字符串的指针,这样我就可以增加指针值并读取 Xmin 的值然后跳过,然后读取 Ymin 的值然后跳过) - 并读取 Xmax 的值和 Ymax 的相同值。获得价值后转到下一行并做同样的事情。如果有人知道读取这些值的任何其他方法,请告诉我,我不需要 X,Y 的值。如果有任何错误那么对不起我是新来的。提前致谢

#include <iostream>
#include <string>
#include <regex>
#include <cassert>
#include <chrono>
#include <sstream>

using namespace std;

int main(void){

    string line("Bounding box for object 1 \"PASperson\" (Xmin, Ymin) - (Xmax, Ymax) : (250, 151) - (299, 294)");

    // METHOD 1 - using regular expressions
    {
        regex e("object ([[:digit:]]+) \"PASperson\" \(Xmin, Ymin\) - \(Xmax, Ymax\) : \(([[:d:]]+), ([[:d:]]+)\) - \(([[:d:]]+), ([[:d:]]+)\)");

        smatch m;

        bool found = regex_search(line, m, e);

        if (found == true){
            assert(m.size() == 6);

            int object_id, xmin, xmax, ymin, ymax;

            object_id = stoi(m[1]);
            xmin = stoi(m[2]);
            ymin = stoi(m[3]);

            xmax = stoi(m[4]);
            ymax = stoi(m[5]);

            cout << "Object ID = " << object_id << endl;
            cout << "Xmin = " << xmin << endl;
            cout << "Ymin = " << ymin << endl;
            cout << "Xmax = " << xmax << endl;
            cout << "Ymax = " << ymax << endl;
        }
    }

    // METHOD 2 : using stringstream
    {
        istringstream ss(line);
        int object_id;
        string xmin_s, xmax_s, ymin_s, ymax_s;
        string other;
        ss >> other; ss >> other; ss >> other; ss >> other; // ignore 4 words
        ss >> object_id;
        ss >> other; ss >> other; ss >> other; ss >> other; ss >> other; ss >> other; ss >> other;  // ignore 7 words
        ss >> xmin_s; ss >> ymin_s; 
        ss >> other; // ignore -
        ss >> xmax_s; ss >> ymax_s;

        // get rid of leading/trailing symbols (,)
        xmin_s.erase(xmin_s.begin()); xmin_s.erase(xmin_s.end() - 1);
        xmax_s.erase(xmax_s.begin()); xmax_s.erase(xmax_s.end() - 1);
        ymin_s.erase(ymin_s.end() - 1);
        ymax_s.erase(ymax_s.end() - 1);

        int xmin, xmax, ymin, ymax;

        // convert strings to integers
        xmin = stoi(xmin_s);
        ymin = stoi(ymin_s);
        xmax = stoi(xmax_s);
        ymax = stoi(ymax_s);

        cout << "Object ID = " << object_id << endl;
        cout << "Xmin = " << xmin << endl;
        cout << "Ymin = " << ymin << endl;
        cout << "Xmax = " << xmax << endl;
        cout << "Ymax = " << ymax << endl;        
    }
}