C++ C++;学习函数语法

C++ C++;学习函数语法,c++,function,syntax,C++,Function,Syntax,我目前正在为我的CS类做一个赋值,对如何使用is子集函数的语法感到困惑。我还在写代码。我感到困惑的是(const Set和other)。。。(我知道这意味着一个集合,然后是另一个集合)我只是好奇如何在我的代码中使用它_numItems和_numItems2是我所暗示的,如果在那里使用(const Set&other)。另外,我可以得到关于如何返回此函数的帮助吗。感谢大家的帮助!这是我的密码: bool Set::isSubset(const Set &other) const {

我目前正在为我的CS类做一个赋值,对如何使用is子集函数的语法感到困惑。我还在写代码。我感到困惑的是(const Set和other)。。。(我知道这意味着一个集合,然后是另一个集合)我只是好奇如何在我的代码中使用它_numItems和_numItems2是我所暗示的,如果在那里使用(const Set&other)。另外,我可以得到关于如何返回此函数的帮助吗。感谢大家的帮助!这是我的密码:

bool Set::isSubset(const Set &other) const
{
    for (int i = 0; i < _numItems2; i++) {
        for (int x = 0; i < _numItems; i++)
    }

    return 0;
}
bool Set::isSubset(常量集和其他)常量
{
对于(int i=0;i<\u numItems2;i++){
对于(int x=0;i<\u numItems;i++)
}
返回0;
}
//main.cpp

#include <iostream>
#include <cassert>
#include "Set.h"
using namespace std;

int main() {

    Set s1, s2, s3;

    s1.addElement(7);
    s1.addElement(3);
    s1.addElement(5);
    s2.addElement(3);
    s2.addElement(5);
    std::cout << s1.containsElement(286) << endl;
    std::cout << s1.containsElement(7);

    return 0;
}
#包括
#包括
#包括“Set.h”
使用名称空间std;
int main(){
设置s1、s2、s3;
s1.补遗(7);
s1.补遗(3);
s1.补遗(5);
s2.补遗(3);
s2.补遗(5);

std::cout一种简单的迭代方法:

bool Set::isSubset(const Set &other) const
{
    int j = 0;

    if(other.size() > size()) {
        return false;
    }

    //size() is the function or variable that denotes the number of elements in the set, replace as needed
    for (int i = 0; i < size(); i++) {
        //if the j index is greater or equal to the size, all elements were found in order
        //therefore the set contains every portion of the subset
        if (j >= other.size()) {
            return true;
        }

        //if the items are the same, advance j index and advance i index by continuing to next iteration of loop
        if (*this[i] == other[j]) {
            j++;
            continue;
        }
        //otherwise, if they are not the same reset j index to 0 and start process of finding the subset again
        else {
            j = 0;
        }
    }

    //if i reaches the end of the main set and a subset is not found, it must not contain that subset therefore false
    return false;
}
bool Set::isSubset(常量集和其他)常量
{
int j=0;
如果(其他.size()>size()){
返回false;
}
//size()是表示集合中元素数量的函数或变量,可根据需要替换
对于(int i=0;i=other.size()){
返回true;
}
//如果项目相同,则通过继续循环的下一次迭代来推进j索引和i索引
如果(*此[i]==其他[j]){
j++;
继续;
}
//否则,如果它们不相同,则将j索引重置为0,并再次开始查找子集的过程
否则{
j=0;
}
}
//如果我到达主集合的末尾,但未找到子集,则它不能包含该子集,因此为false
返回false;
}

这个答案假设您的类有一个工作的
[]
operator

如果作为引用传递的集合包含在调用集合中,则函数应返回true。例如:集合1:1 2 3 4集合2:2 3这是真的,因为集合1包含2 3,它被设置为2 Anks for you answer:),我实际上知道,我只是对我将在其中写入的内容感到有点困惑:_numItems2bool Set::isSubset(const Set&other)const{for(int i=0;i<_numItemsSet2;i++){for(int n=0;n<_numItems;n++){if(s1[i]=s2[n])break;}if(_numItemsSet2=_numItems)返回false;}返回true;我已经更新了一点代码。我也很好奇在s1和s1中放什么s2@jnestor嗯,另一组称为“other”,因此这将是一个很好的起点。您还应该从一开始就阅读本书中关于函数的章节。也许move
other.size()
循环外。无论大小,它都不能是子集。