Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;在类似数组的结构中只给出字符串的一部分时查找字符串_C++_Arrays_String_Partial - Fatal编程技术网

C++ c++;在类似数组的结构中只给出字符串的一部分时查找字符串

C++ c++;在类似数组的结构中只给出字符串的一部分时查找字符串,c++,arrays,string,partial,C++,Arrays,String,Partial,它是以单词的形式出现的,假设我得到了字符串“foo”,在我的数组中有“food”、“fool”、“foo”等单词。所有三个都应该打印出来 我还没有做一个坚实的尝试,因为我不知道如何把我的头围绕着它。有什么想法吗?您可以做一些简单的事情,比如迭代字符串中的每个字符,并使用单独的函数对照您试图匹配的字符串中的字符进行检查。如果一行中有三个字符与要搜索的字符串匹配,请将其添加到向量或其他内容中并显示它们 // Variables bool charMatched = false; vector<

它是以单词的形式出现的,假设我得到了字符串“foo”,在我的数组中有“food”、“fool”、“foo”等单词。所有三个都应该打印出来


我还没有做一个坚实的尝试,因为我不知道如何把我的头围绕着它。有什么想法吗?

您可以做一些简单的事情,比如迭代字符串中的每个字符,并使用单独的函数对照您试图匹配的字符串中的字符进行检查。如果一行中有三个字符与要搜索的字符串匹配,请将其添加到向量或其他内容中并显示它们

// Variables
bool charMatched = false;
vector<string> *stringVec = new vector<string>();
int index = 0;
int counter = 0;
string str = "Whatever you are trying to match";

for (char &c : strings[index])  // For each character in string
{
    // Check for match
    if (checkChar(c))
    {
        counter++;
        charMatched = true;
        if(counter == str.length())
            stringVec->push_back(strings[index]);
    }
    else
    {
        index++;
        counter = 0;
            break;
    }
}

bool checkChar(char c)
{
    // Iterator to go through match string
        static string::iterator it = str.begin();

        if (c == *it)
        {
            if (it == str.end())
                it = str.begin();   // Reset iterator
            else
                it++;               // Increment iterator
            return true;
        }

        else
        {
            if (it == str.end())
                it = str.begin();   // Reset iterator
            else
                it++;               // Increment iterator
            return false;
        }
}
//变量
布尔匹配=假;
vector*stringVec=新向量();
int指数=0;
int计数器=0;
string str=“无论您试图匹配什么”;
for(char&c:strings[index])//对于字符串中的每个字符
{
//检查是否匹配
if(checkChar(c))
{
计数器++;
charMatched=true;
如果(计数器==str.length())
stringVec->push_back(字符串[索引]);
}
其他的
{
索引++;
计数器=0;
打破
}
}
布尔校验字符(字符c)
{
//遍历匹配字符串的迭代器
静态字符串::迭代器it=str.begin();
如果(c==*it)
{
如果(it==str.end())
it=str.begin();//重置迭代器
其他的
it++;//增量迭代器
返回true;
}
其他的
{
如果(it==str.end())
it=str.begin();//重置迭代器
其他的
it++;//增量迭代器
返回false;
}
}

您必须对其进行一些调整,才能以您希望的方式使用数组,但类似的操作应该可以满足您的需要。我没有通过编译器运行它,而是在记事本中编写的,所以可能会有一些小的语法错误。我希望这有帮助

假设您使用的是
std::string
,您可以使用
string::find
查看一个字符串是否包含在另一个字符串中

如果您有一个字符串向量,您可以将其与(例如)
std::remove\u copy\u If
一起使用,以打印出向量中包含所选单词的所有单词:

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


int main() { 
    std::vector<std::string> words{"food", "fool", "foo", "tofoo", "lood", "flood"};

    std::string word = "foo";

    std::remove_copy_if(words.begin(), words.end(),
                        std::ostream_iterator<std::string>(std::cout, "\n"),
                        [&](std::string const &s) {
                            return s.find(word) == std::string::npos;
                        });
}

他需要50个代表来评论…我不应该被否决(/)我不能评论,但既然你们这么快就把我钉死在十字架上,我已经把它变成了一个答案。谢谢。@1。继续贡献,你会得到足够的代表在任何时候发表评论。尽管你的代码正在泄漏。您不应该动态创建
stringVec
,为什么不使用自动对象呢?不要使用全局变量。@Blink,您也不能在全局范围内有一个
for
循环。那么什么是字符串呢?@Snps就像我说的,这只是一个大概的草图,说明了什么可以让这项工作成功。这不是我设置.cpp文件的方式。。。它需要调整。我没有正确处理内存管理或正确声明变量。“strings[]”是指原始问题中提到的数组。一个用来保存一系列要检查的字符串的工具。
food
fool
foo
tofoo