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

  c++, c++11, stdmap, stl, swap
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 it’s elements and then traverse bar to construct it’s new elements?
Or foo traverse all the keys of foo and bar and then execute only necessary modification?
I encountered such a situation that the access to a map by key can’t work for a short time. And the reason may be that the map was being updating 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?

Source: Windows Questions C++

LEAVE A COMMENT