CppModule 08

08

typename์˜ 2๊ฐ€์ง€ ์˜๋ฏธ

  1. ํƒฌํ”Œ๋ฆฟ์˜ ํƒ€์ž… ๋งค๊ฐœ๋ณ€์ˆ˜ ์„ ์–ธ์‹œ(class์™€ ๋™์ผ)
  2. ํƒฌํ”Œ๋ฆฟ ํ•จ์ˆ˜/ํด๋ž˜์Šค ๋‚ด์—์„œ ํƒฌํ”Œ๋ฆฟ์— ์˜์กดํ•˜๋Š” ํƒ€์ž…์˜ ์ด๋ฆ„์„ ํƒ€์ž…์ด๋ผ๊ณ  ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ ๋งํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
    template<typename C>
    ...
    {
     typename C::iterator iter = ...;
     /*		    ------(1)
              ---------(2)
     ------------------(3)
     -----------------------(4)
     (1) : ์ปดํŒŒ์ผ๋Ÿฌ์•ผ, iterator๋ผ๋Š” ํ‚ค์›Œ๋“œ๋Š”
     (2) : ํƒฌํ”Œ๋ฆฟ์ธ์ž C์— ์˜์กด์ ์ธ
     (3) : ํƒ€์ž…๋ช…์ด๋ž€๋‹ค.
     (4) : ๋‚ด๊ฐ€ ๊ทธ ํƒ€์ž…์œผ๋กœ iter๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ• ๊ป€๋ฐ...
     */
    }
    

ex02

stack์€ ์ปจํ…Œ์ด๋„ˆ์˜ ํƒ€์ž… ์ค‘ ์ปจํ…Œ์ด๋„ˆ ์–ด๋Œ‘ํ„ฐ์— ์†ํ•˜๋ฉฐ, ๊ธฐ์กด ์ปจํ…Œ์ด๋„ˆ()์—์„œ ์ œ๊ณตํ•˜๋Šฅ์„ ์ œํ•œ/๋ณ€ํ˜•ํ•œ ์ปจํ…Œ์ด๋„ˆ์ด๋‹ค.

stack์ด์™ธ์—๋„ queue, primery queue๊ฐ€ ์žˆ๋‹ค.

์Šคํƒ(stack)์€ ์ œํ•œ์ ์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋‚˜์—ด ๊ตฌ์กฐ์ด๋‹ค. ๊ทธ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์€ ์–ธ์ œ๋‚˜ ๋ชฉ๋ก์˜ ๋์—์„œ๋งŒ ์ผ์–ด๋‚œ๋‹ค. ๋๋จผ์ €๋‚ด๊ธฐ ๋ชฉ๋ก(Pushdown list)์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค. ์Šคํƒ์€ ํ•œ ์ชฝ ๋์—์„œ๋งŒ ์ž๋ฃŒ๋ฅผ ๋„ฃ๊ฑฐ๋‚˜ ๋บ„ ์ˆ˜ ์žˆ๋Š” ์„ ํ˜• ๊ตฌ์กฐ(LIFO - Last In First Out)์œผ๋กœ ๋˜์–ด ์žˆ๋‹ค. ์ž๋ฃŒ๋ฅผ ๋„ฃ๋Š” ๊ฒƒ์„ โ€˜๋ฐ€์–ด๋„ฃ๋Š”๋‹คโ€™ ํ•˜์—ฌ ํ‘ธ์‰ฌ(push)๋ผ๊ณ  ํ•˜๊ณ  ๋ฐ˜๋Œ€๋กœ ๋„ฃ์–ด๋‘” ์ž๋ฃŒ๋ฅผ ๊บผ๋‚ด๋Š” ๊ฒƒ์„ ํŒ(pop)์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์ด๋•Œ ๊บผ๋‚ด์ง€๋Š” ์ž๋ฃŒ๋Š” ๊ฐ€์žฅ ์ตœ๊ทผ์— ํ‘ธ์‰ฌํ•œ ์ž๋ฃŒ๋ถ€ํ„ฐ ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค. ์ด์ฒ˜๋Ÿผ ๋‚˜์ค‘์— ๋„ฃ์€ ๊ฐ’์ด ๋จผ์ € ๋‚˜์˜ค๋Š” ๊ฒƒ์„ LIFO ๊ตฌ์กฐ๋ผ๊ณ  ํ•œ๋‹ค. -์œ„ํ‚ค๋ฐฑ๊ณผ-

์œ„์™€ ๊ฐ™์€ ์Šคํƒ์˜ ํŠน์„ฑ๋•Œ๋ฌธ์— ํŠน์ • ์ €์žฅ์œ„์น˜์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐ˜๋ณต์ž๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๊ณ , pop, push๋“ฑ ์Šคํƒ์˜ ๋งด๋ฒ„ํ•จ์ˆ˜๋“ค์„ ์ œ๊ณตํ•œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ stack์˜ ํ—ค๋”๋ฅผ ๊ฐ€๋ณด๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด stack์ด ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค.

...
// line 99
template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
...

// line 111
template <class _Tp, class _Container /*= deque<_Tp>*/>
class _LIBCPP_TEMPLATE_VIS stack
{
public:
    typedef _Container                               container_type;
    typedef typename container_type::value_type      value_type;
    typedef typename container_type::reference       reference;
    typedef typename container_type::const_reference const_reference;
    typedef typename container_type::size_type       size_type;
    static_assert((is_same<_Tp, value_type>::value), "" );
...

//line 122
protected:
    container_type c;
...

template <class _Tp, class _Container = deque<_Tp> > class _LIBCPP_TEMPLATE_VIS stack;
template <class _Tp, class _Container /*= deque<_Tp>*/>
class _LIBCPP_TEMPLATE_VIS stack
{
public:
    typedef _Container                               container_type;
    typedef typename container_type::value_type      value_type;
    typedef typename container_type::reference       reference;
    typedef typename container_type::const_reference const_reference;
    typedef typename container_type::size_type       size_type;
    static_assert((is_same<_Tp, value_type>::value), "" );
protected:
    container_type c;

๋”ฐ๋ผ์„œ ๊ณผ์ œ ๊ตฌํ˜„์„ ์œ„ํ•ด ๋ฐ˜๋ณต์ž๋ฅผ ๋”ฐ๋กœ ๊ตฌํ˜„ํ•  ํ•„์š” ์—†์ด, ์‹ค์ œ ์ €์žฅ๋˜๋Š” ์ปจํ…Œ์ด๋„ˆ์— ์ ‘๊ทผํ•˜์—ฌ ๋ฐ˜๋ณต์ž๋ฅผ ๊ฐ€์ ธ์˜ค๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ตฌํ˜„์ฝ”๋“œ

template<typename T>
class MutantStack : public std::stack<T>
{
	...
	public:
		typedef typename std::stack<T>::container_type::iterator iterator;
		iterator begin() throw()
		{
			return this->c.begin();
		}
		iterator end() throw()
		{
			return this->c.end();
		}
}
template<typename T>
class MutantStack : public std::stack<T>
typedef typename std::stack<T>::container_type::iterator iterator;
iterator begin() throw()
{
	return this->c.begin();
}
iterator end() throw()
{
	return this->c.end();
}

์ฐธ๊ณ ํ•œ ๊ณณ : ํ˜ผ์ž ์—ฐ๊ตฌํ•˜๋Š” C/C++