list is blank list

Hi all,

I met a strange issue, I want to get the union of two list , and my code is below: but I always get the blank returned list,
#include <list>
#include <iostream>
#undef _STD
#define _STD ::std::

template <class Object> _STD list<Object> Union(_STD list<Object> const& list1, _STD list<Object> const& list2)
{
_STD list<Object> list;
_STD list<Object>::const_iterator iter = list1.begin();
_STD list<Object>::const_iterator it = list2.begin();
if ( list1.size() == 0 && list2.size() == 0)
{
return list;
}
if ( list1.size() == 0 && list2.size() != 0)
{
while (it != list2.end())
{
list.push_back(*it);
}
return list;
}
if (list2.size() == 0 && list1.size() != 0)
{
while ( iter != list1.end())
{
list.push_back(*iter);
}
return list;
}

while ( 1> 0)
{
if ( iter != list1.end() && it != list2.end() &&*iter < *it )
{
list.push_back(*iter);
++iter;
}
else if ( iter != list1.end() && it != list2.end() &&*iter > *it )
{
list.push_back(*it);
++it;
}
else if (iter != list1.end() && it != list2.end() && *iter == *it )
{
list.push_back(*iter);
++iter;
++it;
}
else if( iter == list1.end() && it != list2.end())
{
list.push_back(*it);
++it;
}
else if ( it == list2.end() && iter != list1.end())
{
list.push_back(*iter);
++iter;
}
else
{
//return list; then the returned list is a blank list
break; //if I use the code , then I can get the right list
}

}
return list;

}
Works for me, after fixing the compilation errors (missing typenames). But this seems to be a re-implementation of std::set_union, why not use that directly?

1
2
3
4
5
6
7
8
9
template <class Object>
std::list<Object> Union(std::list<Object> const& list1,
                        std::list<Object> const& list2)
{
    std::list<Object> list;
    std::set_union(list1.begin(), list1.end(), list2.begin(), list2.end(),
                   back_inserter(list));
    return list;
}
By looking at it, it's not obvious why it doesn't work. I do have a few comments.

As the third block deals with empty lists, you don't need the first two loops at the top that deal with empty lists.

Do you really need that _STD stuff?

Have you tried inserting trace statements to diagnose where it's going wrong?

Please use the code format tags to format your code.
@Cubbi,

Thank you reminder , there is a std function named set_union which is similiar to my function named Union .

@kbw,
I got , the reason why list is blank is that the dtor function will work once complier meets return code. as a result , the list is blank , but in fact ,the returned list is not blank, I use "_STD list<int> list4 = Union(list3,list2);" to verify , the list4 is not blank .

as for , _STD stuff, I have read some source code in std namespace ,I find there are a lot of _STD, so I learn to use it .

at last , I will re-arrange the code format tags .
Thank you all.
@Cubbi,
in fact, I am doing my homework , the question is 3.5 in charpter 3(list, stack and queue) in "Data Structures and Algorithm Analysis in C++" :)
Topic archived. No new replies allowed.