CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2007
    Posts
    90

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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: hash_set visual C compile error

    Use basic_string::c_str method to returb the char* pointer.
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: hash_set visual C compile error

    You forgot to :

    Code:
    #include <string>

  4. #4
    Join Date
    Apr 2007
    Posts
    90

    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
    	}

  5. #5
    Join Date
    Apr 2007
    Posts
    90

    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

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    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.

  7. #7
    Join Date
    Apr 2007
    Posts
    90

    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
  •  





Click Here to Expand Forum to Full Width

Featured