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]
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;
}
Comment