[英]How to Find The Intersection of two Bags
確定,所以我把這段代碼記下來了。
袋接口
#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
我試圖找到兩個袋子的交集,這將是一個新袋子,其中包含在原始兩個袋子中都出現的條目。 因此,基本上我需要設計並指定一個方法交集,該交集作為接收到該方法的調用的包與該方法的一個參數即包的交集作為新包返回。 假設bag1和bag2是bag; bag1包含字符串a,b和c; bag2包含字符串b,b,d和e。 表達式bag1.intersection(bag2)返回僅包含字符串b的bag。
我已經做了兩個袋子的比較,但是我不太確定如何設計交集方法。
任何幫助都會很棒。 謝謝。
由於枚舉袋中物品的唯一方法是使用toVector
,因此您需要遍歷一個輸入袋的toVector
。 對於每個物品,請在兩個輸入袋中均采用該物品的最低頻率,並確保輸出袋中包含該頻率的物品。 由於toVector
可能重復包含相同的項目,因此您必須檢查輸出包以查看它是否已包含您要考慮的項目。
我沒有趕上C ++ 11的步伐,所以我只是用一種老式的方式來做:
template<class T>
Bag<T> intersection(BagInterface<T> const &a, BagInterface<T> const &b) {
Bag<T> c;
std::vector<T> aItems = a.toVector();
for (int i = 0; i < aItems.size(); ++i) {
T const &item = aItems[i];
int needed = std::min(a.getFrequencyOf(item), b.getFrequencyOf(item));
int lacking = needed - c.getFrequencyOf(item);
for ( ; lacking > 0; --lacking) {
c.add(item);
}
}
return c;
}
嘗試對俱樂部使用枚舉。 您可以使用字符串,但是字符串比較比較棘手。
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;
}
<algorithm>
std::set_intersection
對已排序的輸入范圍執行此操作:
vector<int> as { 1, 2, 3 };
vector<int> bs { 2, 3, 4 };
vector<int> cs;
set_intersection(
begin(as), end(as),
begin(bs), end(bs),
back_inserter(cs)
);
// 2 3
for (const auto c : cs)
cout << c << ' ';
它根據輸入范圍的循環進行操作,直到至少一個耗盡為止,然后根據operator<
施加的嚴格弱排序從一個范圍復制出現在另一個范圍內的那些元素。
while (first1 != last1 && first2 != last2) {
if (*first1 < *first2) {
++first1;
} else {
if (!(*first2 < *first1)) {
*output++ = *first1++;
}
++first2;
}
}
return output;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.