I'm trying to sort integers in a vector using a recursive function. My program runs, compiles, and sorts the data, but after the fact it gives me a seg fault. I think its because of the for loop using the addresses in the vector that have been changed causing it to never leave the loop.
void Plays::SortRelevance(vector<Plays> list){
cout << "in the loop" << endl;
for(vector<Plays>::iterator i=list.begin(); i != list.end(); ++i){
cout << i->relevance << endl;
}
for(vector<Plays>::iterator i=list.begin(); i != list.end(); ++i){
if(i->relevance < (i+1)->relevance){
cout << "IN THE THINGY WAT" << endl;
Plays temp(*i);
list.erase (i);
list.push_back (temp);
SortRelevance(list);
cout << "left recursive" << endl;
}
cout << "stuck?" << endl;
}
cout << "left the loop" << endl;
for(vector<Plays>::iterator i=list.begin(); i != list.end(); ++i){
cout << i->relevance << endl;
}
}
The end of my output is as follows, sorted but giving a seg fault at the end:
IN THE THINGY WAT
in the loop
-62
-62
-62
-69
-71
-72
-80
-81
-87
-89
-94
-100
-104
-107
-107
-112
-137
-142
-145
-150
-151
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
stuck?
Segmentation fault
Thanks in advance to anyone who can shed some light on this for me.
EDIT: I haven't so much as fixed the problem as I have found a better and more elegant way to do it. I created a class, overloaded the operators and then used the sort() function to sort what I needed to do.