@@ -84,6 +84,13 @@ public:
8484 */
8585 void push_back (T const &);
8686
87+ /* !
88+ * Add an element to the back.
89+ *
90+ * \param el Element.
91+ */
92+ void push_back (T const &&);
93+
8794 /* !
8895 * Remove an element from the back.
8996 *
@@ -95,7 +102,7 @@ public:
95102 friend void swap (Vector<U>&, Vector<U>&) noexcept ;
96103
97104private:
98- void copy_ (T const * const );
105+ void copy_ (T const * const , size_t const size );
99106
100107 size_t size_ {0 };
101108 T* data_ {nullptr };
@@ -105,7 +112,7 @@ private:
105112template <class T >
106113Vector<T>::Vector(Vector const & other)
107114 : size_ {other.size_ }, data_ {new T[other.size_ ]} {
108- copy_ (other.data_ );
115+ copy_ (other.data_ , other. size_ );
109116}
110117
111118template <class T >
@@ -120,7 +127,7 @@ template <class T>
120127template <size_t n>
121128Vector<T>::Vector(T const (&arr)[n])
122129 : size_ {n}, data_ {new T[n]} {
123- copy_ (arr);
130+ copy_ (arr, n );
124131}
125132
126133template <class T >
@@ -179,11 +186,9 @@ size_t Vector<T>::size() const {
179186
180187template <class T >
181188void Vector<T>::resize(size_t const size) {
182- size_ = min (size_, size);
183-
184189 T* data {new T[size]};
185190 swap_ (data_, data);
186- copy_ (data);
191+ copy_ (data, min (size_, size) );
187192 delete[] data;
188193
189194 size_ = size;
@@ -198,21 +203,27 @@ void Vector<T>::clear() {
198203
199204template <class T >
200205void Vector<T>::push_back(T const & el) {
201- resize (++size_);
206+ resize (size_ + 1 );
207+ data_[size_ - 1 ] = el;
208+ }
209+
210+ template <class T >
211+ void Vector<T>::push_back(T const && el) {
212+ resize (size_ + 1 );
202213 data_[size_ - 1 ] = el;
203214}
204215
205216template <class T >
206217T Vector<T>::pop_back() {
207218 T el {data_[size_ - 1 ]};
208- resize (-- size_);
219+ resize (size_ - 1 );
209220 return el;
210221}
211222
212223
213224template <class T >
214- void Vector<T>::copy_(T const * const data) {
215- for (size_t i {0 }; i < size_ ; ++i) {
225+ void Vector<T>::copy_(T const * const data, size_t const size ) {
226+ for (size_t i {0 }; i < size ; ++i) {
216227 data_[i] = data[i];
217228 }
218229}
0 commit comments