如何找到两个袋子的交集
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 */
和我的实际程序主要.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
我正在尝试找到两个袋子的交集,这将是一个新袋子,其中包含原始两个袋子中出现的条目。所以基本上我需要设计和指定一个方法交集,该方法交集作为新包返回接收对方法调用的包的交集和作为方法的一个参数的包的交集。假设袋 1 和袋 2 是袋子;bag1 包含字符串 a、b 和 c;而 bag2 包含字符串 b、b、d 和 e。表达式 bag1.intersection(bag2) 返回一个仅包含字符串 b 的包。
我已经制作了两个袋子来相互比较,但我不太确定如何设计交叉方法。
任何帮助都会很棒。谢谢。
由于枚举
包中项目的唯一方法是使用 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;
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 图问题:找出两个节点是否在每个节点的O(1)时间和O(2)存储中共享同一分支
- 如何找出在两个整数的乘积中设置了多少位(等于 1)
- 找出两个给定单词和字典之间最短的单词阶梯
- 找出3D空间中两个对象的公式
- NetBeans - 两个项目 - 一个找不到要包含的文件 (C++)
- 我有两个点和一个时间,我如何找出向量
- 如何找出两个图像之间的对象差异
- 比较两个文件,找出区别
- 比较两个值,找出最接近用户输入的值
- 找出两个异或最大的元素
- 找出图中两个节点之间的所有路径
- 在三维空间的一组点中找出两个距离最远的点
- 给定一个整数数组,找出两个数字,使它们加起来等于一个特定的目标数
- 找出两个范围(其中一个已排序)是否有一个公共元素
- 用C++程序提取两个句子,找出常见的字母