unordered_set requierments

What operators my classes should have for this container?

Ok.I added == operator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <unordered_set>
#include <limits>

class Foo
{
private:
	unsigned int data;
public:
	Foo() : data(0) {}
	explicit Foo(int d) : data(d) {}
	unsigned int get_data() const { return data; }
	bool operator == (const Foo& o) const
	{
		return data == o.data;
	}
};

typedef std::unordered_set<Foo> fset;

void print_set(const fset& s)
{
	const auto& end = s.end();
	auto it         = s.begin();
	for(; it != end; ++it)
	{
		std::cout << (*it).get_data() << std::endl;
	}
}

int main()
{
	fset sf;
	sf.insert(Foo(5));
	sf.insert(Foo(4));
	sf.insert(Foo(3));
	sf.insert(Foo(5));

	print_set(sf);

	std::cout << "Press enter to exit...";
	std::cin.ignore( std::numeric_limits< std::streamsize >::max(), '\n' );
	return EXIT_SUCCESS;
}


But VS2010 throws me this error and i can't see what/where is wrong thing?

error C2440: 'type cast' : cannot convert from 'const Foo' to 'size_t'
Last edited on
You have to provide a hash function.
1
2
3
4
5
6
7
8
9
struct HashFoo
{
	std::size_t operator()(const Foo &foo) const
	{
		// return hash of foo
	}
};

typedef std::unordered_set<Foo, HashFoo> fset;


Another way is to specialize std::hash:
1
2
3
4
5
6
7
8
9
10
11
namespace std 
{
	template <>
	struct hash<Foo>
	{
		std::size_t operator()(const Foo &foo) const
		{
			// return hash of foo
		}
	};
}


Or you can
Topic archived. No new replies allowed.