too much copy in map

Jeremy G. Siek jsiek@cthulhu.engr.sgi.com
Sat Apr 1 00:00:00 GMT 2000


Hi,

The first copy is even before you get to the insert().
The "Map::value_type(1,A())" creates a pair, *copying* A()
into the pair's data member. So there is only one copy
inside insert(), and that is necessary.

Levente Farkas wrote:
> 
> hi,
> why is there so much contructor call in map. if I'd like to insert a new
> element into a map it's call too copy constructor at least. why ?
> ---------------
> #include <map>
> #include <iostream>
> using namespace std;
> 
> struct A
> {
>   A() { cout << "def "; }
>   A(const A&) { cout << "copy "; }
> };
> 
> int main()
> {
>   typedef map<int, A> Map;
>   Map m;
>   m.insert(Map::value_type(1, A()));
>   A& a = m[2];
>   m.insert(m.end(), Map::value_type(3, A()));
>   return 0;
> }
> ---------------
> the output is:
> ------
> def copy copy def copy copy def copy copy
> ------
> all of these 3 insertion call 2 times the copy constuctor.
> it can't be implemeted just 1 cc call for an insertation ?
> or is there any way to do so ?
> thnaks.
> 
>  -- lfarkas
>  "The only thing worse than not knowing the truth is
>   ruining the bliss of ignorance."

-- 
------------------------------------------------------------------------
 Jeremy Siek                        email: jsiek@engr.sgi.com
 Ph.D. Candidate               cell-phone: (415) 377-5814
 Univ. of Notre Dame                phone: (650) 933-8724  
 C++ Library & Compiler Group         fax: (650) 932-0127
 Silicon Graphics Inc.                www: http://www.lsc.nd.edu/~jsiek/
------------------------------------------------------------------------


More information about the Libstdc++ mailing list