sort hash_set in alphabetical order

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eagerlearner
    New Member
    • Jul 2007
    • 29

    sort hash_set in alphabetical order

    I have the following code, which does not sort the list in alphabetical order, how can I sort everything in alphabetical order ? Before that, I want to ask, when an insertion occur, does it compare the string that I am inserting with every string inside the set list ? or just with string that is beside the location that is going to be inserted ? Thank you.

    [code]
    Code:
    #include <iostream>
    #include <hash_set>
    #include <string>
    using namespace std;
    
    class strHashCompare : public stdext::hash_compare<string>
    {
    public:
    	size_t operator() (const string& s) const
    	{
    		size_t h = 0;
    		size_t max, i;
    		for(i = 0, max = s.length(); i < max; i++)
    		{
    			h = 31 * h + s[i];
    		}
    		return h;
    	}
    
    	bool operator() (const string& lhs, const string& rhs) const
    	{
    		return (lhs.compare(rhs) > 0);
    	}
    };
    
    typedef stdext::hash_set<string, strHashCompare> strHashSetTy;
    
    int main()
    {
    	strHashSetTy strHashSet;
    	strHashSet.insert(string("january"));
    	strHashSet.insert(string("february"));
    	strHashSet.insert(string("march"));
    	strHashSet.insert(string("april"));
    	strHashSet.insert(string("may"));
    	strHashSet.insert(string("june"));
    	strHashSet.insert(string("july"));
    	strHashSet.insert(string("august"));
    	strHashSet.insert(string("september"));
    	strHashSet.insert(string("october"));
    	strHashSet.insert(string("november"));
    	strHashSet.insert(string("december"));
    
    	strHashSetTy::const_iterator strConstIter;
    	for(strConstIter = strHashSet.begin(); strConstIter != strHashSet.end(); strConstIter++)
    	{
    		cout << *strConstIter << "\n";
    	}
    	return 0;
    }
  • ravenspoint
    New Member
    • Jul 2007
    • 111

    #2
    Originally posted by eagerlearner
    Before that, I want to ask, when an insertion occur, does it compare the string that I am inserting with every string inside the set list ? or just with string that is beside the location that is going to be inserted ?
    It calciulates the position where the new insertion should go. This involves the comparison with some, not all, the previously inserted. The details depend on the algorithm used. Then it comapares the new value with the one already "there". If they are different, it is placed beside the old value. If they are the same, the new one is rejected.

    Comment

    • eagerlearner
      New Member
      • Jul 2007
      • 29

      #3
      Thanks for the reply, it help clear a bit of my curiosity. I think I got to use the std::map instead of stdext::hash_ma p if I want to have the alphabetical order.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by eagerlearner
        Thanks for the reply, it help clear a bit of my curiosity. I think I got to use the std::map instead of stdext::hash_ma p if I want to have the alphabetical order.
        You can always sort your hash_map.

        Comment

        Working...