繁体   English   中英

string.find()无法打印出所有与字符串匹配的行

[英]string.find() can't printout all lines that matched with the string

我正在使用向量存储所有行(从CSV文件解析),这些行与我要查找的字符串匹配(通过string.find()!= string :: npos)。

但是,结果并未打印出包含字符串的所有行。

CSV档案:

fruit, 1000, abc, d890, 1234
fruit, 1432, abc, d890, 1234
fruit, 8923, abc, d890, 1234
fruit, 1454, abc, d890, 1234
fruit, 2574, abc, d890, 1234
fruit, 1000, abc, d890, 1234
fruit, 1000, abc, d890, 1234
water, 1000, abc, d890, 1234
water, 1000, abc, pat1, 1432
water, 1000, abc, pat2, 8923
water, 1000, abc, pat3, 1454
water, 1000, abc, pat4, 2574
water, 1000, abc, d890, 1234

我想要的输出如下:

fruit, 1432, abc, d890, 1234
fruit, 8923, abc, d890, 1234
fruit, 1454, abc, d890, 1234
fruit, 2574, abc, d890, 1234
water, 1000, abc, pat1, 1432
water, 1000, abc, pat2, 8923
water, 1000, abc, pat3, 1454
water, 1000, abc, pat4, 2574

但是,实际输出是

water, 1000, abc, pat1, 1432
water, 1000, abc, pat2, 8923
water, 1000, abc, pat3, 1454
water, 1000, abc, pat4, 2574

代码如下:

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <windows.h>

    using namespace std;

    void splitter(vector<string> HK, vector<string>& CH)
{
    for(unsigned int i = 0; i < HK.size(); i++)
    {
        char sep = ',';
        string s = HK[i];
        for(size_t p = 0, q = 0; p != s.npos; p = q)
        {
            CH.push_back(s.substr(p + (p != 0), (q = s.find(sep, p + 1)) - p - (p != 0)));
        }
    }
}

    int main()
    {
        ifstream iFile("C:\\abc.csv");
        string line;

        //Opening may fail, perform checking
        if(!iFile)
        {
            cout << "Error, could not open file." << endl;
            return -1;
        }

        string lookForStr[] = {"pat1", "pat2", "pat3", "pat4"};

        //create vector to store BLT of parent and child
        vector<string> lookForStrVec;
        vector<string> lookForStrVecNext;
        vector<string> lookForStrVecNext1;
        vector<string> lookForStrVecNext11;     

        while(!iFile.eof())
        {
            getline(iFile, line);

            for(unsigned int i = 0; i < 4; ++i)                
                {
                    if(line.find(lookForStr[i]) != string::npos)
                    {
                        lookForStrVec.push_back(line);
                    }
                }

                splitter(lookForStrVec, lookForStrVecNext);

                for(unsigned int i = 0; i < lookForStrVecNext.size(); i += 5)
                {
                    if(line.find(lookForStrVecNext[i + 4]) != string::npos)
                    {
                        string str = lookForStrVecNext[i + 4];
                        lookForStrVecNext1.push_back(str);
                    }
                }

                for(unsigned int i = 0; i < lookForStrVecNext1.size(); ++i)
                {
                    if(line.find(lookForStrVecNext1[i]) != string::npos)
                    {
                        lookForChildStrNext11.push_back(line);
                    }
                }
            }
        }

        cout << endl << "print all lines with matched string..." << endl;
        for(unsigned int i = 0; i < lookForStrVecNext11.size(); ++i)
        {
            cout << lookForChildVecNext11[i] << endl;
        }       

        system("pause");
    }

这是一种更简单的方法,打印包含以下内容之一的行:
"1432", "8923", "1454", "2574"

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <vector>

int main()
{
    std::ifstream file("file.txt");
    if (!file.is_open()) {
        std::cout << "Couldn't open file\n";
        return -1;
    }

    std::vector<std::string> find { "1432", "8923", "1454", "2574" };

    std::string line;
    while (std::getline(file, line)) {
        bool found = false;
        std::for_each(begin(find), end(find), [&](std::string s) {
            std::string::size_type n = 0;
            n = line.find(s);
            if (n != std::string::npos) {
                found = true;
                return;
            }
        });

        if (found)
            std::cout << line << '\n';
    }
}

打印出预期的结果:

fruit, 1432, abc, d890, 1234
fruit, 8923, abc, d890, 1234
fruit, 1454, abc, d890, 1234
fruit, 2574, abc, d890, 1234
water, 1000, abc, pat1, 1432
water, 1000, abc, pat2, 8923
water, 1000, abc, pat3, 1454
water, 1000, abc, pat4, 2574

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM