C++学习函数语法
C++ Learning Function Syntax
我目前正在为我的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;
}
此答案假设您的类具有工作[]
运算符
相关文章:
- QMetaObject invokeMethod的基于函数指针的语法
- 使用基类指针调用基类的值构造函数的语法是什么?
- C++使用 rand 定义函数语法
- C++中未命名函数指针的语法
- 将值传递给构造函数 c++ 的差异语法
- C++语法中的函数指针
- 存储函数指针的正确语法
- QObject::连接不起作用 - 使用函数语法找不到信号
- 对函数库中的语法感到困惑 std::bind
- 解释通过从函数引用返回数组的语法
- 函数错误 C2059:语法错误:'>'不起作用
- 当C++类函数参数之一是结构时,它们的语法有什么不同
- 将显式指定的函数模板重载作为模板参数传递的正确语法是什么?
- Qt的新信号/时隙语法问题 - 连接到一个简单的函数
- 需要"模板<>"语法 --> 通过函数调用类模板
- C++ std::函数语法问题
- 备用函数语法/函数原型
- 奇怪的c++语法:函数调用之前的类型定义
- c++数组语法(函数返回数组)
- 实现[B,C]=f(A)语法(函数f作用于具有两个或多个输出数组的数组)