1

わかりましたので、このコードを書き留めました。

バッグインターフェース

#ifndef BAGINTERFACE_H
#define    BAGINTERFACE_H

#include <vector>
#include <algorithm>

template<class ItemType>
class BagInterface
{
    public:

    virtual int getCurrentSize() const = 0;
    virtual bool isEmpty() const = 0;
    virtual bool add(const ItemType& newEntry) = 0;
    virtual bool remove(const ItemType& anEntry) = 0;
    virtual void clear() = 0;
    virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
    virtual bool contains(const ItemType& anEntry) const = 0;
    virtual std::vector<ItemType> toVector() const = 0;
}; 

#endif    /* BAGINTERFACE_H */

バッグ #ifndef BAG_H #define BAG_H

#include "BagInterface.h"

template <class ItemType>
class Bag: public BagInterface<ItemType>
{

public:

    int getCurrentSize() const { return v.size(); }
    bool isEmpty() const { return v.empty(); }
    bool add(const ItemType& newEntry) { v.push_back(newEntry); return true; }
    bool remove(const ItemType& anEntry) { std::remove(v.begin(), v.end(), anEntry); return true; }
    void clear() { v.clear(); }
    int getFrequencyOf(const ItemType& anEntry) const { return std::count(v.begin(), v.end(), anEntry); }
    bool contains(const ItemType& anEntry) const { return true; }
    std::vector<ItemType> toVector() const { return v; }

private:

  std::vector<ItemType> v;

};

#endif    /* BAG_H */

そして私の実際のプログラム main.cpp

#include <iostream> // For cout and cin
#include <string> // For string objects
#include "Bag.h" // For ADT bag
using namespace std;
int main()
{
string clubs[] = { "Joker", "Ace", "Two", "Three",
"Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack",
"Queen", "King" };
// Create our bag to hold cards
Bag<string> grabBag;
Bag<string> dumpBag;

grabBag.add(clubs[1]);
grabBag.add(clubs[2]);
grabBag.add(clubs[4]);
grabBag.add(clubs[8]);
grabBag.add(clubs[10]);
grabBag.add(clubs[12]);

dumpBag.add(clubs[3]);
dumpBag.add(clubs[5]);
dumpBag.add(clubs[7]);
dumpBag.add(clubs[9]);
dumpBag.add(clubs[10]);
dumpBag.add(clubs[12]);

Bag<string> Itersection(Bag<string> bagToCompare){


    return grabBag;
}


return 0;
}; // end main

元の 2 つのバッグの両方で発生するエントリを含む新しいバッグとなる 2 つのバッグの交差を見つけようとしています。基本的には、メソッドの呼び出しを受け取るバッグと、メソッドの 1 つの引数であるバッグの交差を新しいバッグとして返すメソッド交差を設計して指定する必要があります。bag1 と bag2 がバッグであるとします。bag1 には文字列 a 、 b 、および c が含まれます。そして、bag2 には文字列 b 、 b 、 d 、および e が含まれます。式 bag1.intersection(bag2) は、文字列 b のみを含むバッグを返します。

私はすでに2つのバッグを互いに比較するように作成しましたが、交差方法を設計する方法がよくわかりません.

どんな助けでも素晴らしいでしょう。ありがとうございました。

4

3 に答える 3

0

クラブに列挙型を使用してみてください。文字列を使用できますが、文字列の比較には注意が必要です。

enum ClubsType { 
    Joker, 
    Ace, 
    Two, 
    Three, 
    Four, 
    Five, 
    Six, 
    Seven, 
    Eight, 
    Nine, 
    Ten, 
    Jack, 
    Queen, 
    King,
    ClubsTypeSize
}

Bag<ClubsType> Intersection(const Bag<ClubsType>& other) {
    set<ClubsType> common;
    int n = v.size();
    for (int i=0;i<n;i++) {
        common.insert(v[i]);
    }
    otherAsVector = other.toVector();
    n = otherAsVector.size();
    for (int i=0;i<n;i++) {
        common.insert(otherAsVector[i]);
    }
    Bag<ClubsType> result;
    for (set<ClubsType>::iterator it = common.begin(); it!=common.end();++it) {
        result.add(*it);
    }
    return result;
}
于 2013-09-17T06:44:08.053 に答える