.


:




:

































 

 

 

 





 

. . , , , , :

 

template < typename T, int SIZE = 10 >
class Stack

{

//...
private:
T m_Data[ SIZE ];
T * m_pTop;
};

 

- ( ). . , , , :

 

if (SIZE > 10)

do1();

Else

do2();

 

., , SIZE > 10 . , . SIZE , . do1(), do2(), SIZE, .

 

. , Stack. , . , , .. . . , , . , , .

 

, : Stack< int, 10 > , Stack< int, 5 >. , , .

 

:

 

stack_fixed_array.hpp

 

#ifndef _STACK_FIXED_ARRAY_HPP_

#define _STACK_FIXED_ARRAY_HPP_

 

#include <stdexcept>

#include <initializer_list>

 

//*****************************************************************************

 

template < typename T, int SIZE = 10 >

class Stack

{

 

/*-----------------------------------------------------------------*/

 

public:

 

/*-----------------------------------------------------------------*/

 

// - !

template < typename, int > friend class Stack;

 

/*-----------------------------------------------------------------*/

 

// . !

Stack ();

 

//

template < typename U >

Stack (std::initializer_list< U > _l);

 

//

template < typename U, int OTHER_SIZE >

Stack (const Stack< U, OTHER_SIZE > & _s);

 

//

template < typename U, int OTHER_SIZE >

Stack< T, SIZE > & operator = (const Stack< U, OTHER_SIZE > & _s);

 

//

void push (const T & _value);

 

//

void pop ();

 

//

T & top () const;

 

//

bool isEmpty () const;

 

//

bool isFull () const;

 

/*-----------------------------------------------------------------*/

 

private:

 

/*-----------------------------------------------------------------*/

 

//

T m_Data[ SIZE ];

 

// - m_Data

T * m_pTop;

 

/*-----------------------------------------------------------------*/

 

};

 

 

//*****************************************************************************

 

//

template < typename T, int SIZE >

Stack< T, SIZE >::Stack ()

{

//

m_pTop = m_Data;

}

 

//*****************************************************************************

 

//

template < typename T, int SIZE >

template < typename U >

Stack< T, SIZE >::Stack (std::initializer_list< U > _l)

{

//

m_pTop = m_Data;

 

// U->T

for (const U & x: _l)

push((const T &) x);

}

 

//*****************************************************************************

 

//

template < typename T, int SIZE >

template < typename U, int OTHER_SIZE >

Stack< T, SIZE >::Stack (const Stack< U, OTHER_SIZE > & _s)

{

//

m_pTop = m_Data;

 

//

int nActual = _s.m_pTop - _s.m_Data;

 

// U->T

for (int i = 0; i < nActual; i++)

push((T) _s.m_Data[ i ]);

}

 

//*****************************************************************************

 

//

template < typename T, int SIZE >

template < typename U, int OTHER_SIZE >

Stack< T, SIZE > &

Stack< T, SIZE >:: operator = (const Stack< U, OTHER_SIZE > & _s)

{

//

if ((const void *)(this) == (const void *)(&_s))

return * this;

 

//

m_pTop = m_Data;

 

//

int nActual = _s.m_pTop - _s.m_Data;

 

// U->T

for (int i = 0; i < nActual; i++)

push(_s.m_Data[ i ]);

 

//

return * this;

}

 

 

//*****************************************************************************

 

//

template < typename T, int SIZE >

void Stack< T, SIZE >::push (const T & _value)

{

// 100%

if (isFull())

throw std::logic_error("Stack overflow error");

 

// -

* m_pTop++ = _value;

}

 

//*****************************************************************************

 

//

template < typename T, int SIZE >

void Stack< T, SIZE >::pop ()

{

//

if (isEmpty())

throw std::logic_error("Stack underflow error");

 

// -

m_pTop--;

}

 

//*****************************************************************************

 

//

template < typename T, int SIZE >

T & Stack< T, SIZE >::top () const

{

//

if (isEmpty())

throw std::logic_error("Stack is empty");

 

// , -

return *(m_pTop - 1);

}

 

//*****************************************************************************

 

//

template < typename T, int SIZE >





:


: 2017-01-21; !; : 376 |


:

:

: , .
==> ...

1955 - | 1578 -


© 2015-2024 lektsii.org - -

: 0.027 .