DSA - Data Structures and Algorithms
Loading...
Searching...
No Matches
dsa::Stack< T > Class Template Reference

Implements Stack class. More...

#include <stack.h>

Public Types

using value_type = T
 Alias for data type used in class.
using reference = T&
 Alias for reference to data type used in class.
using const_reference = const T&
 Alias for const reference to data type used in class.

Public Member Functions

 Stack ()=default
 Construct a new Stack object.
 Stack (const std::initializer_list< T > &init_list)
 Construct a new Stack object using initializer list.
 Stack (const Stack< T > &other)
 Construct a new Stack object using copy constructor.
auto operator= (const Stack< T > &other) -> Stack &
 Constructs Stack using copy assignment.
 Stack (Stack< T > &&other) noexcept
 Construct a new Stack object using move constructor.
auto operator= (Stack< T > &&other) noexcept -> Stack &
 Assign Stack object using move assignment.
 ~Stack ()=default
 Destroy the Stack object.
auto top () -> reference
 Function returns pointer to Stack top element.
auto top () const -> const_reference
 Function returns pointer to Stack top element.
auto empty () const -> bool
 Function checks if container has no elements.
auto size () const -> size_t
 Function returns Stack size.
void push (T value)
 Function add new element at the top of Stack.
void pop ()
 Function removes the top element of Stack.
void swap (Stack< T > &other) noexcept
 Function swaps content of two Stack objects.

Friends

auto operator== (const Stack< T > &stack1, const Stack< T > &stack2) -> bool
 Forward friend declaration to access internal container comparison operator.
auto operator< (const Stack< T > &stack1, const Stack< T > &stack2) -> bool
 Forward friend declaration to access internal container comparison operator.

Detailed Description

template<typename T>
class dsa::Stack< T >

Implements Stack class.

Template Parameters
Ttype of data stored in Stack
Todo

add operator<=>

add emplace

add non-member specialized swap function

Definition at line 43 of file stack.h.

Member Typedef Documentation

◆ const_reference

template<typename T>
using dsa::Stack< T >::const_reference = const T&

Alias for const reference to data type used in class.

Template Parameters
T&const reference to data type

Definition at line 66 of file stack.h.

◆ reference

template<typename T>
using dsa::Stack< T >::reference = T&

Alias for reference to data type used in class.

Template Parameters
T&reference to data type

Definition at line 59 of file stack.h.

◆ value_type

template<typename T>
using dsa::Stack< T >::value_type = T

Alias for data type used in class.

Template Parameters
Tdata type

Definition at line 52 of file stack.h.

Constructor & Destructor Documentation

◆ Stack() [1/3]

template<typename T>
dsa::Stack< T >::Stack ( const std::initializer_list< T > & init_list)

Construct a new Stack object using initializer list.

Parameters
[in]init_listinitializer list of values of type T

Definition at line 182 of file stack.h.

183 {
184 for (const auto& item : init_list)
185 {
186 container.push_back(item);
187 }
188 }
Implements Stack class.
Definition stack.h:44

◆ Stack() [2/3]

template<typename T>
dsa::Stack< T >::Stack ( const Stack< T > & other)

Construct a new Stack object using copy constructor.

Parameters
[in]otherStack object of type T

Definition at line 191 of file stack.h.

192 {
193 if (other.size() >= 1)
194 {
195 for (const auto& item : other.container)
196 {
197 container.push_back(item);
198 }
199 }
200 }
auto size() const -> size_t
Function returns Stack size.
Definition stack.h:257

◆ Stack() [3/3]

template<typename T>
dsa::Stack< T >::Stack ( Stack< T > && other)
noexcept

Construct a new Stack object using move constructor.

Content of other object will be taken by constructed object

Parameters
[in,out]otherStack object of type T

Definition at line 222 of file stack.h.

223 {
224 std::swap(container, other.container);
225 }

Member Function Documentation

