我有一个算法,它应该比较两个单独的向量中的字符串,并比较第一个向量文件的内容:fileContent与fileContent1。如果有一个字符串匹配,程序应该打印出第一个Vector的每一行,并报告是否有一个匹配的向量2中的行(如果是这样的情况打印出“是:”Vector1行或“否”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
file1的内容:
unheated
rheometer
daemonurgy
maltase
morrowless
Plutarchical
czarism
missable
loessoid
siphonozooid
saltimbankery
unreduced
loglike
stampede
clickless
soger
gigglesome
vila
file2的内容:
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());
}
}
}
答案 0 :(得分:0)
在向量2上使用std::find
。
你应该轻易做到这一点
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;
}
答案 1 :(得分: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();