Because STD: auto_ptr is the transfer semantics, and the element of STL container must be the value semantics, that is, the copy semantics.
For example, STL containers store elements in the form of copies.
STD: vector <int> V;
Int A = 1;
V. push_back ();
V [0] is also an int with a value of 1, but it is not a .. It is just a copy. The value of A has not been changed.
STD: auto_ptr <int> P1 (New INT );
STD: auto_ptr <int> P2 = p1;
P2 structure modifies the value of P1, so that P1 gives reference to the dynamically allocated Int. At this time, P1 no longer references the dynamic Int. This is the transfer semantics.
In addition, STD: auto_ptr only provides such a copy constructor to meet the transfer semantic requirement.
Auto_ptr (auto_ptr &); instead of the normal T (const T &); this is a manifestation of non-value semantics.
STD: vector requires the copy constructor T (const T &) for the element type because it requires the value semantics.
It cannot be used for dynamically created arrays.
It may be because it is appropriate to use STD: vector most of the time, so there is no auto_array corresponding to auto_ptr. However, if you have special requirements, you can easily write an auto_array like STD: auto_ptr.
To use auto_ptr, you need to know:
1. Smart pointers cannot share ownership of Objects
2. Smart pointers cannot point to arrays. Because the implementation calls Delete instead of Delete []
3. The smart pointer cannot be an element of the container class.
Because STD: auto_ptr is the transfer semantics, and the element of STL container must be the value semantics, that is, the copy semantics.
For example, STL containers store elements in the form of copies.
STD: vector <int> V;
Int A = 1;
V. push_back ();
V [0] is also an int with a value of 1, but it is not a .. It is just a copy. The value of A has not been changed.
Let's take a look at STD: auto_ptr.
STD: auto_ptr <int> P1 (New INT );
STD: auto_ptr <int> P2 = p1;
P2 structure modifies the value of P1, so that P1 gives reference to the dynamically allocated Int. At this time, P1 no longer references the dynamic Int. This is the transfer semantics.
In terms of semantics, these two are not compatible.
In addition, STD: auto_ptr only provides such a copy constructor to meet the transfer semantic requirement.
Auto_ptr (auto_ptr &); instead of the normal T (const T &); this is a manifestation of non-value semantics.
STD: vector requires the copy constructor T (const T &) for the element type because it requires the value semantics.