{ private: int data; ListOfElements * next; //指向 列表中下一个元素的指针 public: ListOfElements(); ~ListOfElements (); addNewElement(int new_data); } ListOfElements * head = NULL;指向第一个元素的指针 我现在如何实现反向?逆转的算法不是问题,我不知道如何处理常量引用和 返回一个对象... 常量引用意味着你可以将参数视为实例,你不能改变它(或调用非常量的方法)我不认为的课程。 我看到的问题是返回列表的头部必须是 在堆栈上分配,其余的节点分配在 堆上,如果析构函数调用delete,那么分配就是 不可能。按值返回将调用默认的副本 构造函数,因此两个列表头将指向 元素的相同尾部列表,然后当本地副本退出时范围它将摧毁 所有。 Hi guys,I''m dealing with, probably, easy problem, however I''m not sure what todo and whether my way of thinking is correct..The task is to implement a list of object of a class ListOfElementsand then implement a function that will reverse the order of theelements on the list, but the function should look like this: ListOfElements reverse (const ListOfElements&) As far as I understand I should create a list of these objects, whichI know how to do. I have difficulties with the function reverse. Whatdoes it realy mean that as parameter it takes ListOfElements& ? Isthis a reference to the first object, ie. a pointer to the firstobject? say we have the following: class ListOfElements{private:int data;ListOfElements * next; //pointer to the next element on thelistpublic:ListOfElements();~ListOfElements();addNewElement (int new_data);} ListOfElements * head = NULL; pointer to the first element How can I now implement reverse? The algorithm of the reversing is notthe problem, I don''t know what to do with the constant reference andreturning of an object... 解决方案 1337-chixor;) wrote:Hi guys,I''m dealing with, probably, easy problem, however I''m not sure what todo and whether my way of thinking is correct..The task is to implement a list of object of a class ListOfElementsand then implement a function that will reverse the order of theelements on the list, but the function should look like this:ListOfElements reverse (const ListOfElements&) Why not use a sorted container (std:map or std::set) and use a reverseiterator to iterate through them, or copy them to another container? --Ian Collins."1337-chixor;)" <ne*********@gmail.comwrote in messagenews:11**********************@o5g2000hsb.googlegro ups.com...Hi guys,I''m dealing with, probably, easy problem, however I''m not sure what todo and whether my way of thinking is correct..The task is to implement a list of object of a class ListOfElementsand then implement a function that will reverse the order of theelements on the list, but the function should look like this:ListOfElements reverse (const ListOfElements&)Your parameter is a reference to a ListOfElements which is constant. Whichmeans you are not to change the passed in list. You''ll need to then createa copy. It sounds to me like an easy way would simply be to create a ListOfObjectsinside this function. Then start inserting from the passed in listbackwards. I.E. Start reading from the back and insert to the end. Thenreturn the ListOfObjects you created. There is most likely a standard algorithm you can use. Copy or something.I don''t use them yet, so I would just use a for loop. This question sounds suspiciously like homework (not sure if it is or not)so I''m reluctant to show code. As far as I understand I should create a list of these objects, whichI know how to do. I have difficulties with the function reverse. Whatdoes it realy mean that as parameter it takes ListOfElements& ? Isthis a reference to the first object, ie. a pointer to the firstobject?say we have the following:class ListOfElements{ private: int data; ListOfElements * next; //pointer to the next element on thelist public: ListOfElements(); ~ListOfElements(); addNewElement (int new_data);}ListOfElements * head = NULL; pointer to the first elementHow can I now implement reverse? The algorithm of the reversing is notthe problem, I don''t know what to do with the constant reference andreturning of an object...The constant refernece means you can treat the paramter as the instance, youjust can''t change it (or call non-const methods of the class I don''t think). The problem I see is that the head of the returned list must beallocated on the stack, with the rest of the nodes allocated on theheap, and if the destructor calls delete next then the assignment isimpossible. The return by value will invoke the default copyconstructor, so both list heads will point to the same tail list ofelements, and then as the local copy goes out of scope it will destroythem all. 这篇关于课程和缺点参考...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-30 08:59