-9

How to use parallel_sort with this struct:

struct d
{
    char name[5];
};

struct less_than_key
{
    inline bool operator() (const d& struct1, const d& struct2) const
    {
        return strcmp(struct1.name, struct2.name) == -1;
    }
};

vector<d> my_d;
my_d.resize(3);

strcpy(my_d[0].name, "mike");
strcpy(my_d[1].name, "joe");
strcpy(my_d[2].name, "anna");

parallel_sort(my_d.begin(), my_d.end(), less_than_key());

DONE!, I hope this helps someone else.

Guess it's time to take my Phd in sorting algorithms!

SkyRipper
  • 155
  • 5
  • 15

1 Answers1

2

Don't use C-style strings when you can easily use std::string.

Since your struct only contains a string you may want to just use std::string instead (which implements the operators that you need for parallel_sort to work).

std::vector<std::string> vec(3);
vec.emplace_back("mike");
vec.emplace_back("joe");
vec.emplace_back("anna");

and then, following the function signature, you just need to find the begin and end iterator:

parallel_sort(std::begin(vec), std::end(vec));
Shoe
  • 74,840
  • 36
  • 166
  • 272