【问题标题】:Template in C++ .. Printing wrong outputC ++中的模板..打印错误的输出
【发布时间】:2021-06-15 12:29:07
【问题描述】:

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

预期答案:

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();
}

【问题讨论】:

  • 您是否在调试器中单步调试过您的代码以了解其运行方式?
  • 是的,但我的输出是 0->
  • 要审查的代码很多,因此您至少应该能够弄清楚它为什么会卡住。
  • 在函数void add(T value1, U value2)中,为什么添加后将next设置为nullptr?在您的 find_value 函数中也是如此。另一个:在您的my_pair 构造函数中,值或混合(首选构造函数中的初始化列表,并避免 func 参数和类 var 的名称相同)。
  • 想知道我们是否可以提前关闭函数(在//Your code starts here//Your code ends here 内部)以创建其他函数以遵守 5 规则;-)

标签: c++ visual-c++ c++14


【解决方案1】:

注意几点:

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

  2. 由于主代码在同一个(第一个)节点上调用add,而不是新添加的节点,当前的add 例程(如果您删除nullptr 分配)只会以2 个节点结束在您的列表中。您必须找到一种方法来遍历列表并找到最后一个节点,然后添加到那个节点。

  3. 您的find_valueN 陷入无限循环。只要current 存在,它就会一直重复,但你没有任何改变current。您确实有一条线会更改current-&gt;next,但这对您没有帮助。 (事实上​​,这会破坏您正在查看的数据。当您只应该查看数据时,请小心修改数据。)您应该做的是更改该行以使 current 变为current-&gt;next 中保存的值。不要更改 current 中的任何内容。以print 函数底部为例。

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

【讨论】:

  • 3. const node* curr = this 可能会有所帮助。遗憾的是,不应修改的周围代码有一些“错误”。
  • 我同意。我本来打算提到 const-ness,但我认为这是课程的一部分,并且 const-ness 还没有被引入。
猜你喜欢
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 2016-10-19
  • 1970-01-01
相关资源
最近更新 更多