VS2010 hash_map

#include <iostream> #include <vector> #include <cassert> #include <algorithm> #include <string> #include <hash_map> #include <hash_set> using namespace std; struct Coordinate { friend ostream& operator<<(ostream &out,const Coordinate &c); Coordinate(int xv,int yv):x(xv),y(yv){} int x,y; }; ostream& operator<<(ostream &out,const Coordinate &c) { out << c.x << ',' << c.y; return out; } struct Coordinate_cmp { enum { bucket_size=100, }; //Hash function size_t operator()(const Coordinate &c) const { return c.x+c.y; } //compare < bool operator()(const Coordinate &c1,const Coordinate &c2) const { if(c1.x < c2.x) return true; else if(c1.x > c2.x) return false; else { if(c1.y < c2.y) return true; else return false; } } }; int main() { hash_map<string,int> hm; hm.insert(make_pair("wang",21)); hm.insert(make_pair("gujun",20)); hm.insert(make_pair("xusi",19)); auto i=hm.find("wzang"); if(i!=hm.end()) cout << i->second << endl; for(auto i=hm.begin();i!=hm.end();i++) cout << i->first << '/t' << i->second << endl; // hash_map<Coordinate,string,Coordinate_cmp> pos; pos.insert(make_pair(Coordinate(1,2),"songjiang")); pos.insert(make_pair(Coordinate(2,2),"qingpu")); pos.insert(make_pair(Coordinate(5,4),"jiading")); pos.insert(make_pair(Coordinate(1,3),"fengxian")); pos.insert(make_pair(Coordinate(7,4),"chongming")); pos.insert(make_pair(Coordinate(2,9),"jiangsu")); //cout << pos.count(); cout << endl << pos.bucket_count() << endl; cout << pos.bucket(Coordinate(2,9)) << endl; for(auto j=pos.begin();j!=pos.end();j++) cout << j->first << '/t' << j->second << endl; system("pause"); return 0; }

 

MSDN解释:

hash_compare Class 

 

The template class describes an object that can be used by any of the hash associative containers — hash_map, hash_multimap, hash_set, or hash_multiset — as a default Traits parameter object to order and hash the elements they contain.

template<class Key, class Traits = less<Key> > class hash_compare { Traits comp; public: const size_t bucket_size = 4; const size_t min_buckets = 8; hash_compare( ); hash_compare( Traits pred ); size_t operator( )( const Key& Key ) const; bool operator( )( const Key& _Key1, const Key& _Key2 ) const; };

Remarks

Each hash associative container stores a hash traits object of type Traits (a template parameter). You can derive a class from a specialization of hash_compare to selectively override certain functions and objects, or you can supply your own version of this class if you meet certain minimum requirements. Specifically, for an object hash_comp of type hash_compare<Key, Traits>, the following behavior is required by the above containers:

  • For all values _Key of type Key, the call hash_comp(_Key) serves as a hash function, which yields a distribution of values of type size_t. The function supplied by hash_compare returns _Key.

  • For any value _Key1 of type Key that precedes _Key2 in the sequence and has the same hash value (value returned by the hash function), hash_comp(_Key2, _Key1) is false. The function must impose a total ordering on values of type Key. The function supplied by hash_compare returns comp(_Key2, _Key1), where comp is a stored object of type Traits that you can specify when you construct the object hash_comp. For the default Traits parameter type less<Key>, sort keys never decrease in value.

  • The integer constant bucket_size specifies the mean number of elements per "bucket" (hash-table entry) that the container should try not to exceed. It must be greater than zero. The value supplied by hash_compare is 4.

  • The integer constant min_buckets specifies the minimum number of buckets to maintain in the hash table. It must be a power of two and greater than zero. The value supplied by hash_compare is 8.

In Visual C++ .NET 2003, members of the <hash_map> and <hash_set> header files are no longer in the std namespace, but rather have been moved into the stdext namespace. See The stdext Namespace for more information.

你可能感兴趣的:(object,function,Class,2010,pair,containers)