C++ Iterators and Linked Lists
So I'm trying to assign the values of one linked list to another, exterior linked list (not in the current method) using iterators.
LIST_ITER i = temp.begin();
while(bLeft != end)
{
*bLeft = *i;
++i;
++bLeft;
}
This is only a portion of the code, the iterator i is for the temp list, whereas bLeft and end are the beginning and end (respectively) of the exterior list.
This above code, however, is producing a strange error where I get a bunch of strange text (some of it actually says something about Microsoft Windows Compatible etc. etc.) that when run on a Unix machine just gives a Segmentation Fault.
EDIT: Here is the code in its entirety:
#include <iostream>
#include <list>
#include <string>
#include <iterator>
using namespace std;
typedef list<string> LIST; // linked list type
typedef LIST::size_type LIST_SIZE; // size type for list, e.g., unsigned
typedef LIST::iterator LIST_ITER; // iterator type
typedef LIST::value_type LIST_CONTAINS; // type in the list, i.e., a string
void merge_sort(LIST_ITER beg, LIST_ITER end, LIST_SIZE sz);
void merge(LIST_ITER bLeft, LIST_ITER bRight, LIST_ITER end);
int main()
{
LIST l;
LIST_CONTAINS v;
// Read in the data...
while (cin >> v)
l.push_back(v);
// Merge the data...
LIST_ITER i = l.begin();
LIST_ITER iEnd = l.end();
merge_sort(i, iEnd, v.size());
// Output everything...
for (; i != iEnd; ++i)
{
cout << *i << '\n';
}
system("pause");
}
void merge_sort(LIST_ITER beg, LIST_ITER end, LIST_SIZE sz)
{
if(sz < 2)
{
return;
}
else
{
LIST_SIZE halfsz = (distance(beg, end)/2); //half of list size
LIST_ITER i1End = beg; //iterator for the end of the first list
advance(i1End, halfsz); //advance to the midpoint
i2 = i1End++; //iterator for the beginning of the second list
--end;//iterator for the end of the second list
merge_sort(beg, i1End, halfsz); //recursively pass first list
merge_sort(i2, end, halfsz); //recursively pass second list
}
merge(beg, i2, end);
}
void merge(LIST_ITER bLe开发者_StackOverflowft, LIST_ITER bRight, LIST_ITER end)
{
LIST temp;
LIST_ITER beg = bLeft;
LIST_ITER halfw = bRight;
LIST_ITER i = temp.begin();
while(beg != bRight && halfw != end)
{
if(*beg < *halfw)
{
temp.push_back(*halfw);
halfw++;
}
else
{
temp.push_back(*beg);
beg++;
}
}
while(beg != bRight)
{
temp.push_back(*beg);
beg++;
}
while(halfw != end)
{
temp.push_back(*halfw);
halfw++;
}
while(bLeft != end) ///HERE IS THE PREVIOUSLY POSTED CODE
{
*bLeft = *i;
++i;
++bLeft;
}
}
Most likely cause is that the source list doesn't have enough elements in it. Without more information (or context), it's not possible to be more precise, though.
Shouldn't the loop test be:
while (bLeft != end && i != temp.end())
How do you know that i is bigger than the other container?
Why not use std::list's assign method? If the data in the two lists are of the same type, that really should be all you need, no?
Seems like what you are trying to accomplish could be accomplished with the assign
function.
exterior.assign(temp.begin(), temp.end());
This should assign the exterior list the values of the temp list from beginning to end.
If you're going to continue using the temporary list afterwards, use std::copy
. If you're not, using std::list.splice
.
I think I've discovered the error, it has to do with how I've incremented some of my iterators unnecessarily (such as decrementing the "end" unnecessarily) and my code still has errors but I need to go through it some more.
Thanks for the suggestions!
精彩评论