I need to get this sorting class to be compatable with the main. The constraint is that I cannot change the main! I can only change the code present in the sorting class. I have tried many different things, including changing around what is templated, templating the class, have a different constructor that calls a void quickSort, adding a destructor etc. I simply cannot figure this out. Please help.
Again, I can't change the main.cpp code at all.
Thank you, and the code and screen shot of errors is posted below.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class quickSort{
private:
template <typename Comparable>
void insertionSort(vector<Comparable>& a, int left, int right, bool reverse){
for(int p=1;p<a.size();++p)
{
Comparable tmp=move(a[p]);
int j;
for(j=p;j>0 and tmp<a[j-1];--j)
a[j]=move(a[j-1]);
a[j]=move(tmp);
}
}
template <typename Comparable>
const Comparable & median3(vector<Comparable>& a, int left, int right, bool reverse)
{
int center=(left+right)/2;
if(a[center]<a[left]){
swap(a[left],a[center]);
}
if(a[right]<a[left]){
swap(a[left],a[right]);
}
if(a[right]<a[center]){
swap(a[center],a[right]);
}
swap(a[center],a[right-1]);
return a[right-1];
}
template <typename Comparable>
void privquickSort(vector<Comparable>& a, int left, int right, bool reverse){
if(left+10<=right)
{
const Comparable & pivot = median3(a,left,right,reverse);
cout<<pivot<<"\n";
int i=left;
int j=right-1;
for(;;){
while(a[++i]<pivot){}
while(pivot<a[--j]){}
if(i<j)
swap(a[i],a[j]);
else
break;
}
swap(a[i],a[right-1]);
privquickSort(a,left,i-1,reverse);
privquickSort(a,i+1,right,reverse);
}
else
{
insertionSort(a,left,right,reverse);
}
}
public:
template <typename Comparable>
quickSort(vector<Comparable>& a, bool reverse=false){
privquickSort(a,0,a.size()-1,reverse);
}
};
# include "mySorting.hpp"
# include <iostream>
# include <vector>
# include <fstream>
# include <string>
void parseInstance(const std::string& line, std::vector<std::string> & res)
{
int p = 0, q = 0;
while(q < line.size())
{
if(line[q] ==',')
{
res.push_back(line.substr(p,q-p));
p=q+1;
}
q++;
}
}
int main(int argc, char* argv[])
{
std::ifstream fin;
std::vector<std::string> data;
fin.open(argv[1]); // open file
if (!fin.is_open()) // fail to open
{
std::cout << "Can't open the test instance file!\n";
return 0;
}
else
{
std::string line;
while(std::getline(fin, line))
{
parseInstance(line,data);
}
}
std::vector<int> data2;
for(auto x : data) data2.push_back(std::stoi(x));
for(auto x : data2) std::cout << x << " ";
std::cout << "\n";
quickSort(data2);
for(auto x : data2) std::cout << x << " ";
std::cout << "\n";
quickSort(data2, true);
for(auto x : data2) std::cout << x << " ";
std::cout << "\n";
for(auto x : data) std::cout << x << " ";
std::cout << "\n";
quickSort(data);
for(auto x : data) std::cout << x << " ";
std::cout << "\n";
quickSort(data, true);
for(auto x : data) std::cout << x << " ";
std::cout << "\n";
return 0;
}
Here is a screen shot of the errors that might be helpful:
Aucun commentaire:
Enregistrer un commentaire