◆ empty()

template<typename T>
auto dsa::Stack< T >::empty ( ) const -> bool
nodiscard

Function checks if container has no elements.

Return values
trueif container is empty
falseif container is not empty

Definition at line 251 of file stack.h.

252 {
253 return container.size() == 0;
254 }

◆ operator=() [1/2]

template<typename T>
auto dsa::Stack< T >::operator= ( const Stack< T > & other) -> Stack&

Constructs Stack using copy assignment.

Parameters
[in]otherStack object of type T
Returns
Stack& reference to Stack object

Definition at line 203 of file stack.h.

204 {
205 if (&other != this)
206 {
207 while (!container.empty())
208 {
209 container.pop_front();
210 }
211
212 for (const auto& item : other.container)
213 {
214 container.push_back(item);
215 }
216 }
217
218 return *this;
219 }

◆ operator=() [2/2]

template<typename T>
auto dsa::Stack< T >::operator= ( Stack< T > && other) -> Stack&
noexcept

Assign Stack object using move assignment.

Content of other object will be taken by constructed object

Parameters
[in,out]otherStack object of type T
Returns
Stack& reference to Stack object

Definition at line 228 of file stack.h.

229 {
230 if (&other != this)
231 {
232 std::swap(container, other.container);
233 }
234
235 return *this;
236 }

◆ pop()

template<typename T>
void dsa::Stack< T >::pop ( )

Function removes the top element of Stack.

Definition at line 269 of file stack.h.

270 {
271 container.pop_back();
272 }

◆ push()

template<typename T>
void dsa::Stack< T >::push ( T value)

Function add new element at the top of Stack.

Parameters
[in]valueelement of type T

Definition at line 263 of file stack.h.

264 {
265 container.push_back(value);
266 }

◆ size()

template<typename T>
auto dsa::Stack< T >::size ( ) const -> size_t
nodiscard

Function returns Stack size.

Returns
size_t number of elements in container

Definition at line 257 of file stack.h.

258 {
259 return container.size();
260 }

◆ swap()

template<typename T>
void dsa::Stack< T >::swap ( Stack< T > & other)
noexcept

Function swaps content of two Stack objects.

Parameters
[in,out]otherobject to swap content with

Definition at line 275 of file stack.h.

276 {
277 if (&other != this)
278 {
279 std::swap(container, other.container);
280 }
281 }

◆ top() [1/2]

template<typename T>
auto dsa::Stack< T >::top ( ) -> reference

Function returns pointer to Stack top element.

Returns
T& reference to Stack top element

Definition at line 239 of file stack.h.

240 {
241 return container.back();
242 }

◆ top() [2/2]

template<typename T>
auto dsa::Stack< T >::top ( ) const -> const_reference
nodiscard

Function returns pointer to Stack top element.

Returns
const T& const_reference to Stack top element

Definition at line 245 of file stack.h.

246 {
247 return container.back();
248 }

◆ operator<

template<typename T>
auto operator< ( const Stack< T > & stack1,
const Stack< T > & stack2 ) -> bool
friend

Forward friend declaration to access internal container comparison operator.

Template Parameters
Ttype of data stored in Stack
Parameters
[in]stack1input container
[in]stack2input container
Return values
trueif the content of stack1 are lexicographically less than the content of stack2
falseotherwise

Definition at line 346 of file stack.h.

347 {
348 return stack1.container < stack2.container;
349 }

◆ operator==

template<typename T>
auto operator== ( const Stack< T > & stack1,
const Stack< T > & stack2 ) -> bool
friend

Forward friend declaration to access internal container comparison operator.

Template Parameters
Ttype of data stored in Stack
Parameters
[in]stack1input container
[in]stack2input container
Return values
trueif containers are equal
falseif containers are not equal

Definition at line 315 of file stack.h.

316 {
317 return stack1.container == stack2.container;
318 }

The documentation for this class was generated from the following file: