从键盘输入的c++通用数据



是否有一种方法可以从键盘获得通用数据T而无需进入方法调用?

类似如下:

        T data;
        std::cout << "Please enter the data that you would like to delete: ";
        std::cin >> data;
        list.deleteAll(data);

除非我进入deleteAll方法,否则我实际上不能将变量data创建为t。只是想知道是否有办法做到这一点。

LinkedList<int> list;

deleteAll:

void deleteAll(T data) {
    int i = 0;
    Node *curr = this->head, *prev;
    while(i < this->size){
        if(data == this[i].data && i == 0){
            this->head = curr->next;
            std::cout << curr->data << endl;
            delete curr;
        }
        else if(data == this[i].data){
            prev = curr;
            prev->next = curr->next;
            std::cout << curr->data << endl;
            delete curr;
            i++;
        }
        else{
            i++;
        }
        this->size--;
    }
}; //delete all

可以通过模板函数来实现:

template <class T>
void promptAndDelete(LinkedList<T> list) {
    T data;
    std::cout << "Please enter the data that you would like to delete: ";
    std::cin >> data;
    list.deleteAll(data);
}

相关内容

  • 没有找到相关文章

最新更新