-
July 27th, 2007, 11:00 AM
#1
hash_set visual C compile error
Hello, I got compile error as stated in the comment line below using Visual C++, what's wrong with that cout ?
And I got one more issue to ask, about strcmp (not used below), what does it compare exactly ? I read in MSDN, how does a string be calculated into numeric values and the compared ? Thank you.
Code:
#include <iostream>
#include <hash_set>
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("c"));
strHashSet.insert(string("b"));
strHashSet.insert(string("a"));
strHashSetTy::const_iterator strConstIter;
for(strConstIter = strHashSet.begin(); strConstIter != strHashSet.end(); strConstIter++)
{
/*
error C2679: binary '<<' :
no operator found which takes a right-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>'
(or there is no acceptable conversion)
*/
cout << *strConstIter << "\n";
}
return 0;
}
-
July 27th, 2007, 11:34 AM
#2
Re: hash_set visual C compile error
Use basic_string::c_str method to returb the char* pointer.
Victor Nijegorodov
-
July 27th, 2007, 12:10 PM
#3
Re: hash_set visual C compile error
-
July 27th, 2007, 12:40 PM
#4
Re: hash_set visual C compile error
Thanks, to both of you, that is such a careless mistake. But I thought the compiler should complain since the first line of
Code:
strHashSet.insert(string("c"));
strHashSet.insert(string("b"));
strHashSet.insert(string("a"));
Does my string comparison guarantee that I always get the alphabetical list order of set ? Thank you.
Code:
bool operator() (const string& lhs, const string& rhs) const
{
return (lhs.compare(rhs) < 0); // I have amend to be less than 0, so I get alphabetical order
}
-
July 27th, 2007, 12:55 PM
#5
Re: hash_set visual C compile error
I have test out with
Code:
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"));
The result is it does not sort everything in alphabetical order, anyone have the idea ? Thanks
-
July 27th, 2007, 01:00 PM
#6
Re: hash_set visual C compile error
I don't think that a hash-container would sort alphabetically.
I'm not familiar with hash_set, so I don't know exactly why
it needs a comparison operator. I would think that it would
store the elements into buckets based on the hash-function.
At any rate, if you do want them alphabetically, use std::set
instead.
edit: from the hash_set documentation:
The actual order of elements in the controlled sequence depends on the hash function, the ordering function, and the current size of the hash table stored in the container object. You cannot determine the current size of the hash table, so you cannot in general predict the order of elements in the controlled sequence.
Last edited by Philip Nicoletti; July 27th, 2007 at 01:02 PM.
-
July 27th, 2007, 04:51 PM
#7
Re: hash_set visual C compile error
Thanks, you are right, because of the hash_function, it makes my hash_set list unordered. Got to use the std::map then. 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|