vendredi 26 août 2016

Is c++11 operator[] equivalent to emplace on map insertion?

For C++11, is there still a performance difference between the following?

(for std::map<Foo, std::vector<Bar> > as an example)

map[key] = myVector and map.emplace(key, myVector)

The part I'm not figuring out is the exact internal of operator[]. My understanding so far has been (when key doesn't exist):

  1. Create a new key and the associated empty default vector in place inside the map
  2. Return the reference of the associated empty vector
  3. Assign myVector to the reference???

The point 3 is the part I couldn't understand, how can you assign a new value to a reference in the first place?

Though I cannot sort through point 3 I think somehow there's just a copy/move required. Assuming C++11 will be smart enough to know it's gonna be a move operation, is this whole "[]" assignment then already cheaper than insert()? Is it almost equivalent to emplace()? ---- default construction and move content over, versus construct vector with content directly in place?

Aucun commentaire:

Enregistrer un commentaire