Jaden Ng
Jaden Ng

Reputation: 131

Assign object into unordered_map array?

typedef tr1::unordered_map <string, pin *> pin_cmp;
pin_cmp _pin_cmp;
_Pins[_num_pins] = new pin (pin_id, _num_pins, s, n, d);
_pin_cmp[_Pins[_num_pins]->get_name ()] = _Pins[_num_pins]; //performance profiling

Could you teach me what actually the code doing?

_pin_cmp[_Pins[_num_pins]->get_name ()] = _Pins[_num_pins]; //performance profiling

I am not familiar with unordered_map which still can use with array[].I am confuse unordered_map just need key and value why will have array[]?

Upvotes: 0

Views: 442

Answers (1)

Enigma
Enigma

Reputation: 1717

In above example I expect _Pins to be a sequential container.

_pin_cmp[_Pins[_num_pins]->get_name ()] = _Pins[_num_pins]; //performance profiling

This line of code accesses a element _Pins[_num_pins] twice:

  1. On the right handside to get the object
  2. On the left handside to get the name of the object.

Then the object is placed inside _pin_cmp (unordered map) using the name of the object as index. Exact behavior of this operation is described here.

Upvotes: 1

Related Questions