C++学习函数语法

C++ Learning Function Syntax

本文关键字:语法 函数 学习 C++      更新时间:2023-10-16

我目前正在为我的CS类做作业,并且对如何使用is子集函数的语法感到困惑。我仍在处理代码。我感到困惑的是(常量集和其他)...(我知道这意味着一组,然后是另一组)我只是好奇如何在我的代码中使用它。_numItems和_numItems2是我暗示的地方,如果这是使用(常量集和其他)的地方。另外,我能否获得有关如何返回此函数的帮助。所有的帮助感谢!这是我的代码:

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

主.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;
}

一个简单的迭代方法:

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;
}

此答案假设您的类具有工作[]运算符