0

I am referring you to a previous link that compares the performance of qsort vs stdsort.

I have written a C program that populates a large std::map and I want to sort the array.I am currently using qsort.

typedef std::map<uint16_t, uint32_t> TSrcMap;
TPSrcMap sp;
TSrcMap::iterator its;
/*Code to populate the array_start.*/

/*Code to populate the array_end.*/

typedef struct port_count
{
        uint32_t port_number;
        uint32_t port_count;
}port_count_t;

port_count_t pcount[10];
memset(pcount,0,sizeof(pcount));
size_t structs_len = sizeof(pcount)/sizeof(port_count_t);
for(its = stcp.begin(); its != stcp.end();its++)
{
      if(pcount[smallest_index].port_count < (*its).second)
      {
            pcount[smallest_index].port_count = (*its).second;
            pcount[smallest_index].port_number = (*its).first;
            /*qsort(pcount, structs_len, sizeof(port_count_t), struct_cmp_by_port_count);*/
            std::sort(pcount,sizeof(port_count_t));
      }
}

The qsort function is correctly sorting the array. I want to compare the performance of qsort with std::sort but the call std::sort call is giving a compile error

no matching function for call to ‘sort(port_count_t [10], long unsigned int)’

I would like to compare the performance of std::sort with qsort algorithm. How do I do that?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
liv2hak
  • 14,472
  • 53
  • 157
  • 270

2 Answers2

6

The signature of std::sort() is:

template <class RandomAccessIterator, class StrictWeakOrdering>
void sort(RandomAccessIterator first, RandomAccessIterator last,
          StrictWeakOrdering comp);

So it requires two iterators and a comparator, not a pointer and a length. To fix your code, call

std::sort(pcount, pcount+structs_len, cmp_port_count);

assuming that cmp_port_count is your comparator function which takes two port_count_t objects by reference and returns true when the first argument is to be ordered before the second argument, false otherwise.

Michael Wild
  • 24,977
  • 3
  • 43
  • 43
0

Try calling:

std::sort(pcount,pcount + 10);

std::sort takes as arguments a begin and an end iterator. So in order to sort an array you pass a pointer to the beginning of the array and a pointer to one element after the end of the array(which is always array_pointer + array_size).

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176