7template<
typename ElementType>
21 if (this->m_size != 0 || m_ptr !=
nullptr)
24 this->m_ptr =
nullptr;
31 this->m_ptr =
nullptr;
44 {
return static_cast<const_iterator>(&this->m_ptr[this->m_size]); }
46 auto rbegin() {
return std::reverse_iterator(
end()); }
47 auto rend() {
return std::reverse_iterator(
begin()); }
53 size_t size()
const {
return this->m_size; }
59 else if(
count == this->m_size)
69 this->m_ptr =
new ElementType[this->m_size];
84 stream <<
"Default constructor called" <<
endl;
88 this->m_ptr =
size > 0 ?
new ElementType[
size] :
nullptr;
100 m_size{right_hand_side.m_size}
106 this->m_ptr = this->m_size > 0 ?
new ElementType[this->m_size] :
nullptr;
109 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
121 ElementType &
operator[](
size_t index) {
return this->m_ptr[index]; }
123 const ElementType &
operator[](
size_t index)
const {
return this->m_ptr[index]; }
125 ElementType &
at(
size_t index)
127 if (index < this->m_size && this->m_ptr !=
nullptr)
128 return this->m_ptr[index];
133 const ElementType &
at(
size_t index)
const
135 if (index < this->m_size && this->m_ptr !=
nullptr)
136 return this->m_ptr[index];
145 stream <<
"Copy assignment operator() called" <<
endl;
152 if (
this != std::addressof(right_hand_side))
154 if (this->m_size == right_hand_side.m_size)
161 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
167 this->m_size = right_hand_side.m_size;
169 if (this->m_size > 0)
174 this->m_ptr =
new ElementType[this->m_size];
175 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
190 : m_size{right_hand_side.m_size},
191 m_ptr{right_hand_side.m_ptr}
198 right_hand_side.invalidate();
203 stream <<
"Move assignment operator() called" <<
endl;
205 if (
this != std::addressof(right_hand_side))
209 this->m_size = right_hand_side.m_size;
210 this->m_ptr = right_hand_side.m_ptr;
215 right_hand_side.invalidate();
237 for(
size_t i = 0; i <
size; ++i)
240 os << da[
size] <<
" }";
251 for(
size_t i = 0; i < a.size(); ++i)
261 std::vector< dynamic_array_new<int> > jagged_array(
count);
263 stream <<
"At this point, default constructor of dynamic_array_new is called "
266 for(
size_t i = 0; i < jagged_array.size(); ++i)
269 stream <<
"In the for loop, default constructor of dynamic_array_new is called "
270 <<
count <<
" times, and move constructor is called " <<
count <<
" times\n" <<
endl;
277 stream <<
"\nthis is better, but not perfect!!\n" <<
endl;
281 std::vector< dynamic_array_new<int> > jagged_array;
282 jagged_array.reserve(
count);
284 for(
size_t i = 0; i < jagged_array.capacity(); ++i)
291 stream <<
"\nthis is PEREFECT WAY, YOU SHOULD ALWAYS USE THIS METHOD!!\n" <<
endl;
295 std::vector< dynamic_array_new<int> > jagged_array;
296 jagged_array.reserve(
count);
298 for(
size_t i = 0; i < jagged_array.capacity(); ++i)
300 jagged_array.emplace_back(i + 1);
307 for(
size_t j = 0; j < jagged_array.back().size(); ++j)
308 jagged_array.back()[j] = (int)j;
313 for(
auto& da: jagged_array)
329 for_each(array.crbegin(), array.crend(), [](
auto e)
331 std::cout << e << std::endl;
void examples_for_dynamic_array_new()
void please_use_this_method()
void is_still_is_not_perfect()
dynamic_array_new(size_t size=1)
friend std::ostream & operator<<(std::ostream &os, const dynamic_array_new &da)
const ElementType & operator[](size_t index) const
dynamic_array_new & operator=(const dynamic_array_new &right_hand_side)
ElementType * operator&()
dynamic_array_new(const dynamic_array_new &right_hand_side)
dynamic_array_new & operator=(dynamic_array_new &&right_hand_side) noexcept
dynamic_array_new(dynamic_array_new &&right_hand_side) noexcept
ElementType & at(size_t index)
const ElementType & at(size_t index) const
const ElementType * const_iterator
ElementType & operator[](size_t index)
void resize(size_t count)
Stream output operators << are implemented.
#define Tpf_ThrowDebugException(debug_message)
Throw a debug_exception with message as argument.