简体   繁体   English

两个排序列表的交集和并集(返回类型)

[英]Intersection and Union of two sorted lists (return type)

This code is for finding the intersection and union of two sorted lists. 此代码用于查找两个排序列表的交集和并集。 The sorted list in inherited from a list class with all the basic functions. 从具有所有基本功能的列表类继承的排序列表。 The main question is what is the return type of the function. 主要问题是该函数的返回类型是什么。 Is it a pointer to a list or the list itself? 它是指向列表还是列表本身的指针? How would i display the contents of that "pointer". 我将如何显示该“指针”的内容。

template <typename Object>
class sorted_list : public List<Object>{
    friend sorted_list<Object>*& slUnion( const sorted_list<Object>& list1, const sorted_list<Object> & list2){
        auto i=list1.begin();
        auto j=list2.begin();
        sorted_list<Object> un;
        static sorted_list<Object>* newlist=&un;
        while(i!=list1.end() && j!=list2.end()){
            if(*i<*j){
                un.push_back(*i);
                i++;
            }
            else if(*i>*j){
                un.push_back(*j);
                j++;
            }
            else{ //if equal
                un.push_back(*i);
                i++; j++;
            }
        }
        while(i!=list1.end())
            un.push_back(*i++);
        while(j!=list2.end())
            un.push_back(*j++);
        return newlist;
    }
};

When the program runs, the "un" in main points to NULL. 程序运行时,main中的“ un”指向NULL。

int main(){
    sorted_list<int> l1;
    int i=1;
    while(i<10){
        l1.push_back(i++);
    }
    sorted_list<int>l2;
    int j=1;
    while(j<10){
        l2.push_back(j);
        j+=2;
    }
    sorted_list<int> *un = slUnion(l1,l2);
}

You should typically return by value, ie sorted_list<Object> . 您通常应按值返回,即sorted_list<Object> Newer versions of cpp guarantee you that they will not actually make a copy. 较新版本的cpp保证您实际上不会进行复制。

What you are doing right now is wrong, because it has undefined behavior. 您现在正在做的事情是错误的,因为它具有未定义的行为。 You are using un , which is on the function stack, and return a pointer to it. 您正在使用函数堆栈上的un ,并返回指向它的指针。 By the time the function returns un has gone out of scope and the memory location can have been reused. 到函数返回时, un的作用域已超出范围,并且内存位置可以被重用。 Just completely remove the newlist pointer and return un instead. 只需完全删除newlist指针,然后返回un

You also seem to be confused about classes, methods and functions. 您似乎也对类,方法和函数感到困惑。 As it is, your method does not have to be inside a class, or, since it does not seem to use class state, it can be static, if inside a class. 照原样,您的方法不必在类内部,或者,因为它似乎不使用类状态,所以如果在类内部,则它可以是静态的。 It also does not seem like it would have to be a friend. 似乎也不必成为朋友。 If you wanted to write this as a member-function, it would look sth like this: 如果您想将其编写为成员函数,则看起来像这样:

sorted_list<Object>& unionWith(const sorted_list<Object>& rhs) {
  // merge this and rhs w deduplication into temp, then swap temp with this
  ...

  return *this;
}

I think what is likely your problem, is that you don't assign &un to newlist , but that you initialize newlist with &un . 我认为您可能遇到的问题是,您没有将&un分配给newlist ,而是使用&un初始化了newlist Initialization is only performed once for a function-static variable, so future iterations of your method just skip that line and the pointer points to where the original version of un was. 初始化仅对函数静态变量执行一次,因此方法的未来迭代仅跳过该行,并且指针指向un的原始版本所在的位置。 Try putting the assignment on a different line. 尝试将作业放在另一行。 That should fix your immediate issue, but the solution with the static pointer is still really bad, because the pointer is shared by all instances. 那应该可以解决您眼前的问题,但是使用静态指针的解决方案仍然非常糟糕,因为所有实例都共享该指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM