C++ 如何递归比较向量?

C++ 如何递归比较向量?,c++,loops,recursion,C++,Loops,Recursion,我试图编写一个递归检查给定向量a是否在向量B的任何连续块中的函数。例如,如果a={5,6,7}和B={1,2,3,4,5,6,7},它应该返回true。如果B={1,2,3,4,5,7,6},它应该返回false。目前,我的代码一直输出true,因为我认为我的逻辑不正确。我还不能修改它以产生任何结果。任何帮助都将不胜感激 bool r_app(vector<int>& a1, vector<int> b1, size_t k) { k=0; if

我试图编写一个递归检查给定向量a是否在向量B的任何连续块中的函数。例如,如果
a={5,6,7}
B={1,2,3,4,5,6,7}
,它应该返回true。如果
B={1,2,3,4,5,7,6}
,它应该返回
false
。目前,我的代码一直输出
true
,因为我认为我的逻辑不正确。我还不能修改它以产生任何结果。任何帮助都将不胜感激

bool r_app(vector<int>& a1, vector<int> b1, size_t k)
{
    k=0;

    if (a1.size() == 0) {
        cout << "true";
        return true;;
    }

    for (int i=0;i<a1.size();i++){
        for(int j=0;j<b1.size();j++){  
            if (a1.at(i)==b1.at(j)) {
                cout << "true" << endl;
                return true;
            }   
        }
        cout << "false" << endl;
        return false;    

        return r_app(a1,b1,k+1);   
    }
}
bool r_应用程序(向量&a1,向量b1,大小k)
{
k=0;
如果(a1.size()==0){
cout
模板
bool r_应用程序(标准::矢量和a1,标准::矢量和b1,标准::大小和开始){
std::size\u t savedPos=start+1,k=0;
对于(;k
例如:

编辑: 现在更高效的搜索-在上一次搜索结束的地方或在与搜索字符串开头匹配的字符处开始搜索

您还可以通过查看
savedPos-1

打印出第一个匹配发生的位置。如果要递归地执行所有操作,您需要1两个递归函数

一个用于测试序列是否在特定点找到,另一个用于使用另一个函数测试每个点的相等性。下面是一个简单的模板实现,它将与允许迭代的任何STL容器以及非STL序列(如原始数组)一起使用:

模板
布尔递归序列(
针迭代器针开始,
针头,
HaystackIterator haystack_begin,
干草堆迭代器(干草堆)
{
//序列是相等的,因为我们到达了针的末端。
如果(打捆针开始==打捆针结束){
返回true;
}
//如果我们到达干草堆的尽头,或者当前元素不相等
//那么这里的序列就不相等了。
如果(haystack_begin==haystack_end | | |*Pineder_begin!=*haystack_begin){
返回false;
}
//我们既不在干草堆的尽头,也不在针头的尽头,而那些元素是
//相等。继续下一个元素。
返回递归_序列_等于(
++打捆针开始,打捆针结束,
++干草堆(开始,干草堆结束);
}
模板
HaystackIterator递归序列查找(
针迭代器针开始,
针头,
HaystackIterator haystack_begin,
干草堆迭代器(干草堆)
{
//我们到达终点时没有对手。
如果(干草堆开始==干草堆结束){
返回干草堆开始;
}
//如果此时序列相等,则返回haystack迭代器。
如果(递归序列)等于(指针开始,指针结束,
干草堆(开始,干草堆(结束)){
返回干草堆开始;
}
//检查干草堆中的下一个位置。
返回递归\u序列\u查找(
打捆针开始,打捆针结束,
++干草堆(开始,干草堆结束);
}
这样使用:

std::vector<int> a = { 5, 6, 7 };
std::vector<int> b = { 1, 2, 3, 4, 5, 6, 7 };

auto found = recursive_sequence_find(
    a.begin(), a.end(),
    b.begin(), b.end());

if (found != b.end()) {
    // There was a match, found is an iterator to the beginning of the match in b.
} else {
    // No match.  (Or both containers were empty!)
}
std::向量a={5,6,7};
向量b={1,2,3,4,5,6,7};
自动查找=递归\u序列\u查找(
a、 begin(),a.end(),
b、 begin(),b.end());
如果(找到!=b.end()){
//有一个匹配项,在b中找到匹配项开头的迭代器。
}否则{
//不匹配。(或者两个容器都是空的!)
}
()


1。技术上,如果你使用一些额外的参数来表示你是否处于相等测试的中间,你可以用一个函数来完成这一点。然而,这增加了一个额外的复杂度,因为没有两个不同的递归函数。

#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
using namespace std;
bool r_app(vector<int> a, vector<int> b, int starta, int startb)
{
    if(a.size() == 0) return 0;
    if(starta == 0) {
        int i=0;
        while(1) {
            while(a[0] != b[i] && i < b.size())
                i++;
            if(i >= b.size()) return 0;
            if(r_app(a, b, starta+1, i+1) == 1)
                return 1;
            else i++;
        }
    }
    else {
        if(starta == a.size())
            return 1;
        else if(startb == b.size())
            return 0;
        else if(a[starta] == b[startb])
            return r_app(a, b, starta+1, startb+1);
        else
            return 0;
    }   
}
int main() {
    vector<int> a;
    vector<int> b;
    b.push_back(1);
    b.push_back(2);
    b.push_back(3);
    b.push_back(4);
    b.push_back(5);
    a.push_back(3);
    a.push_back(4);
    cout << r_app(a,b,0,0);
    return 0;
}
#包括 #包括 #包括 使用名称空间std; 布尔r_应用程序(向量a、向量b、整数星塔、整数星塔) { 如果(a.size()==0)返回0; 如果(starta==0){ int i=0; 而(1){ 而(a[0]!=b[i]&&i=b.size())返回0; 如果(r_app(a,b,starta+1,i+1)==1) 返回1; 否则i++; } } 否则{ 如果(starta==a.size()) 返回1; else if(startb==b.size()) 返回0; 如果(a[starta]==b[startb]) 返回r_应用程序(a、b、starta+1、startb+1); 其他的 返回0; } } int main(){ 载体a; 载体b; b、 推回(1); b、 推回(2); b、 推回(3); b、 推回(4); b、 推回(5); a、 推回(3); a、 推回(4);
使用迭代器进行我的尝试:

#include <iostream>
#include <vector>
#include <algorithm>

template<typename Iter>
bool r_app(Iter first_a, Iter last_a, Iter first_b, Iter last_b)
{
    if(first_a == last_a)   //trivial case
        return true;

    auto found = std::find(first_b, last_b, *first_a);
    if(found == last_b)
        return false;
    else
        return r_app(++first_a, last_a, found, last_b);   //recursion
}

int main()
{
    std::vector<int> a{5,6,7};
    std::vector<int> b{1,2,3,4,5,6,7};
    std::cout << r_app(a.begin(),a.end(),b.begin(),b.end());

    return 0;
}
#包括
#包括
#包括
模板
布尔r_应用程序(国际热核实验堆第一个、国际热核实验堆最后一个、国际热核实验堆第一个、国际热核实验堆最后一个)
{
if(first_a==last_a)//一般情况
返回true;
自动查找=标准::查找(第一个、最后一个、*第一个);
如果(找到==最后一次)
返回false;
其他的
返回r_app(++first_a,last_a,found,last_b);//递归
}
int main()
{
std::向量a{5,6,7};
std::向量b{1,2,3,4,5,6,7};

std::cout
a1.at(i)=b1.at(j)
不是相等测试,您将一个向量中的值分配给另一个向量中的值。您忘记了一个等号。(
=
是赋值;
=
是相等测试。)此外,即使使用实际的相等测试,这也会在第一个相等元素上停止并声明测试已完成——如果第一个向量包含与第二个向量中的任何元素相等的元素,它将返回true。@cdhowie这完全让我忘记了,谢谢。但是,我仍然不确定如何修改循环以使其检查如果所有元素
std::vector<int> a = { 5, 6, 7 };
std::vector<int> b = { 1, 2, 3, 4, 5, 6, 7 };

auto found = recursive_sequence_find(
    a.begin(), a.end(),
    b.begin(), b.end());

if (found != b.end()) {
    // There was a match, found is an iterator to the beginning of the match in b.
} else {
    // No match.  (Or both containers were empty!)
}
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
using namespace std;
bool r_app(vector<int> a, vector<int> b, int starta, int startb)
{
    if(a.size() == 0) return 0;
    if(starta == 0) {
        int i=0;
        while(1) {
            while(a[0] != b[i] && i < b.size())
                i++;
            if(i >= b.size()) return 0;
            if(r_app(a, b, starta+1, i+1) == 1)
                return 1;
            else i++;
        }
    }
    else {
        if(starta == a.size())
            return 1;
        else if(startb == b.size())
            return 0;
        else if(a[starta] == b[startb])
            return r_app(a, b, starta+1, startb+1);
        else
            return 0;
    }   
}
int main() {
    vector<int> a;
    vector<int> b;
    b.push_back(1);
    b.push_back(2);
    b.push_back(3);
    b.push_back(4);
    b.push_back(5);
    a.push_back(3);
    a.push_back(4);
    cout << r_app(a,b,0,0);
    return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>

template<typename Iter>
bool r_app(Iter first_a, Iter last_a, Iter first_b, Iter last_b)
{
    if(first_a == last_a)   //trivial case
        return true;

    auto found = std::find(first_b, last_b, *first_a);
    if(found == last_b)
        return false;
    else
        return r_app(++first_a, last_a, found, last_b);   //recursion
}

int main()
{
    std::vector<int> a{5,6,7};
    std::vector<int> b{1,2,3,4,5,6,7};
    std::cout << r_app(a.begin(),a.end(),b.begin(),b.end());

    return 0;
}