gpt4 book ai didi

C++学习函数语法

转载 作者:行者123 更新时间:2023-11-30 05:28:31 26 4
gpt4 key购买 nike

我目前正在为我的 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;
}

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

最佳答案

一个简单的迭代方法:

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

这个答案假设你的类(class)有一个有效的 [] 运算符

关于C++学习函数语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36806170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com