Home > Back-end >  Value for the class of the STL map, map [xx]=class assignment cause the copy constructor and assignm
Value for the class of the STL map, map [xx]=class assignment cause the copy constructor and assignm

Time:11-25

 # include & lt; Map> 
#include
using namespace std;

The class Test
{
Public:
The Test ()
{
Printf (" 1111% p \ n ", this);
}
The Test (const Test & amp; Test)
{
Printf (" 2222% p % p \ n ", this, & amp; Test).
}
Test& Operator=(const Test & amp; Test)
{
Printf (" 3333% p % p \ n ", this, & amp; Test).
}
};

Int main ()
{
Map MapTmp;
Test a;
Printf (" hello \ n ");
MapTmp [1]=a;
return -1;
}

Print results:
1111 0 x7ffd293db95b
 
Hello
1111 x7ffd293db8ef
2222 0 x7ffd293db914 0 x7ffd293db8ef
2222 0 x2455054 0 x7ffd293db914
3333 0 x2455054 0 x7ffd293db95b

I know analysis: 0 x7ffd293db95b this object is a, 0 x2455054 this object is in red and black tree map build good Test objects,
Problem: please explain the last four lines to print interpretation, each line is printed what is respectively, and the object is and why call the corresponding function, thanks

CodePudding user response:

Here is mainly the map operator [] members method; When this element does not exist in the map, the function USES the key to insert a new element, and returns the mapping value of the reference, please note that even if there is no value for the element distribution mapping (using the default constructor tectonic elements), it will add a container size;
Is equal to (* (this - & gt; Insert (make_pair (k, mapped_type ()))). The first)). The second
27 line of the code above is equivalent to (* ((mapTmp. Insert (make_pair (1, the Test ()))). The first)). The second=a;
So the last four lines of print respectively corresponding to the

Make_pair (1, the Test ())
==& gt; The default constructor: 1111 0 x7ffd293db8ef
==& gt; Copy constructor: 2222 0 x7ffd293db914 0 x7ffd293db8ef (make_pair create a pair object and invokes the method to construct pair pair (const _T1 & amp; __a, const _T2 & amp; __b) : first (__a), second (__b) {}, thus invoking the Test copy constructor)

MapTmp. Insert (make_pair (1, the Test ())
==& gt; Copy constructor: 2222 0 x7ffd293db914 0 x7ffd293db8ef (insert method of map, called pair of copy constructor, then call the Test copy constructor)

(* ((mapTmp. Insert (make_pair (1, the Test ()))). The first)). The second=a
==& gt; Assignment constructor: 3333 0 x2455054 0 x7ffd293db95b (call operator=constructor)

CodePudding user response:

C + + standard 98 g + + implementation, first mapped_type default constructed a (), and then copy constructed as value_type is STD: : a member of the pair, and then the whole pair is copied into the red-black tree structure, so there are two copies structure:

 
Mapped_type & amp;
Operator [] (const key_type & amp; __k)
{
//concept requirements
__glibcxx_function_requires (_DefaultConstructibleConcept & lt; Mapped_type & gt;)

The iterator __i=lower_bound (__k);
//__i - & gt; The first is greater than or equivalent to __k.
If (__i==end () | | key_comp () (__k, __i (*). The first))
# if __cplusplus & gt;
=201103 l__i=_M_t. _M_emplace_hint_unique (__i, STD: : piecewise_construct,
STD: : tuple (__k),
STD: : tuple<> ());
# the else
__i=insert (__i value_type (__k, mapped_type ()));//c + + 98: first mapped_type default constructed a (), and then copy constructed as value_type is STD: : a member of the pair, and then the whole pair is copied into the red-black tree structure, so there are two copies structure,
# endif
__i return (*). The second;
}



C + + 11 then directly in the red and black tree structure by default, do not need to copy,
  • Related