Trying to get my treesort function to execute but I'm getting a segmentation fault. Any help would be appreciated.
Assignment
Implement the TREESORT function after the following algorithm:
Given: * a vector filled with N values of some type T; not in order;
* and empty set<T>
Step1: Iterate over the vector and insert each vector element into
the Set;
Step2: Iterate over the set, and copy the first set element into
[0] of vector, the second set element into [1] of vector,
etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#include <vector>
#include <iostream>
#include <set>
using namespace std;
template <class T>
void treesort(vector<T> & data)
{
// declare a search tree of the correct type
multiset<T> sorter;
typename vector<T>::iterator itr, stop;
stop=data.end();
// copy the entire vector into the tree
for (itr=data.begin(); itr != stop; ++itr)
sorter.insert(*itr);
// now copy the values back into the array
typename multiset<T>::iterator tree;
for (itr = data.begin(); itr != stop; ++itr)
*itr = *tree++;
}
int main() {
// Get size of vector
int N;
cout << "Enter size for the vector: ";
cin >> N;
// Create vector of size N
vector<int> v(N);
// Fill vector with user input for elements
cout << "Fill vector with the following elements: ";
for (int i=0;i<N;i++)
cin >> v[i];
// Print out vector
for (int i=0;i<N;i++)
cout << v[i] << " ";
treesort(v);
for (int i=0;i<N;i++)
cout << v[i] << " ";
return 0;
}
|
I used gdb to get more info on the seg fault and got this:
Program received signal SIGSEGV, segmentation fault.
0x00007ffff7b4acf0 in std::_Rb_tree_increment(std::_Rb_tree_node_base const*)
() from /usr/lib.../libstdc++.so.6