1

我有一个算法应该比较两个单独向量中的字符串并比较第一个向量文件的内容:fileContent 和 fileContent1。如果有字符串匹配,程序应打印出第一个向量的每一行,并报告向量二中是否有匹配的行(如果是这种情况,则打印出“YES:”Vector1 行或“No”Vector1 行)目前我的输出是这样的,我似乎无法弄清楚我做错了什么。任何提示或帮助将不胜感激。

我目前的输出:

NO:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:altimbankery
YES:loessoid
NO:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:loessoid
YES:stampede
YES:stampede
NO:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:stampede
YES:soger
YES:soger
YES:soger
NO:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:soger
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
NO:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:Plutarchical
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
NO:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:gigglesome
YES:unheated
YES:unheated
YES:unheated
YES:unheated
YES:unheated
YES:unheated
NO:unheated
YES:unheated
YES:unheated
YES:unheated
YES:unheated
YES:unheated
YES:unheated
YES:unheated
YES:unheated
YES:unheated
YES:unheated
YES:unheated
YES:unheated
YES:unheated
YES:offendress
YES:offendress
YES:offendress
YES:offendress
YES:offendress
YES:offendress
YES:offendress
NO:offendress
YES:offendress
YES:offendress
YES:offendress
YES:offendress
YES:offendress
YES:offendress
YES:offendress
YES:offendress
YES:offendress
YES:offendress
YES:offendress
YES:offendress
YES:syruper
YES:syruper
YES:syruper
YES:syruper
YES:syruper
YES:syruper
YES:syruper
YES:syruper
NO:syruper
YES:syruper
YES:syruper
YES:syruper
YES:syruper
YES:syruper
YES:syruper
YES:syruper
YES:syruper
YES:syruper
YES:syruper
YES:syruper
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
NO:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:multispindle
YES:stinging
YES:stinging
YES:stinging
YES:stinging
YES:stinging
YES:stinging
YES:stinging
YES:stinging
YES:stinging
YES:stinging
NO:stinging
YES:stinging
YES:stinging
YES:stinging
YES:stinging
YES:stinging
YES:stinging
YES:stinging
YES:stinging
YES:stinging
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
NO:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:Euspongia
YES:recluse
YES:recluse
YES:recluse
YES:recluse
YES:recluse
YES:recluse
YES:recluse
YES:recluse
YES:recluse
YES:recluse
YES:recluse
YES:recluse
NO:recluse
YES:recluse
YES:recluse
YES:recluse
YES:recluse
YES:recluse
YES:recluse
YES:recluse
YES:continuum
YES:continuum
YES:continuum
YES:continuum
YES:continuum
YES:continuum
YES:continuum
YES:continuum
YES:continuum
YES:continuum
YES:continuum
YES:continuum
YES:continuum
NO:continuum
YES:continuum
YES:continuum
YES:continuum
YES:continuum
YES:continuum
YES:continuum
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
NO:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:promotrix
YES:menyie
YES:menyie
YES:menyie
YES:menyie
YES:menyie
YES:menyie
YES:menyie
YES:menyie
YES:menyie
YES:menyie
YES:menyie
YES:menyie
YES:menyie
YES:menyie
YES:menyie
NO:menyie
YES:menyie
YES:menyie
YES:menyie
YES:menyie
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
NO:perpetuator
YES:perpetuator
YES:perpetuator
YES:perpetuator
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
YES:hornblower
NO:hornblower
YES:hornblower
YES:hornblower
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
YES:kurung
NO:kurung
YES:kurung
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
YES:inductionally
NO:inductionally

文件1的内容:

unheated
rheometer
daemonurgy
maltase
morrowless
Plutarchical
czarism
missable
loessoid
siphonozooid
saltimbankery
unreduced
loglike
stampede
clickless
soger
gigglesome
vila

文件2的内容:

saltimbankery
loessoid
stampede
soger
Plutarchical
gigglesome
unheated
offendress
syruper
multispindle
stinging
Euspongia
recluse
continuum
promotrix
menyie
perpetuator
hornblower
kurung
inductionally

算法:

if(algo_speed == "slow")
{
    i = 0;

    while(getline(ifs1, line))
    {
        fileContent1.push_back(line);
        fileContent.push_back(line);
    }

    for(size_t i = 0; i < fileContent1.size() ; i++)
    {
        for(size_t j = 0; j < fileContent.size() ; j++)
        {
            size_t found = fileContent[i].find(fileContent1[j]);
            if(found) {
                cout << "YES:" << fileContent.at(i) << endl;
            } else {
                cout << "NO:" << fileContent.at(i) << endl;
            }

            //sort(fileContent1.begin(), fileContent1.end());
        }
    }
}
4

2 回答 2

0

在你的向量上使用std::find2。你应该很容易解决这个问题

std::for_each(vector1.cbegin(),vector1.cend(), [&](const std::string &s)->void{
    auto it=std::find_if(vector2.cbegin(),vector2.cend(), 
        [&](const std::string & s2)-> bool{
        return s2==s1;
    }
    std::cout<<(it==vector2.cend() ? "NO ":"YES "<<s;
}
于 2013-09-10T21:46:47.030 回答
0

你的问题并不完全清楚。我知道您希望第一个文件中的每个单词都有一行输出。在这种情况下,您在错误的循环中执行输出阶段。您应该在外部循环中执行此操作,而不是内部循环。对于内部循环,您只需循环直到找到。

您遇到的另一个问题是期望string::find返回一个布尔值。这不是真的。它返回一个索引,或者string::npos如果没有找到。您可以改用字符串相等运算符。

因此,使用上述建议,您的代码变为:

for(size_t i = 0; i < fileContent.size() ; i++)
{
    bool found = false;
    for(size_t j = 0; j < fileContent1.size() && !found; j++)
    {
        found = fileContent[i] == fileContent1[j];
    }

    if(found) {
        cout << "YES:" << fileContent.at(i) << endl;
    } else {
        cout << "NO:" << fileContent.at(i) << endl;
    }
}

当然,如果你std::find在向量上使用,你可以完全避免内循环。

bool found = std::find(fileContent1.begin(), fileContent1.end(), fileContent[i]) != fileContent1.end();
于 2013-09-10T21:57:29.337 回答