CAtlMapでユーザー定義型
以下、CAtlMapでキーにユーザ定義型を使用する方法。
Traitsの場合。
Traitsを使用しない場合は、DWORD()をオーバーロードしてハッシュ関数を定義する。
#include <atlcoll.h> class Node { public: long nid; float xyz[3]; Node() : nid(0) {} Node(long nid) : nid(nid) {} // compare operator BOOL operator==(const Node& in) const { if (nid == in.nid) return TRUE; return FALSE; } /* // operator to return random DWORD value for HashKey() operator DWORD() const throw(){ return (DWORD)(this->nid); } // copy Node& operator=(const Node& in) { nid = in.nid; xyz[0]=in.xyz[0]; xyz[1]=in.xyz[1]; xyz[2]=in.xyz[2]; return *this; } */ }; struct NodeTraits : public ATL::CElementTraits<Node> { static ULONG Hash( const Node& t ) { return t.nid; } static bool CompareElements( const Node& e1, const Node& e2 ) { return e1.nid==e2.nid; } }; int main() { CAtlMap<Node,float,NodeTraits> m; //m.InitHashTable(10); m[Node(1)] = 10.0; m[Node(2)] = 20.0; m[Node(3)] = 30.0; m[Node(4)] = 40.0; m[Node(3)] = 50.0; printf("v=%f\n", m[Node(2)]); printf("v=%f\n", m[Node(3)]); }