gpt4 book ai didi

c++ - 如何使用 等模板测试给定的 ADT 实现?

转载 作者:行者123 更新时间:2023-11-28 04:25:01 25 4
gpt4 key购买 nike

我正在处理一个需要实现两个 ADT 的问题。实现后,我需要使用以下模板组合测试我的包实现:

<int, string> -- 所有函数 <string, int> -- 仅插入和查找函数

到目前为止,我的测试一直是输入整数来测试不同的函数。我不明白用模板测试实现是什么意思。

这是我的 bagADT 实现:

#include <stdlib.h>
#include "bagADT.h"

template <typename E>
class ABag : public Bag<E> {
private:
int maxSize;
int listSize;
E* listArray;
public:
ABag(int size = defaultSize) { // Constructor
maxSize = size;
listSize = 0;
listArray = new E[maxSize];
}
~ABag() { delete[] listArray; } // Destructor

bool addItem(const E& item) {
if (listSize >= maxSize) {
return false;
}
listArray[listSize] = item;
std::cout << "Add Item: Added " << item << " in spot " << listSize << std::endl;
listSize++;
return true;
}

bool remove(E& item) {
for (int i = 0; i < listSize; i++) {
if (listArray[i] == item) {
std::cout << "Remove: Removed " << item << " from position ";
item = i;
std::cout<< item << " and adjusted the location of all other elements." << std::endl;
for (i= item; i < listSize; i++) {
listArray[i] = listArray[i + 1];
}
listSize--;
return true;
}
}
return false;
}

bool removeTop(E& returnValue) {
if (listSize == 0) {
return false;
}
else {
returnValue = listArray[listSize - 1];
std::cout << "Remove Top: Removed " << returnValue << " from the top of the stack." << std::endl;
for (int i = listSize; i < maxSize; i++) {
listArray[i] = listArray[i + 1];
}
listSize--;
return true;
}
}

bool find(E& returnValue) const {
for (int i = 0; i < (listSize - 1); i++) {
if (listArray[i] == returnValue) {
returnValue = i;
return true;
}
}
return false;
}

bool inspectTop(E& item) const {
if (listSize == 0) {
return false;
}
else {
item = listArray[listSize - 1];
std::cout << "Inspect Top: The value on top is currently " << item << "." << std::endl;
return true;
}
}

void emptyBag() {
delete[] listArray;
listSize = 0;
listArray = new E[maxSize];
std::cout << "Empty Bag: Emptied the bag." << std::endl;
}

bool operator+=(const E& addend) {
if (listSize < maxSize) {

return true;
}
return false;
}

int size() const {
std::cout << "Size: Number of elements in listArray: " << listSize << std::endl;
return (listSize - 1);
}
int bagCapacity() const {
std::cout << "Bag Capacity: The capacity of this bag is " << maxSize << std::endl;
return maxSize;
}
};

这是我的教授提供的另一个文件,名为 kvpairs :

#ifndef KVPAIR_H
#define KVPAIR_H

// Container for a key-value pair
// Key object must be an object for which the == operator is defined.
// For example, int and string will work since they both have == defined,
// but Int will not work since it does not have == defined.
template <typename Key, typename E>
class KVpair {
private:
Key k;
E e;
public:
// Constructors
KVpair() {}
KVpair(Key kval, E eval)
{
k = kval; e = eval;
}
KVpair(const KVpair& o) // Copy constructor
{
k = o.k; e = o.e;
}

void operator =(const KVpair& o) // Assignment operator
{
k = o.k; e = o.e;
}

bool operator==(const KVpair& o) const {
if (o.k == k) {
return true;
}
return false;
}

//The following overload is provided by Adam Morrone, Spring 2016 class.
//Thanks Adam :)
friend ostream& operator<<(ostream& os, const KVpair& o) // output print operator
{
os << "Key: " << o.k << " Value: " << o.e;
return os;
}



// Data member access functions
Key key() { return k; }
void setKey(Key ink) { k = ink; }
E value() { return e; }
};


#endif

我应该使用上述模板显示测试输出,但我不知道该怎么做。另外,忽略 += 重载。这是不正确的,我知道。我应该重载它以直接向数组添加一个新的 int。

最佳答案

我想我现在明白了。我可能是错的,但这是我的猜测。

您的包是单独模板化的,但它将容纳 KVpair .他们说他们会用KVpair<int, string><string, int> .

当他们谈论测试它时,这意味着他们将按如下方式实例化它:

int main() {
ABag<KVPair<int, string>> bag;
bag.addItem(KVpair(1, "hi"));
//...
}

我很确定他们所说的“用模板测试它”就是这个意思。

作为一个小的编辑,我不知道你使用的是什么 C++ 版本,但如果它非常陈旧,你可能需要编写像 ABag<KVPair<int, string> > 这样的模板实例化。而不是将它们放在一起。我依稀记得这是很久以前的一个问题。

关于c++ - 如何使用 <int, int> 和 <string, int> 等模板测试给定的 ADT 实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54546448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com