0
class hash
{
private:

    static const int tableSize = 1000;
    struct item
    {
        string name;
        unsigned int reps;
        item* next;
    };
    item* HashTable[tableSize];
}

In my program I constructed my hashtable with this structure, and I am using chaining. These items holds very similar strings, and an integer variable with them, storing how many times they occur in a text file. I want to write a function to print most frequent K strings in the context.

void PrintTopFrequentList(int K);

My resulting hash table is actually very big,maybe 100.000 entries? What I have got so far is this...

void hash::PrintTopFrequentList(int length)
{
    vector<item*> row;
    vector<item*> sorted;
    int no;
    for ( int i = 0 ; i<tableSize ; i++)
    {
        item* Ptr= HashTable[i];

        while ( Ptr->next!=NULL )
        {
            row.push_back(Ptr);
            Ptr=Ptr->next;
        }
        sort(row.begin(),row.end());
        sorted.insert(sorted.end(), row.begin(),row.end());
        sort(sorted.begin(),sorted.end());
    }

My problems are, first of all those sort functions at the end of PrintTopFreq. Function does not seem to work, after the for loop I add this part to control sorting, but it didn't work.

What I actually need to do is to customize this sort function such that my result vector will be in Descending order. I tried some function overloading inside class definition I looked from other posts but couldn't make it work.
I tried reading this post but it didn't help.

Community
  • 1
  • 1
methodp
  • 19
  • 2
  • uhm, maybe you could simply sort on reverse iterators? `sort(row.rbegin(), row.rend())` ? – Tomasz Lewowski Oct 22 '16 at 15:59
  • 2
    only question I can spot is "...100.000 entries?" how are we supposed to know? :P Please describe what brings you to the conclusion that it doesnt work, because "doesnt work" can mean anything – 463035818_is_not_an_ai Oct 22 '16 at 15:59
  • sort(row.rbegin(), row.rend()) did reversing the list Im printing to control, but still I do not have a sorted list it went from 11114111211111 to 111112111411111 for example – methodp Oct 22 '16 at 16:04
  • @methodp Put additional information into your question please, as comment it's not useful. – πάντα ῥεῖ Oct 22 '16 at 16:07
  • *My resulting hash table is actually very big,maybe 100.000 entries* -- Honestly, that's not "very big". – PaulMcKenzie Oct 22 '16 at 16:28

1 Answers1

0

To sort something the sort algorithm needs to know how to compare items std::sort. Otherwise it will use the default comparison for int. Either you implement comparison operators (<, >, ==) for item (or implement a comparison class for your item).

truschival
  • 303
  • 1
  • 2
  • 7