class MySort
{
void bubble(std::vector<int> &vec, std::function<int(int,int)> func)
{ ... } //tihs works
void selectionSort(std::vector<int> &vec, std::function<int(int,int)> func)
{
int vecSize=vec.size();
int i, j, min_idx;
int temp;
for (i = 0; i < vecSize-1; i++)
{
min_idx = i;
for (j = i+1; j < vecSize; j++)
if(func(vec[j],vec[min_idx]) < 0)
min_idx = j;
temp = vec[min_idx];
vec[min_idx] = vec[i];
vec[i] = temp;
}
}
};
I have another MyData class :
class MyData
{
std::vector<int> t_;
int sortNormal2(int first, int sec)
{
return first > sec;
}
public :
MyData(std::vector<int> t)
{
std::copy(t.begin(),t.end(),back_inserter(t_));
}
void sortNormal(std::string choice)
{
MySort s;
auto sort= std::bind(&MyData::sortNormal2,this,std::placeholders::_1, std::placeholders::_2);
if(choice == "bubbleSort")
{
std::cout<<"BubbleSort : "<<std::endl;
s.bubble(t_, sort);
}
else
{
std::cout<<"SelectionSort : "<<std::endl;
s.selectionSort(t_, sort);
}
}
Above, i have a function which sort normal and has as a parameter a string.I have use bind and if the choise is bubbleSort it works ok ,but if the choice is another string, it doesn't work , it prints the vector table as normal, not sorted And main looks like this :
std::vector<int> table{ 244, 57, 78, 331, 9, 212, 6};
MyData data(table);
data.printVec();
std::cout<<std::endl;
data.sortNormal("bubbleSort"); //here if instead of buubleSort i type another string, it doesn't work
data.printVec();
Have anyone any idea why this algorithm is not ok? It does nothing and I cannot figure out what is wrong.
You test against being less than zero:
But your actual function can only return 0 or 1:
So the
if
branch will never be taken.