C++ 如何检查单词是否存在于向量中
我使用向量将文件名检索到路径中。 我只想得到某种类型的文件。这就是为什么我尝试使用.find来检测文件是否具有conllu格式。我想知道字符串是否包含“conllu”C++ 如何检查单词是否存在于向量中,c++,vector,find,C++,Vector,Find,我使用向量将文件名检索到路径中。 我只想得到某种类型的文件。这就是为什么我尝试使用.find来检测文件是否具有conllu格式。我想知道字符串是否包含“conllu” 如果要检查std::vector是否包含特定的std::string,只需执行以下操作: bool contains(const std::string & word, const std::vector<std::string> & set) { bool found(false);
如果要检查
std::vector
是否包含特定的std::string
,只需执行以下操作:
bool contains(const std::string & word, const std::vector<std::string> & set)
{
bool found(false);
for(size_t i = 0; !found && (i < set.size()); ++i)
{
if(set[i] == word)
found = true;
}
return found;
}
我在所有情况下都对它进行了测试,结果都很成功
我希望这能有所帮助
编辑:如果没有已经实现这种功能的现有库,我会非常惊讶。但是,如果我们想自己实施,这是一种方法
编辑2:我意识到我的字符串搜索函数在多次包含同一字母的模式中存在问题。
因此,我编写了一个更好的(简单/简洁/功能性/高效)函数实现,可以处理所有可能的情况:
bool contains(const std::string & str, const std::string & pattern)
{
bool found(false);
if(!pattern.empty() && (pattern.length() < str.length()))
{
for(size_t i = 0; !found && (i <= str.length()-pattern.length()); ++i)
{
if((str[i] == pattern[0]) && (str.substr(i, pattern.length()) == pattern))
{
found = true;
}
}
}
return found;
}
bool包含(常量std::string和str,常量std::string和pattern)
{
bool-found(假);
如果(!pattern.empty()&&(pattern.length() 对于(size_t i=0;!found&(i您需要在向量中的每个字符串中搜索子字符串.conllu
。我建议使用循环和std::string::find
#include <vector>
#include <string>
#include <iostream>
int main() {
std::vector<std::string> v = { "nope", "yes.conllu", "also.conllu", "nothere" };
for (auto& str : v) {
if (str.find(".conllu") != std::string::npos) {
std::cout << "Found .conllu in " << str << std::endl;
}
}
}
#包括
#包括
#包括
int main(){
向量v={“nope”,“yes.conllu”,“allow.conllu”,“nothere”};
用于(自动&str:v){
if(str.find(“.conllu”)!=std::string::npos){
std::cout“它没有检测到conllu”:可能是因为您检查了.conllu。事实上,我想检测例如:el_gdt-ud-dev.conllu文件名中是否有**conllu**查找是否相等。如果您想检测匹配中的子字符串,则必须为其提供一个要检查的函数,如果您不想查找字符串“conllu”,则可能需要使用find\来检查在向量中,但是你想找到一个包含“conllu”的字符串,这是两个不同的东西,因为C++17,你有了更通用的std::file_system
。
bool contains(const std::string & pattern, const std::string & str)
{
bool found(false);
bool ongoing(false);
size_t cursor(0);
for(size_t i = 0; (!pattern.empty()) && !found && (i < str.length()); ++i)
{
if(ongoing)
{
if(str[i] == pattern[0])
{
cursor = 1;
}
else if(str[i] == pattern[cursor])
{
if(cursor == pattern.length()-1)
found = true;
else
++cursor;
}
else
{
ongoing = false;
cursor = 0;
}
}
else
{
if(str[i] == pattern[0])
{
if(pattern.size() == 1)
found = true;
else
{
ongoing = true;
++cursor;
}
}
}
}
return found;
}
bool contains(const std::string & str, const std::string & pattern)
{
bool found(false);
if(!pattern.empty() && (pattern.length() < str.length()))
{
for(size_t i = 0; !found && (i <= str.length()-pattern.length()); ++i)
{
if((str[i] == pattern[0]) && (str.substr(i, pattern.length()) == pattern))
{
found = true;
}
}
}
return found;
}
#include <vector>
#include <string>
#include <iostream>
int main() {
std::vector<std::string> v = { "nope", "yes.conllu", "also.conllu", "nothere" };
for (auto& str : v) {
if (str.find(".conllu") != std::string::npos) {
std::cout << "Found .conllu in " << str << std::endl;
}
}
}