CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2010
    Posts
    9

    Sorting hashes...

    Is there an easy way to sort a hash by its values?

    What I'm doing is building a dictionary of words from a text file, and my wordStruct looks like so (including hash map declaration in main):

    Code:
    struct wordStruct
    {
    	string word;
    	int wordCount;
    	int docCount;
    	vector<int> docs;
    };
    
    hash_map<string, wordStruct> dict;
    I can populate the hash map just fine, what my problem is is efficiency (most likely with sorting)... right now, I convert this hash map into a vector (which is really bad since I'm mirroring the hash map, thus doubling how much memory I'm using), then Merge Sort the vector and use that as a way to print by my wordCount, but it takes quite a while for a text file of size 600 kb (and I would definitely like to go higher...), so is there a way to sort this hash more easily? Any advice would be appreciated!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Sorting hashes...

    You can store the iterators in a vector, and then sort the iterators according to the wordCount members of what they refer to.

    By the way, the TR1 extensions to the standard library include unordered_map, which will become part of the C++ standard library, so you may wish to use that instead of hash_map.
    Last edited by laserlight; September 17th, 2010 at 12:53 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Sep 2010
    Posts
    9

    Re: Sorting hashes...

    Ahhhh, I'll trying storing the iterators, good idea!

    For some reason, my tr1 doesn't have unordered_map... if I do "std::tr1::" I have no words that begin with u... so I guess I don't have VS 2008's updated libs? I actually had to download a "feature pack" to get my hash_maps working, but still no unordered_map. It's really odd.

  4. #4
    Join Date
    Sep 2010
    Posts
    9

    Re: Sorting hashes...

    BUMP. Any advice on getting unordered_map to work?

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Sorting hashes...

    Well, you can always use the unordered_map from Boost if necessary.

  6. #6
    Join Date
    Sep 2010
    Posts
    9

    Re: Sorting hashes...

    Ahhh, forgot Boost had its own unordered_map.

    I changed the hash_map to store the iterators in the vector--no change. Still took the usual 4 minutes, which is terrible, haha.

    Then I changed to unordered_map, without storing the iterators in the vector--no change. Still 4 minutes.

    Then I used unordered_map and its iterators... 12 seconds.

    Nice job all. Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured