Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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++ - Fatal编程技术网

C++ 如何在文本中找到模式?

C++ 如何在文本中找到模式?,c++,C++,我的问题是,我用来在单词中查找模式的代码会一个字母一个字母地查找“模式”。(我的目标是使用我的代码在一个单词中找到整个模式,例如,对于单词“abc”和模式“abc”,我希望得到“3”的结果。) 这是我的密码 case 2:{ string text, pattern; int a, b; int counter = 0; cin >> text >> pattern;

我的问题是,我用来在单词中查找模式的代码会一个字母一个字母地查找“模式”。(我的目标是使用我的代码在一个单词中找到整个模式,例如,对于单词“abc”和模式“abc”,我希望得到“3”的结果。)

这是我的密码

   case 2:{
            string text, pattern;
            int a, b;
            int counter = 0;
            cin >> text >> pattern;

            for (a = 0; a < text.size(); a++) {
                for (b = 0; b < pattern.size(); b++) {
                    if (text[a] == pattern[b])
                        counter++;
                }
            }
            cout << counter;
            break;
         }
案例2:{
字符串文本、模式;
INTA,b;
int计数器=0;
cin>>文本>>模式;
对于(a=0;acout有两种众所周知的通用算法:

如果你需要更特殊的东西,维基百科可能会帮助你找到一个开始的方向:

(字符串匹配算法)[

检查此链接是否有用-从今天早些时候开始-C,但切中要害。或者简单地使用@DavidC.Rankin。问题是关于模式的计数,因此string::find不起作用。当然,它会将
索引返回到模式的开头,因此您只需保留一个偏移量并向下处理字符串。这是否回答了您的问题?