jeudi 15 avril 2021

When a map is copied to another existing map, what happend in details?

int main ()
{
  std::map<char,int> foo,bar;

  foo['x']=100;
  foo['y']=200;
  foo['z']=300;

  bar['x']=100;
  bar['y']=2000;
  bar['zz']=400;

  foo=bar;
  return 0;
}

Is it such a process that foo clear all its elements and then traverse bar to construct its new elements? Or foo traverse all the keys of foo and bar and then execute only necessary modification? I encountered such a situation that access to a map by key can't work for a short time. And the reason may be that the map was being updated using the "=" assignment (foo=bar). Is it possible to solve this problem by using std::swap (foo.swap(bar))? Or a read-write lock must be used?

Aucun commentaire:

Enregistrer un commentaire