如何使用 <int, int> 和 <string, int> 等模板测试给定的 ADT 实现?

How to test the given ADT implementation with templates such as <int, int> and <string, int>?

本文关键字:int gt lt 实现 测试 ADT string 何使用      更新时间:2023-10-16

我正在研究一个需要实现两个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> >一样编写模板实例化,而不是将它们放在一起。我依稀记得这是很久以前的一个问题。