tags:

views:

106

answers:

3

I am seeing a code where in the program it is creating a hash_map:

   // Create a hash_map hm3 with the 
   // allocator of hash_map hm1
   hash_map <MyStr, MyInt>::allocator_type hm1_Alloc;
   hm1_Alloc = hm1.get_allocator( );
   hash_map <MyStr, MyInt, hash_compare <MyStr, less_str > > hm3( hash_compare <MyStr, less_str > (), hm1_Alloc );
   hm3.insert( Int_Pair( "three", 30 ) );

Could anyone please explain me the 3rd statement where hm3 is declared.

hash_map <MyStr, MyInt, hash_compare <MyStr, less_str > > hm3( hash_compare <MyStr, less_str > (), hm1_Alloc );

The complete program can be found here

A: 

We create object called hm3. It's class is hash_map <MyStr, MyInt, hash_compare <MyStr, less_str > >. It (class) is a template class hash_map and this template takes two parameters - two class names. First one is MyStr. Second one is template function hash_compare <MyStr, less_str >. This (second) template takes also two parametrs. They are MyStr class and something called less_str.

Why such template? I suppose first parametr of hash is the container of element. The second one is the function for comparison such containers.

Add: And about constructor: it takes result of template function smt hash_compare <MyStr, less_str > (void) and some kind of object.

Add2: It can be shown like this:

typedef hash_map <MyStr, MyInt, hash_compare <MyStr, less_str > > Someclass;
Someotherclass var = hash_compare <MyStr,    less_str > (); // `var` is what this function returned

Someclass hm3( var, hm1_Alloc );
MInner
this part is easy to understand but what about the latter part `hm3( hash_compare <MyStr, less_str > (), hm1_Alloc )`
Vijay Sarathi
It's constructor parametrs. `hash_compare <MyStr, less_str > ()` is a template function with two template parameters and zero function parameters, and it returns something. That's what it returns is first parameter of constructor. See my answer edit for more.
MInner
+1  A: 
hash_map <MyStr, MyInt, hash_compare <MyStr, less_str > >

This is a type, being a hash map which maps a MyStr to a MyInt, using a custom hash compare functor type. Let's call it HashMap.

hash_compare <MyStr, less_str > ()

The syntax T() creates a temporary object of type T using the default constructor. The code above constructs the hash compare functor. Let's call this object hashCmp.

hm1_Alloc

This is a custom allocator.

That declaration can then be rewritten as

typedef hash_compare<MyStr, less_str>     HashCmpT;
typedef hash_map<MyStr, MyInt, HashCmpT>  HashMap;

HashCmpT hashCmp;

HashMap hm3 (hashCmp, hm1_Alloc);
KennyTM
... `(HashCmp(), hm` .. probably?
MInner
@MInner: Why? [ ](http://.com)
KennyTM
Hm. It dependes on what `hash_compare` is. If it's a template function - we should have (). But if that's really produces functor, you are write. I haven't read MSDN article, so I guessed what `hash_compare` could be, so I suppose you re write.
MInner
I have never seen such `hash_compare <MyStr, less_str > ()` creation of object.
MInner
@MInner: using `T()` to create an object of T with the default constructor is pretty common in STL, [e.g. `std::sort(l.begin(), l.end(), std::greater<int>() );`](http://www.sgi.com/tech/stl/greater.html).
KennyTM
@KennyTM: Is it same as `new T()`? Or it works only with functors?
MInner
@MInner: No it's not. `T()` creates a temporary object of type `T` on the stack. `new T()` allocates a pointer of type `T*` on the heap. The syntax `T()` works with any types with a default constructor, e.g. `int()`.
KennyTM
@KennyTM: Shouldn't you be passing an instance of `HashCmp` using `HashCmp()` as hm3 constructor parameter?
Naveen
@Naveen: `HashCmp` is already an instance.
KennyTM
@KennyTM: Caught it. Never knew. Thanks.
MInner
A: 

It is creating an object of hash_map named hm3. Here is my take on the parameters:

Template parameter 1 (MyStr): Key for the map

Template parameter 2 (MyInt): Value for the key

Template parameter 3 : The comparison function to compare two keys. You are using a function called hash_compare (which again is a template) to do this.

The constructor you are using (2nd one in the MSDN) of the hash_map class requires an instance of the comparator function and the allocator. You are creating an unnamed (temporary) instance of the function object hash_compare and passing an allocator hm1_Alloc to the constructor.

Naveen