2016-04-23 6 views
1

現在CSクラスの割り当てを行っていますが、isサブセット関数の構文の使い方が混乱していました。私はまだコードに取り組んでいます。私が混乱しているのは、(const Set & other)...(私はそれが1つのセットと別のセットを意味することを知っています)私はちょうど私のコードでそれを使用する方法が不思議です。 _numItemsと_numItems2は、(const Set & other)が使用されている場合、私が意味するところです。また、私はこの関数をどのように返すかについて助けを得ることができます。すべての助けに感謝!ここに私のコードです:C++ラーニング関数の構文

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

関数は、参照として渡されたセットが呼び出しセットに含まれている場合にtrueを返す必要があります。 例: セット1:1 2 3 4 セット2:2 3 セット1が2つの – Stephen

+0

おかげであなたは:)答えるために、私は実際にそれを知ってるが設定されている2 3、I」が含まれているため、これが本当のiretuns { \t \tを_numItems2 – jnestor

+0

が設定しBOOL :: isSubset(constの設定&その他)のconst { \tをするために(;;私は<_numItemsSet2私は++ i = 0 int型):それは言う。ここで、mはちょうど私が書くでしょうかについて混乱して少し混乱しました(s1 [i] == s2 [n])\t \t \t if(int n = 0; n jnestor

答えて

1

簡単な反復法:

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

この答えは、あなたのクラスは作業[]オペレータを持っていることを前提としてい

+0

は 'other.size()'をループ外に移動させるかもしれません。どんなものであれ、それがサブセットになることはできません。 – Thomas