too much copy in map
Levente Farkas
!spam-lfarkas@mindmaker.hu
Sat Apr 1 00:00:00 GMT 2000
ok. then the proper question:
why is not there a way to insert something into a map with just
ONE copy contructor call ?
yes you can said that this's the standard, but if there be an
insert(const key_type&, const mapped_type&) or at least
operator[const key_type&] could have implement in a way to call
just one copy constructor.
> 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."
More information about the Libstdc++
mailing list