根据某些标准存储在向量中

发布于 2025-01-19 06:31:23 字数 2973 浏览 3 评论 0原文

早上好,堆栈溢出,

我很难在符合某些标准时将一些钥匙值对存储在矢量中。到目前为止,除此之外,我的程序中的一切都在工作。

在这里开始是我的主要:

int main()
{
    Records passports;
    std::string filename = "test.txt";
    std::ifstream inFile(filename);
    std::string liNe;
    while (!inFile.eof()) {
        getline(inFile, liNe);
        if (liNe.length() == 0) passports.keyHolder2.push_back(" ");
        std::istringstream ss(liNe);
        while (ss >> liNe) {

            passports.keyHolder2.push_back(liNe);
           
        }
    }
    inFile.close();
    cout << isinRangeTest(passports) << endl;
}

这是我存储所有向量的结构:

struct Records {

    std::vector<string> keyHolder;
    std::vector<string> validKeys;
    std::vector<string> keyHolder2;
    std::vector<string> validKeys2;

};

这是我的功能来确定它们是否具有“ ECL,BYR或IYR”等前缀,而其他:

int validPasstest(Records passports)
{
    //cout << passports.keyHolder2.size() << endl;
    std::vector<string>::iterator count;
    string x, z;
    int more = 0;
    bool plus = false;
    for (count = passports.keyHolder2.begin(); count != passports.keyHolder2.end(); count++) {
        //cout << *count << endl;
    }
    for (int i = 0; i < passports.keyHolder2.size(); i++) {
        z = passports.keyHolder2.at(i);
        std::size_t found = z.find("byr");
        if (found != std::string::npos) more++;
        found = z.find("iyr");
        if (found != std::string::npos) more++;
        found = z.find("hgt");
        if (found != std::string::npos) more++;
        found = z.find("hcl");
        if (found != std::string::npos) more++;
        found = z.find("ecl");
        if (found != std::string::npos) passports.validKeys2.push_back(z);
        found = z.find("pid");
        if (found != std::string::npos) more++;
        found = z.find("cid");
        if (found != std::string::npos) more++;
        found = z.find(" ");
        if (found != std::string::npos) 
        {
           /*if (more == 7) {
               plus++;
           }*/
           more = 0;
        }

    }

   
    return plus;
}

这是我的功能,是显示仅具有具有的向量的功能有效的键值对:

int isinRangeTest(Records passports)
{
    for (int i = 0; i < passports.validKeys2.size(); i++) 
    {
        cout << passports.validKeys2.at(i);
   
    }
    
    return 0;
}

当我尝试输出有效keys 2时,我在输出窗口上获得了一个“ 0”,而没有其他选择。这是我的textfile和输出:

ecl:gry pid:860033327 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm

iyr:2013 ecl:amb cid:350 pid:028048884
hcl:#cfa07d byr:1929

hcl:#ae17e1 iyr:2013 cid:150
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm

hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in cid:230


output

在我的函数有效passtest中,我正在尝试添加'z z '到vector vallekeys2。它不起作用。有什么建议吗?

Good morning Stack Overflow,

I am having trouble getting some key-value pairs to store in a vector only if they meet certain criteria which I will explain below. Everything is working in my program so far except this.

To begin here is my main:

int main()
{
    Records passports;
    std::string filename = "test.txt";
    std::ifstream inFile(filename);
    std::string liNe;
    while (!inFile.eof()) {
        getline(inFile, liNe);
        if (liNe.length() == 0) passports.keyHolder2.push_back(" ");
        std::istringstream ss(liNe);
        while (ss >> liNe) {

            passports.keyHolder2.push_back(liNe);
           
        }
    }
    inFile.close();
    cout << isinRangeTest(passports) << endl;
}

Here is my structure to store all my vectors:

struct Records {

    std::vector<string> keyHolder;
    std::vector<string> validKeys;
    std::vector<string> keyHolder2;
    std::vector<string> validKeys2;

};

Here is my function to determine if they have the prefixes like "ecl, byr or iyr" and others:

int validPasstest(Records passports)
{
    //cout << passports.keyHolder2.size() << endl;
    std::vector<string>::iterator count;
    string x, z;
    int more = 0;
    bool plus = false;
    for (count = passports.keyHolder2.begin(); count != passports.keyHolder2.end(); count++) {
        //cout << *count << endl;
    }
    for (int i = 0; i < passports.keyHolder2.size(); i++) {
        z = passports.keyHolder2.at(i);
        std::size_t found = z.find("byr");
        if (found != std::string::npos) more++;
        found = z.find("iyr");
        if (found != std::string::npos) more++;
        found = z.find("hgt");
        if (found != std::string::npos) more++;
        found = z.find("hcl");
        if (found != std::string::npos) more++;
        found = z.find("ecl");
        if (found != std::string::npos) passports.validKeys2.push_back(z);
        found = z.find("pid");
        if (found != std::string::npos) more++;
        found = z.find("cid");
        if (found != std::string::npos) more++;
        found = z.find(" ");
        if (found != std::string::npos) 
        {
           /*if (more == 7) {
               plus++;
           }*/
           more = 0;
        }

    }

   
    return plus;
}

Here is my function to display the vector that only has valid key-value pairs:

int isinRangeTest(Records passports)
{
    for (int i = 0; i < passports.validKeys2.size(); i++) 
    {
        cout << passports.validKeys2.at(i);
   
    }
    
    return 0;
}

When I try to output validKeys 2 I get a "0" on the output window and nothing else. Here is my textfile and output:

ecl:gry pid:860033327 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm

iyr:2013 ecl:amb cid:350 pid:028048884
hcl:#cfa07d byr:1929

hcl:#ae17e1 iyr:2013 cid:150
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm

hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in cid:230


output

In my function validPasstest, I am trying to add 'z' to the vector validKeys2. It is not working. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文