stereomatching 17 Posting Whiz in Training

i am curious about the unordered map of boost

this is the first code I wrote

typedef pair<int, int> axis;
typedef boost::unordered_map<axis, int> mCost;

void testUnorderedMap()
{
mCost mc;
axis p(0, 0);
mc.insert(mCost::value_type(p, 3));
p.first = 0; p.second = 6;
mc.insert(mCost::value_type(p, 33));
p.first = 0; p.second = 1;
mc.insert(mCost::value_type(p, 77));
p.first = 0; p.second = 2;
mc.insert(mCost::value_type(p, 33));

for(mCost::const_iterator iter = mc.begin(); iter != mc.end(); ++iter)
cout << "(" << iter->first.first << "," << (iter->first).second << ") = " <<iter->second<<endl;
}

as you can see, i didn't use the hash_value fucntion in the
fitst code

this is the second code I wrote

class point { int x; int y;

public: point() : x(0), y(0) {} point(int _x, int _y) : x(_x), y(_y){}

bool operator==(point const& other) const { return x == other.x && y == other.y; }

friend std::size_t hash_value(point const& p) { std::size_t seed = 0; boost::hash_combine(seed, p.x); boost::hash_combine(seed, p.y);

return seed; } };

void testHashTable() { //boost::hash<point> point_hasher;

point p1(0, 3); point p2(1, 2); point p3(4, 1);

typedef boost::unordered_map<point, int > um; um Points;

Points.insert(make_pair(p1, 33)); Points.insert(make_pair(p2, 34)); Points.insert(make_pair(p3, 99));

for(um::const_iterator iter = Points.begin(); iter != Points.end(); ++iter) cout << iter->second <<endl; } [\code]

I have two questions about it 1:which one is better? I only give the key to the first code but without hash_value, would collision happen? if it really happen, how would unordered_map handle it?

2:how could I obtain the data of p1, p2 or p3 of the second code? I had tried to write some member function and wrote something like "iter->first.getX()", but it wouldn't work totally how could I fix this problem?

thanks a lot[code = second]
class point
{
int x;
int y;

public:
point() : x(0), y(0) {}
point(int _x, int _y) : x(_x), y(_y){}

bool operator==(point const& other) const
{
return x == other.x && y == other.y;
}

friend std::size_t hash_value(point const& p)
{
std::size_t seed = 0;
boost::hash_combine(seed, p.x);
boost::hash_combine(seed, p.y);

return seed;
}
};

void testHashTable()
{
//boost::hash<point> point_hasher;

point p1(0, 3);
point p2(1, 2);
point p3(4, 1);

typedef boost::unordered_map<point, int > um;
um Points;

Points.insert(make_pair(p1, 33));
Points.insert(make_pair(p2, 34));
Points.insert(make_pair(p3, 99));

for(um::const_iterator iter = Points.begin(); iter != Points.end(); ++iter)
cout << iter->second <<endl;
}
[\code]

I have two questions about it
1:which one is better?
I only give the key to the first code but without hash_value, would collision happen?
if it really happen, how would unordered_map handle it?

2:how could I obtain the data of p1, p2 or p3 of the second code?
I had tried to write some member function and wrote something like
"iter->first.getX()", but it wouldn't work totally
how could I fix this problem?

thanks a lot