微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C++ 中的模板.. 打印错误的输出

如何解决C++ 中的模板.. 打印错误的输出

我不明白......我的代码没有打印任何东西。我需要创建一个不同的节点还是我写错了代码?请帮忙。一个解释将不胜感激。代码只能到它说“从这里开始 // 到这里结束。外面的任何东西都不能修改

预期答案:

one->eins
two->zwei
three->drei
four->vier
five->funf
one
three
zwei
funf
one->1
two->2
three->3
four->4
five->5
one
three
2
5

代码

#include <iostream>

using namespace std;

//The class my_pair which stores two values of different types referred to as value1 and value2

//Your code starts here
template <class T,class U>
//Your code ends here
class my_pair
{
    //Your code starts here
    T value1;
    U value2;
    //Your code ends here

public:
    //Your code starts here
    my_pair(T value1,U value2)
    {
        value1 = this->value1;
        value2 = this->value2;
    }
    //Your code ends here

    T get_value1()
    {
        return this->value1;
    }
    U get_value2()
    {
        return this->value2;
    }
};

            
//The class node implements a linked list of pairs
template <class T,class U>
class node
{
    my_pair<T,U>* value;
    node* next;

public:
    //Creates a new node
    node(T value1,U value2)
    {
        //Your code starts here
        this->value = new my_pair<T,U>(value1,value2);
        this->next = nullptr;
        //Your code ends here
    }
    //Add a new pair at the end of the list
    void add(T value1,U value2)
    {
        //Your code starts here
        if (this->next == nullptr) this->next = new node(value1,value2);
        this->next = nullptr;
        //Your code ends here
    }
    //Find the value2 corresponding to the value1 passed as parameter
    T find_value1(U value2)
    {
        //Your code starts here
        node* curr = this;
        while (curr != nullptr)
        {
            if (curr->value->get_value2() == value2) 
                return curr->value->get_value1();
            curr->next = nullptr;
        }
        return 0;
        //Your code ends here
    }
    //Find the value1 corresponding to the value2 passed as parameter
    U find_value2(T value1)
    {
        //Your code starts here
        node* current = this;
        while (current != nullptr)
        {
            if (current->value->get_value1() == value1)
                return current->value->get_value2();
            current->next = nullptr;
        }
        return 0;
        //Your code ends here
    }

    void print()
    {
        node* curr = this;
        while (curr != nullptr)
        {
            cout << curr->value->get_value1() << "->" << curr->value->get_value2() << endl;
            curr = curr->next;
        }
    }
};

//After

int main()
{
    node<int,string>* dictionary = new node<int,string>(1,"one");
    dictionary->add(2,"two");
    dictionary->add(3,"three");
    dictionary->add(4,"four");
    dictionary->print();
    cout << endl;
    cout << dictionary->find_value1("one") << endl;
    cout << dictionary->find_value2(4) << endl;
    cout << endl;
    cin.get();

    node<string,string>* dictionary2 = new node<string,string>("uno","one");
    dictionary2->add("dos","two");
    dictionary2->add("tres","three");
    dictionary2->add("cuatro","four");
    dictionary2->print();
    cout << endl;
    cout << dictionary2->find_value2("uno") << endl;
    cout << dictionary2->find_value1("cuatro") << endl;
    cout << endl;
    cin.get();
}

解决方法

注意事项:

  1. 每当您调用 add 时,您都将 nullptr 分配给 next,撤消了函数第一行所做的操作。

  2. 由于主代码在同一个(第一个)节点上调用 add,而不是新添加的节点,因此当前的 add 例程(如果删除 nullptr 赋值) 最终只会在您的列表中出现 2 个节点。您必须找到一种方法来遍历列表并找到最后一个节点,然后将其添加到该节点中。

  3. 您的 find_valueN 陷入无限循环。只要 current 存在,它就会不断重复,但您没有任何改变 current 的东西。您确实有一行更改了 current->next,但这对您没有帮助。 (实际上,这会破坏您正在查看的数据。当您只应该查看数据时,请小心修改数据。)相反,您应该做的是更改该行以使 {{1} } 成为 current 中保存的值。不要更改 current->next 内的任何内容。查看 current 函数底部的示例。

否则看起来还好。模板定义做得很好。 :-)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。