<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>JEVLOG</title>
    <link>https://jevlog.tistory.com/</link>
    <description>개발 블로그</description>
    <language>ko</language>
    <pubDate>Sat, 11 Apr 2026 11:37:39 +0900</pubDate>
    <generator>TISTORY</generator>
    <ttl>100</ttl>
    <managingEditor>gyunnnnnn</managingEditor>
    <image>
      <title>JEVLOG</title>
      <url>https://tistory1.daumcdn.net/tistory/5745250/attach/038f060e9cff41128c72748242caa11c</url>
      <link>https://jevlog.tistory.com</link>
    </image>
    <item>
      <title>다중 상속과 가상 상속</title>
      <link>https://jevlog.tistory.com/36</link>
      <description>&lt;h2&gt;멤버 함수와 가상 함수의 동작 원리&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;객체가 생성되면 &lt;strong&gt;멤버 변수는 객체 내&lt;/strong&gt;에 존재한다.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;멤버 함수는 메모리의 한 공간&lt;/strong&gt;에 별도로 위치하고 이 함수가 정의된 클래스의 &lt;strong&gt;모든 객체가 이를 공유&lt;/strong&gt;하는 형태를 취한다.&lt;/li&gt;
&lt;li&gt;한 개 이상의 가상 함수를 포함하는 클래스에 대해서는 컴파일러가 &lt;strong&gt;가상 함수 테이블&lt;/strong&gt;을 만든다.&lt;/li&gt;
&lt;li&gt;가상 함수 테이블은 객체의 &lt;strong&gt;생성과 상관없이&lt;/strong&gt; &lt;code&gt;main&lt;/code&gt; 함수가 호출되기전 메모리 공간에 할당된다.&lt;/li&gt;
&lt;li&gt;가상 함수 테이블은 호출되어야 할 &lt;strong&gt;함수의 위치정보&lt;/strong&gt;를 담고 있는 테이블이다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;class A
{
public:
    virtual void Func() { cout &amp;lt;&amp;lt; &amp;quot;A&amp;quot; &amp;lt;&amp;lt; endl; }
};

class B : public A
{
public:
    void Func() { cout &amp;lt;&amp;lt; &amp;quot;B&amp;quot; &amp;lt;&amp;lt; endl; }
};

int main()
{
    A* a = new A();
    a-&amp;gt;Func(); // 결과는 A
    A* b = new B();
    b-&amp;gt;Func(); // 결과는 B
    return 0;
}&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;A &amp;lt;- B&lt;/code&gt; 구조에서 B 클래스의 가상 함수 테이블을 살펴보면, &lt;strong&gt;오버 라이딩&lt;/strong&gt;된 A클래스의 &lt;code&gt;Func&lt;/code&gt; 함수에 대한 정보가 없다.&lt;ul&gt;
&lt;li&gt;가상 함수의 호출 원리로 b의 &lt;code&gt;Func&lt;/code&gt; 함수를 호출하면 B클래스의 &lt;code&gt;Func&lt;/code&gt; 함수가 호출된다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;다중 상속&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;class A
{
public:
    void Func() { }
};

class B
{
public:
    void Func() { }
};

class C : public A, public B
{
public:
    void Call Func() 
    {
        Func(); // 모호성 존재
        A::Func();
        B::Func();
    }
};&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;어느 클래스에 선언된 멤버에 접근을 하는지에 대해 &lt;strong&gt;모호성&lt;/strong&gt;이 존재한다.&lt;/li&gt;
&lt;li&gt;A, B 클래스가 같은 이름의 함수를 가지고 있다면 &lt;code&gt;A::Func();&lt;/code&gt;, &lt;code&gt;B::Func();&lt;/code&gt;로 &lt;strong&gt;클래스를 명시&lt;/strong&gt;해야 한다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;가상 상속&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;class Base
{
public:
    Base() { cout &amp;lt;&amp;lt; &amp;quot;Base Constructor&amp;quot; &amp;lt;&amp;lt; endl; }
    void BaseFunc() { cout &amp;lt;&amp;lt; &amp;quot;Base&amp;quot; &amp;lt;&amp;lt; endl; }
};

class Middle1 : virtual public Base
{
public:
    Middle1() : Base() { cout &amp;lt;&amp;lt; &amp;quot;Middle1 Constructor&amp;quot; &amp;lt;&amp;lt; endl; }
    void Middle1Func()
    {
        BaseFunc();
        cout &amp;lt;&amp;lt; &amp;quot;Middle1&amp;quot; &amp;lt;&amp;lt; endl;
    }
};

class Middle2 : virtual public Base
{
public:
    Middle2() : Base() { cout &amp;lt;&amp;lt; &amp;quot;Middle2 Constructor&amp;quot; &amp;lt;&amp;lt; endl;  }
    void Middle2Func()
    {
        BaseFunc();
        cout &amp;lt;&amp;lt; &amp;quot;Middle2&amp;quot; &amp;lt;&amp;lt; endl;
    }
};

class Last : public Middle1, public Middle2
{
public:
    Last() : Middle1(), Middle2() { cout &amp;lt;&amp;lt; &amp;quot;Last Constructor&amp;quot; &amp;lt;&amp;lt; endl; }
    void LastFunc()
    {
        Middle1Func();
        Middle2Func();
        BaseFunc();
    }
};

int main()
{
    Last last;
    last.LastFunc();
    return 0;
}&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;다중 상속을 하면 &lt;code&gt;last&lt;/code&gt; 생성 시 &lt;code&gt;base&lt;/code&gt; 생성자가 &lt;strong&gt;두 번&lt;/strong&gt; 호출된다.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Middle1&lt;/code&gt;, &lt;code&gt;Middle2&lt;/code&gt;에서 &lt;strong&gt;가상 상속&lt;/strong&gt;을 통해 가상 상속을 하면 &lt;code&gt;base&lt;/code&gt; 생성자가 &lt;strong&gt;한 번&lt;/strong&gt;만 호출되도록 할 수 있다.&lt;/li&gt;
&lt;/ul&gt;</description>
      <category>Programming | 프로그래밍 언어/C++</category>
      <category>CPP</category>
      <category>Multiple Inheritance</category>
      <category>virtual</category>
      <category>virtual inheritance</category>
      <category>가상 상속</category>
      <category>다중 상속</category>
      <category>상속</category>
      <author>gyunnnnnn</author>
      <guid isPermaLink="true">https://jevlog.tistory.com/36</guid>
      <comments>https://jevlog.tistory.com/36#entry36comment</comments>
      <pubDate>Fri, 27 Jan 2023 20:06:44 +0900</pubDate>
    </item>
    <item>
      <title>가상 함수</title>
      <link>https://jevlog.tistory.com/35</link>
      <description>&lt;h2&gt;객체 포인터의 참조 관계&lt;/h2&gt;
&lt;h3&gt;객체 포인터 변수&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;객체의 주소 값을 저장하는 포인터 변수&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;Person * ptr = new Person();&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;A&lt;/code&gt;형 포인터 변수는 &lt;code&gt;A&lt;/code&gt;&lt;strong&gt;객체&lt;/strong&gt; 또는 &lt;code&gt;A&lt;/code&gt;를 직접 혹은 간접적으로 &lt;strong&gt;상속하는 모든 객체&lt;/strong&gt;를 가리킬 수 있다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;class Person{};
class Student : public Person{};
class PartTimeStudent : public Student{};
Person * ptr1 = new Student(); // Person형 포인터 변수 ptr
Person * ptr2 = new PartTimeStudent(); // Person형 포인터 변수 ptr
Student * ptr3 = new PartTimeStudent(); // Student형 포인터 변수 ptr&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;위와 같이 &lt;code&gt;Person&lt;/code&gt;형 포인터는 &lt;code&gt;Person&lt;/code&gt; 객체뿐만 아니라, &lt;code&gt;Person&lt;/code&gt;을 상속하는 파생 클래스의 객체도 가리킬 수 있다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;각 객체 포인터에 대한 참조 가능 여부&lt;/h3&gt;
&lt;p&gt;&lt;img src=&quot;/img/cpp9.jpg&quot; alt=&quot;cpp9&quot;&gt;&lt;/p&gt;
&lt;h2&gt;함수 오버 라이딩&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;파생 클래스와 기본 클래스에서 &lt;strong&gt;동일한 이름과 형태&lt;/strong&gt;로 두 함수를 정의하는 것&lt;/li&gt;
&lt;li&gt;오버 라이딩된 기본 클래스의 함수는 오버 라이딩을 한 파생 클래스의 함수에 가려진다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;오버 로딩 vs 오버 라이딩&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;오버 로딩&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;같은 클래스 내에서 같은 이름의 메서드를 사용하는 것&lt;/li&gt;
&lt;/ul&gt;
&lt;ol start=&quot;2&quot;&gt;
&lt;li&gt;오버 라이딩&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;기본 클래스에서 정의한 메서드를 파생 클래스에서 변경하는 것&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;가상 함수&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;C++ 컴파일러는 포인터 연산의 가능성 여부를 판단할 때 선언한 &lt;strong&gt;포인터의 자료형&lt;/strong&gt;을 기준으로 판단하지, &lt;strong&gt;실제 가리키는 객체&lt;/strong&gt;의 자료형을 기준으로 판단하지 않는다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;// 컴파일 성공
// 포인터는 Base
Base * bptr = new Derived(); 

// 컴파일 에러
Derived * dptr = bptr; 

// 컴파일 에러
// bptr 포인터는 Base이기 때문에 Derived에 있는 함수는 호출할 수 없다.
bptr -&amp;gt; DerivedFunc() 

// 컴파일 성공
Derived * dptr = new Derived();

// 컴파일 성공
Base * bptr = dptr;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;virtual&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;virtual&lt;/code&gt; 키워드는 함수에 붙일 수 있다. 부모가 자식을 가리켜 자식에서 새롭게 정의된 기능을 쓸 수 있다.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;virtual&lt;/code&gt; 키워드는 오버 라이딩을 성립할 수 있게 해 주어 다형성을 구현하는 핵심 역할을 한다.&lt;/li&gt;
&lt;li&gt;가상 함수 포인터로 인해 클래스 크기에 4Byte가 더 들어간다.(32비트 컴퓨터 기준)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;memset&lt;/code&gt;으로 객체를 초기화하면 &lt;strong&gt;가상 함수 테이블 정&lt;/strong&gt;보까지 같이 초기화 된다.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;virtual&lt;/code&gt; 키워드를 사용해 가상 함수로 선언되면, 해당 함수 호출 시 &lt;strong&gt;포인터의 자료형&lt;/strong&gt;을 기반으로 호출 대상을 결정하지 않고, 포인터 변수가 &lt;strong&gt;실제로 가리키는 객체&lt;/strong&gt;를 참조하여 &lt;strong&gt;호출의 대상을 결정&lt;/strong&gt;한다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;class A
{
public:
    int a;
    virtual void func()
    {
        cout &amp;lt;&amp;lt; &amp;quot;A&amp;quot; &amp;lt;&amp;lt; endl;
    }
};

class B : public A
{
public:
    int a;
    virtual void func()
    {
        cout &amp;lt;&amp;lt; &amp;quot;B&amp;quot; &amp;lt;&amp;lt; endl;
    }
};

class C : public B
{
public:
    int a;
    virtual void func()
    {
        cout &amp;lt;&amp;lt; &amp;quot;C&amp;quot; &amp;lt;&amp;lt; endl;
    }
};

int main()
{
    A* a = new A;
    A* b = new B;
    A* c = new C;

    a-&amp;gt;func();
    b-&amp;gt;func();
    c-&amp;gt;func();

    cout &amp;lt;&amp;lt; sizeof(a) &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; sizeof(b) &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; sizeof(c) &amp;lt;&amp;lt; endl; // 4,4,4
    cout &amp;lt;&amp;lt; sizeof(A) &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; sizeof(B) &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; sizeof(C) &amp;lt;&amp;lt; endl; // 8,12,16
}&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;실행 결과 : A B C&lt;/li&gt;
&lt;li&gt;virtual 선언이 안 되어 있다면 A A A로 출력된다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;다형성&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;모습은 같은데 형태는 다르다 = 문장은 같은데 결과는 다르다.&lt;/code&gt;&lt;ul&gt;
&lt;li&gt;참조하는 객체의 자료형이 다르기 때문이다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;이름이 같은 함수여도 참조하는 객체에 따라서 결과가 다르다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;순수 가상 함수&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;함수의 몸체가 정의되지 않은 함수&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;0의 대입&lt;/strong&gt;을 통해 표현한다.&lt;/li&gt;
&lt;li&gt;잘못된 &lt;strong&gt;객체 생성을 막는 용도&lt;/strong&gt;로 사용한다.&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;virtual int AbstractFunc() = 0; // 순수 가상함수&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;추상 클래스&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;하나 이상의 멤버 함수를 순수 가상 함수로 선언한 클래스를 &lt;strong&gt;추상 클래스&lt;/strong&gt;라 한다.&lt;/li&gt;
&lt;li&gt;추상 클래스는 &lt;strong&gt;객체 생성이 불가능한 클래스&lt;/strong&gt;라는 의미를 지닌다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;가상 소멸자&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;맨 위에 존재하는 기본 클래스의 소멸자만 &lt;code&gt;virtual&lt;/code&gt;로 선언하면 이를 상속하는 &lt;strong&gt;파생 클래스의 소멸자&lt;/strong&gt;들도 &lt;strong&gt;모두 가상 소멸자&lt;/strong&gt;로 선언이 된다.&lt;/li&gt;
&lt;li&gt;가상 소멸자가 호출되면, 상속의 계층구조상 맨 아래에 존재하는 파생 클래스의 소멸자가 대신 호출된다.&lt;/li&gt;
&lt;li&gt;파생 클래스 -&amp;gt; 기본 클래스의 순으로 소멸자가 호출된다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;class First
{
public:
    string strOne;
    First(string str) : strOne(str){}
    virtual ~First()
    {
        cout &amp;lt;&amp;lt; &amp;quot;~First()&amp;quot; &amp;lt;&amp;lt; endl;
    }
};

class Second : public First
{
public:
    string strTwo;
    Second(string str1, string str2) : First(str1), strTwo(str2) {}
    ~Second()
    {
        cout &amp;lt;&amp;lt; &amp;quot;~Second()&amp;quot; &amp;lt;&amp;lt; endl;
    }
};

First * ptr1 = new Second(&amp;quot;simple&amp;quot;, &amp;quot;complex&amp;quot;);
delete ptr1;

Second * ptr2 = new Second(&amp;quot;simple&amp;quot;, &amp;quot;complex&amp;quot;);
delete ptr2;&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;모두 &lt;code&gt;~Second() -&amp;gt; ~First()&lt;/code&gt; 순으로 소멸자가 호출된다.&lt;/li&gt;
&lt;li&gt;소멸자에 &lt;code&gt;virtual&lt;/code&gt; 선언이 없다면 1에서는 &lt;code&gt;~First()&lt;/code&gt;만 호출이 된다.&lt;/li&gt;
&lt;/ul&gt;</description>
      <category>Programming | 프로그래밍 언어/C++</category>
      <category>abstract</category>
      <category>CPP</category>
      <category>overriding</category>
      <category>polymorphism</category>
      <category>vitrtual</category>
      <category>가상함수</category>
      <category>다형성</category>
      <category>오버라이딩</category>
      <category>추상클래스</category>
      <author>gyunnnnnn</author>
      <guid isPermaLink="true">https://jevlog.tistory.com/35</guid>
      <comments>https://jevlog.tistory.com/35#entry35comment</comments>
      <pubDate>Mon, 16 Jan 2023 20:03:42 +0900</pubDate>
    </item>
    <item>
      <title>C++에서 상속에 대해 정리</title>
      <link>https://jevlog.tistory.com/34</link>
      <description>&lt;h2 data-ke-size=&quot;size26&quot;&gt;상속이란?&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;기존에 정의해 놓은 클래스의 &lt;b&gt;재활용을 목적&lt;/b&gt;으로 만들어진 문법적 요소이다.
&lt;pre class=&quot;angelscript&quot;&gt;&lt;code&gt;class Base{};
class Derived : public Base{};&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;상속받은 클래스(파생 클래스)의 생성자 정의&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;span style=&quot;color: red;&quot;&gt;파생 클래스&lt;/span&gt;의 객체 생성 과정에서 &lt;span style=&quot;color: blue;&quot;&gt;기초 클래스&lt;/span&gt;의 생성자는 &lt;b&gt;무조건&lt;/b&gt; 호출된다.&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: red;&quot;&gt;파생 클래스&lt;/span&gt;의 생성자는 &lt;span style=&quot;color: blue;&quot;&gt;기초 클래스&lt;/span&gt;의 멤버까지 초기화 할 의무가 있다.&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: red;&quot;&gt;파생 클래스&lt;/span&gt;의 생성자는 &lt;span style=&quot;color: blue;&quot;&gt;기초 클래스&lt;/span&gt;의 생성자를 호출해서 부모 클래스의 멤버를 초기화 하는 것이 좋다.&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: red;&quot;&gt;파생 클래스&lt;/span&gt;의 생성자에서 &lt;span style=&quot;color: blue;&quot;&gt;기초 클래스&lt;/span&gt;의 생성자 호출을 명시하지 않으면, &lt;span style=&quot;color: blue;&quot;&gt;기초 클래스&lt;/span&gt;의 &lt;b&gt;void 생성자&lt;/b&gt;가 호출된다.&lt;/li&gt;
&lt;li&gt;접근 제한의 기준은 클래스이므로 상속받은 &lt;code&gt;private&lt;/code&gt; 변수는 그 클래스의 &lt;code&gt;public&lt;/code&gt; 함수를 통해서 간접적으로 접근을 해야한다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;파생 클래스의 객체 생성과정&lt;/h3&gt;
&lt;ol style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;메모리 공간 할당&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: red;&quot;&gt;파생 클래스&lt;/span&gt;의 생성자 호출&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: blue;&quot;&gt;기초 클래스&lt;/span&gt;의 생성자 호출&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: blue;&quot;&gt;기초 클래스&lt;/span&gt;의 생성자 실행&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: red;&quot;&gt;파생 클래스&lt;/span&gt;의 생성자 실행&lt;/li&gt;
&lt;li&gt;객체 생성 완료&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;파생 클래스 객체의 소멸과정&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;span style=&quot;color: red;&quot;&gt;파생 클래스&lt;/span&gt;의 객체가 소멸될 때는, &lt;span style=&quot;color: red;&quot;&gt;파생 클래스&lt;/span&gt;의 &lt;b&gt;소멸자&lt;/b&gt;가 실행되고 &lt;span style=&quot;color: blue;&quot;&gt;기초 클래스&lt;/span&gt;의 &lt;b&gt;소멸자&lt;/b&gt;가 실행된다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;소멸자&lt;/b&gt;에선 &lt;b&gt;자신의 생성자&lt;/b&gt;에서 할당한 메모리 공간에 대한 해제만을 책임진다.&lt;/li&gt;
&lt;li&gt;스택에 생성된 객체의 &lt;b&gt;소멸순서는 생성순서와 반대&lt;/b&gt;이다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;세 가지 형태의 상속&lt;/h2&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;public 상속&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;code&gt;public&lt;/code&gt;으로 선언된 멤버변수는 &lt;b&gt;모든 클래스&lt;/b&gt;에서 접근이 가능하다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;protected 상속&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;code&gt;protected&lt;/code&gt;로 선언된 멤버변수는 이를 &lt;b&gt;상속하는 파생 클래스&lt;/b&gt;에서 접근이 가능하다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;private 상속&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;code&gt;private&lt;/code&gt;로 선언된 멤버변수는 &lt;b&gt;접근이 불가능&lt;/b&gt;하다.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;A &amp;lt;- B &amp;lt;- C&lt;/code&gt; 관계에서 B를 &lt;code&gt;private&lt;/code&gt;상속하면 C가 B를 &lt;code&gt;public&lt;/code&gt;으로 상속받아도 B의 멤버변수에 접근이 불가능하다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;상속을 위한 조건&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;상속을 위한 기본 조건으로는 &lt;code&gt;IS-A&lt;/code&gt; 관계의 성립이 있다.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;HAS-A&lt;/code&gt; 관계도 상속의 조건은 되지만 &lt;b&gt;객체 합성&lt;/b&gt;으로 이를 대신하는 것이 일반적이다.&lt;/li&gt;
&lt;li&gt;상속을 통해 연관된 일련의 클래스에 대해 &lt;b&gt;공통적인 규약을 정의&lt;/b&gt;할 수 있다.&lt;/li&gt;
&lt;/ul&gt;</description>
      <category>Programming | 프로그래밍 언어/C++</category>
      <category>base</category>
      <category>CPP</category>
      <category>Derived</category>
      <category>inheritance</category>
      <category>private</category>
      <category>protected</category>
      <category>public</category>
      <category>상속</category>
      <author>gyunnnnnn</author>
      <guid isPermaLink="true">https://jevlog.tistory.com/34</guid>
      <comments>https://jevlog.tistory.com/34#entry34comment</comments>
      <pubDate>Fri, 13 Jan 2023 20:11:15 +0900</pubDate>
    </item>
    <item>
      <title>백준 17143. 낚시왕</title>
      <link>https://jevlog.tistory.com/33</link>
      <description>&lt;h2&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/17143&quot;&gt;17143. 낚시왕&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;문제에서 주어진대로 진행하는 시뮬레이션 문제이다. &lt;/li&gt;
&lt;li&gt;정렬을 편하게 하기 위해 &lt;strong&gt;상어 구조체&lt;/strong&gt;를 만들고 &lt;strong&gt;크기순&lt;/strong&gt;, &lt;strong&gt;번호순&lt;/strong&gt;으로 정렬하기 위한 compare함수를 선언하였다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;풀이 과정&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;낚시왕이 &lt;strong&gt;오른쪽&lt;/strong&gt;으로 한 칸 이동한다.&lt;/li&gt;
&lt;li&gt;낚시왕이 있는 열에 있는 상어 중에서 &lt;strong&gt;가장 땅과 가까운 상어&lt;/strong&gt;를 잡는다. 이때, 상어를 잡으면 잡힌 상어는 &lt;strong&gt;격자판에서 사라진다&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;상어를 이동&lt;/strong&gt;시킨다.&lt;/li&gt;
&lt;li&gt;1~3과정을 낚시왕이 맨 끝 자리로 이동할때까지 반복한다.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;소스코드&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;#include&amp;lt;iostream&amp;gt;
#include&amp;lt;vector&amp;gt;
#include&amp;lt;algorithm&amp;gt;

using namespace std;

int dx[] = { 0,-1,1,0,0 };
int dy[] = { 0,0,0,1,-1 };
int ans;
int map[101][101];
int curDir;
int moveCount;
int alives[10001];
struct shark
{
    int r; // 1~R
    int c; // 1~C
    int s; // 속력 0~1000
    int d; // 방향 1~4
    int z; // 크기 1~10000
    int idx; // 상어 번호
};
// 크기순 정렬
bool compare(shark&amp;amp; a, shark&amp;amp; b)
{
    return a.z &amp;lt; b.z;
}
// 번호순 정렬
bool compare2(shark&amp;amp; a, shark&amp;amp; b)
{
    return a.idx &amp;lt; b.idx;
}
int main()
{
    vector&amp;lt;shark&amp;gt; sharks;
    int r, c, m;
    cin &amp;gt;&amp;gt; r &amp;gt;&amp;gt; c &amp;gt;&amp;gt; m;
    for (int i = 1; i &amp;lt;= m; i++)
    {
        int r, c, s, d, z;
        cin &amp;gt;&amp;gt; r &amp;gt;&amp;gt; c &amp;gt;&amp;gt; s &amp;gt;&amp;gt; d &amp;gt;&amp;gt; z;
        sharks.push_back({ r,c,s,d,z,i });
        map[r][c] = i;
        alives[i] = 1;
    }
    // 낚시왕의 위치
    int cnt = 0;
    while (cnt &amp;lt; c)
    {
        // 번호순 정렬
        sort(sharks.begin(), sharks.end(), compare2);
        // 1.낚시왕이 오른쪽으로 한 칸 이동한다.
        cnt++;
        // 2. 낚시왕이 있는 열에 있는 상어 중에서 가장 땅과 가까운 상어를 잡는다.
        // 상어를 잡으면 격자판에서 잡은 상어가 사라진다.
        for (int i = 1; i &amp;lt;= r; i++)
        {
            if (map[i][cnt] != 0)
            {
                // 잡은 상어는 alive[i]를 0으로 하고 상어 크기를 답에 더함
                alives[map[i][cnt]] = 0;
                ans += sharks[map[i][cnt] - 1].z;
                map[i][cnt] = 0;
                break;
            }
        }
        // 상어 크기순 정렬
        sort(sharks.begin(), sharks.end(), compare);
        // 3. 상어가 이동한다.
        for (int i = 0; i &amp;lt; sharks.size(); i++)
        {
            if (alives[sharks[i].idx] == 1)
            {
                // 현재 상어의 위치에 저장된 값이 자기자신이면 0으로 바꿔줌
                // 아니면 자기보다 크기가 작은 상어가 자신 위치에 온것이므로 내둔다.
                if (map[sharks[i].r][sharks[i].c] == sharks[i].idx)
                {
                    map[sharks[i].r][sharks[i].c] = 0;
                }
                int movement = sharks[i].s;
                // 방향에 맞게 상어 이동시키기
                switch (sharks[i].d)
                {
                    // 위,아래방향일땐 r-1 *2 시간마다 같은 위치
                case 1:
                    movement %= ((r - 1) * 2);
                    curDir = 1;
                    moveCount = 0;
                    while (moveCount &amp;lt; movement)
                    {
                        if (sharks[i].r == 1)
                        {
                            curDir = 2;
                        }
                        else if (sharks[i].r == r)
                        {
                            curDir = 1;
                        }
                        sharks[i].r += dx[curDir];
                        moveCount++;
                    }
                    sharks[i].d = curDir;
                    map[sharks[i].r][sharks[i].c] = sharks[i].idx;
                    break;
                case 2:
                    movement %= ((r - 1) * 2);
                    curDir = 2;
                    moveCount = 0;
                    while (moveCount &amp;lt; movement)
                    {
                        if (sharks[i].r == 1)
                        {
                            curDir = 2;
                        }
                        else if (sharks[i].r == r)
                        {
                            curDir = 1;
                        }
                        sharks[i].r += dx[curDir];
                        moveCount++;
                    }
                    sharks[i].d = curDir;
                    map[sharks[i].r][sharks[i].c] = sharks[i].idx;
                    break;
                    // 좌, 우 방향일땐 c-1*2 시간마다 같은 위치
                case 3:
                    movement %= ((c - 1) * 2);
                    curDir = 3;
                    moveCount = 0;
                    while (moveCount &amp;lt; movement)
                    {
                        if (sharks[i].c == 1)
                        {
                            curDir = 3;
                        }
                        else if (sharks[i].c == c)
                        {
                            curDir = 4;
                        }
                        sharks[i].c += dy[curDir];
                        moveCount++;
                    }
                    sharks[i].d = curDir;
                    map[sharks[i].r][sharks[i].c] = sharks[i].idx;
                    break;
                case 4:
                    movement %= ((c - 1) * 2);
                    curDir = 4;
                    moveCount = 0;
                    while (moveCount &amp;lt; movement)
                    {
                        if (sharks[i].c == 1)
                        {
                            curDir = 3;
                        }
                        else if (sharks[i].c == c)
                        {
                            curDir = 4;
                        }
                        sharks[i].c += dy[curDir];
                        moveCount++;
                    }
                    sharks[i].d = curDir;
                    map[sharks[i].r][sharks[i].c] = sharks[i].idx;
                    break;
                }
            }
        }
        // 맵에 0이 아닌 상어 번호가 있다면 alives의 값 증가
        for (int i = 1; i &amp;lt;= r; i++)
        {
            for (int j = 1; j &amp;lt;= c; j++)
            {
                if (map[i][j] != 0)
                {
                    alives[map[i][j]]++;
                }
            }
        }
        // alives의 모든 값을 1을 빼서 0또는 1로 만든다.
        for (int i = 0; i &amp;lt;= m; i++)
        {
            alives[i]--;
        }
    }
    cout &amp;lt;&amp;lt; ans &amp;lt;&amp;lt; endl;
    return 0;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/tdm1223/Algorithm/blob/master/acmicpc.net/source/17143.cpp&quot;&gt;전체 소스 코드&lt;/a&gt;&lt;/p&gt;</description>
      <category>Algorithm | 알고리즘</category>
      <category>17143</category>
      <category>acmicpc</category>
      <category>algorithm</category>
      <category>BOJ</category>
      <category>낚시왕</category>
      <category>백준</category>
      <author>gyunnnnnn</author>
      <guid isPermaLink="true">https://jevlog.tistory.com/33</guid>
      <comments>https://jevlog.tistory.com/33#entry33comment</comments>
      <pubDate>Tue, 6 Dec 2022 11:35:59 +0900</pubDate>
    </item>
    <item>
      <title>자료구조 List에 대한 설명과 구현</title>
      <link>https://jevlog.tistory.com/32</link>
      <description>&lt;h2 data-ke-size=&quot;size26&quot;&gt;리스트란?&lt;/h2&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;list.png&quot; data-origin-width=&quot;340&quot; data-origin-height=&quot;106&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/booWTy/btrSN16vEB4/JGmL7eOEl07GJsJCpMeqVk/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/booWTy/btrSN16vEB4/JGmL7eOEl07GJsJCpMeqVk/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/booWTy/btrSN16vEB4/JGmL7eOEl07GJsJCpMeqVk/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbooWTy%2FbtrSN16vEB4%2FJGmL7eOEl07GJsJCpMeqVk%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;340&quot; height=&quot;106&quot; data-filename=&quot;list.png&quot; data-origin-width=&quot;340&quot; data-origin-height=&quot;106&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;ol style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;각각의 데이터가 자신의 다음 데이터(또는 이전, 다음 데이터 모두)의 위치를 가지고 있는 자료구조&lt;/li&gt;
&lt;li&gt;데이터가 메모리에서 연속적으로 위치하고 있지는 않다.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;리스트 종류&lt;/h2&gt;
&lt;ol style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;Singly Linked List - 데이터가 자신의 다음 데이터의 위치만 가지고 있다.&lt;/li&gt;
&lt;li&gt;Double Linked List - 데이터가 자신의 이전과 다음 데이터의 위치를 가지고 있다.&lt;/li&gt;
&lt;li&gt;Circular Linked List - 마지막 데이터가 처음 데이터의 위치를 가지고 있다.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;리스트 연산의 시간 복잡도&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;데이터를 추가(임의의 위치) - O(1)&lt;/li&gt;
&lt;li&gt;데이터를 제거(임의의 위치) - O(1)&lt;/li&gt;
&lt;li&gt;데이터 확인/변경 - O(N)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;리스트 구현(Singly Linked List)&lt;/h2&gt;
&lt;pre class=&quot;haskell&quot;&gt;&lt;code&gt;#include&amp;lt;iostream&amp;gt;

using namespace std;

template&amp;lt;class T&amp;gt;
class Node
{
private:
    T data; // 데이터
public:
    Node&amp;lt;T&amp;gt; * nextNode; // 다음 노드를 가리키는 포인터
    T GetData()
    {
        return data;
    }
    Node(int k) : data(k), nextNode(NULL) {}
};

// 노드 추가
template&amp;lt;class T&amp;gt;
void AppendNode(Node&amp;lt;T&amp;gt;** Head, Node&amp;lt;T&amp;gt;* NewNode)
{
    if ((*Head) == NULL)
    {
        *Head = NewNode;
    }
    else
    {
        Node&amp;lt;T&amp;gt;* Tail = (*Head);
        while (Tail-&amp;gt;nextNode != NULL)
        {
            Tail = Tail-&amp;gt;nextNode;
        }
        Tail-&amp;gt;nextNode = NewNode;
    }
}

// 노드 위치 반환
template&amp;lt;class T&amp;gt;
Node&amp;lt;T&amp;gt;* GetNodeAt(Node&amp;lt;T&amp;gt;* Head, int location)
{
    Node&amp;lt;T&amp;gt;* current = Head;
    while (current != NULL &amp;amp;&amp;amp; (--location) &amp;gt;= 0)
    {
        current = current-&amp;gt;nextNode;
    }
    return current;
}

// 노드 삭제
template&amp;lt;class T&amp;gt;
void RemoveNode(Node&amp;lt;T&amp;gt;** Head, Node&amp;lt;T&amp;gt;* Remove)
{
    if (*Head == Remove)
    {
        *Head = Remove-&amp;gt;nextNode;
    }
    else
    {
        Node* Current = *Head;
        //처음부터 지우려는 노드의 이전 노드까지 순회
        while (Current != NULL &amp;amp;&amp;amp; Current-&amp;gt;nextNode != Remove) 
        {
            Current = Current-&amp;gt;nextNode;
        }
        if (Current != NULL)
        {
            Current-&amp;gt;nextNode = Remove-&amp;gt;nextNode;
        }
    }
}

// 노드 삽입
template&amp;lt;class T&amp;gt;
void InsertNode(Node&amp;lt;T&amp;gt;* Current, Node&amp;lt;T&amp;gt;* NewNode)
{
    NewNode-&amp;gt;nextNode = Current-&amp;gt;nextNode;
    Current-&amp;gt;nextNode = NewNode;
}

// 노드 갯수 반환
template&amp;lt;class T&amp;gt;
int GetNodeCount(Node&amp;lt;T&amp;gt;* Head)
{
    int count = 0;
    Node&amp;lt;T&amp;gt;* Current = Head;
    while (Current != NULL)
    {
        Current = Current-&amp;gt;nextNode;
        count++;
    }
    return count;
}&lt;/code&gt;&lt;/pre&gt;</description>
      <category>DataStructure | 자료구조</category>
      <category>CPP</category>
      <category>LIST</category>
      <category>리스트</category>
      <author>gyunnnnnn</author>
      <guid isPermaLink="true">https://jevlog.tistory.com/32</guid>
      <comments>https://jevlog.tistory.com/32#entry32comment</comments>
      <pubDate>Mon, 5 Dec 2022 10:34:13 +0900</pubDate>
    </item>
    <item>
      <title>백준 17135. 캐슬 디펜스</title>
      <link>https://jevlog.tistory.com/31</link>
      <description>&lt;h2&gt;&lt;a href=&quot;https://www.acmicpc.net/problem/17135&quot;&gt;17135. 캐슬 디펜스&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;브루트포스 + 시뮬레이션 문제였다. &lt;/li&gt;
&lt;li&gt;문제에 써있는 내용대로 궁수 3명의 위치를 선택 후 시뮬레이션을 진행하여 풀면 된다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;풀이 과정&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;궁수 3명의 위치를 선택한다. (go 함수)&lt;/li&gt;
&lt;li&gt;적을 공격하고 적이 이동하도록 시뮬레이션해준다. (Simulate 함수)&lt;ol&gt;
&lt;li&gt;궁수는 자신으로부터 거리가 D이하인 적 중에서 가장 가까운 적을 공격한다.&lt;/li&gt;
&lt;li&gt;거리가 가까운 적이 여럿일 경우에는 가장 왼쪽에 있는 적을 공격한다.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;모든 적이 맨 아래로 이동하여 제외될때까지 반복한다.&lt;/li&gt;
&lt;li&gt;1~3 과정을 반복한다.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;소스코드&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;#include&amp;lt;iostream&amp;gt;
#include&amp;lt;algorithm&amp;gt;
#include&amp;lt;vector&amp;gt;

using namespace std;

int map[16][16];
int n, m, d;
int ch[3];
int ans;
int endLine=-1; // 최초 상태에 적이 있는 가장 높은 곳
vector&amp;lt;pair&amp;lt;int, int&amp;gt;&amp;gt; shotPos; // 궁수가 쏜 좌표를 모아논곳

// 두점의 거리를 구하는 함수
int GetDistance(int x1, int y1, int x2, int y2)
{
    int x = x1 - x2 &amp;gt; 0 ? x1 - x2 : x2 - x1;
    int y = y1 - y2 &amp;gt; 0 ? y1 - y2 : y2 - y1;
    return x + y;
}

void Simulate()
{
    int cnt = 0;
    int sum = 0;
    // 맵 복사
    int tmpMap[16][16];
    for (int i = 0; i &amp;lt; n; i++)
    {
        for (int j = 0; j &amp;lt; m; j++)
        {
            tmpMap[i][j] = map[i][j];
        }
    }

    // 맨 위의 적이 다 내려올때까지 반복
    while (cnt&amp;lt; endLine)
    {
        shotPos.clear();
        // 궁수가 먼저 공격
        for (int i = 0; i &amp;lt; 3; i++)
        {
            int shotX = 20;
            int shotY = 20;
            int tmpDist = d;
            for (int j = n - 1; j &amp;gt;= 0; j--)
            {
                for (int k = 0; k &amp;lt; m; k++)
                {
                    // 거리가 작으면 무조건 선택
                    if (tmpMap[j][k] == 1 &amp;amp;&amp;amp; 
                        GetDistance(j, k, n, ch[i]) &amp;lt; tmpDist)
                    {
                        tmpDist = GetDistance(j, k, n, ch[i]);
                        shotX = j;
                        shotY = k;
                    }
                    // 거리가 같을땐 가장 왼쪽
                    else if (tmpMap[j][k] == 1 &amp;amp;&amp;amp; 
                            GetDistance(j, k, n, ch[i]) == tmpDist)
                    {
                        if (k &amp;lt; shotY)
                        {
                            shotX = j;
                            shotY = k;
                        }
                    }
                }
            }
            // 궁수가 쏜 위치가 갱신되어있는 상태라면 추가
            if (shotX != 20 &amp;amp;&amp;amp; shotY != 20)
            {
                shotPos.push_back({ shotX,shotY });
            }
        }

        // 궁수가 쏜 포지션들에 대해 반복
        for (int i = 0; i &amp;lt; shotPos.size(); i++)
        {
            // 중복된 지점을 쐈을땐 1만 증가해야하므로 아래 조건 추가
            if (tmpMap[shotPos[i].first][shotPos[i].second] == 1)
            {
                tmpMap[shotPos[i].first][shotPos[i].second] = 0;
                sum++;
            }

        }

        // 적 이동
        for (int i = n-1; i &amp;gt;=1; i--)
        {
            for (int j = 0; j &amp;lt; m; j++)
            {
                tmpMap[i][j] = tmpMap[i - 1][j];
            }
        }
        for (int i = 0; i &amp;lt; m; i++)
        {
            tmpMap[0][i] = 0;
        }
        cnt++;
    }

    // 최댓값이라면 갱신
    ans = max(ans, sum);
}

// 궁수 3명을 놓을 위치 선택
void go(int cnt,int idx)
{
    if (cnt == 3)
    {
        Simulate();
        return;
    }

    for (int i = idx; i &amp;lt; m; i++)
    {
        ch[cnt] = i;
        go(cnt + 1, i + 1);
    }
}

int main()
{
    cin &amp;gt;&amp;gt; n &amp;gt;&amp;gt; m &amp;gt;&amp;gt; d;

    for (int i = 0; i &amp;lt; n; i++)
    {
        for (int j = 0; j &amp;lt; m; j++)
        {
            cin &amp;gt;&amp;gt; map[i][j];
            if (map[i][j] == 1 &amp;amp;&amp;amp; endLine==-1)
            {
                endLine = n-i;
            }
        }
    }

    go(0,0);
    cout &amp;lt;&amp;lt; ans &amp;lt;&amp;lt; endl;
    return 0;
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href=&quot;https://github.com/tdm1223/Algorithm/blob/master/acmicpc.net/source/17135.cpp&quot;&gt;전체 소스 코드&lt;/a&gt;&lt;/p&gt;</description>
      <category>Algorithm | 알고리즘</category>
      <category>17135</category>
      <category>BOJ</category>
      <category>백준</category>
      <category>알고리즘</category>
      <category>캐슬디펜스</category>
      <author>gyunnnnnn</author>
      <guid isPermaLink="true">https://jevlog.tistory.com/31</guid>
      <comments>https://jevlog.tistory.com/31#entry31comment</comments>
      <pubDate>Mon, 28 Nov 2022 18:41:46 +0900</pubDate>
    </item>
    <item>
      <title>const, friend, static, mutable, explicit</title>
      <link>https://jevlog.tistory.com/30</link>
      <description>&lt;h2 data-ke-size=&quot;size26&quot;&gt;const&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;code&gt;const&lt;/code&gt;는 값을 상수로 선언할 수 있도록 도와주는 키워드다.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;const&lt;/code&gt;를 앞에 붙이면 값은 &lt;b&gt;변경할 수 없게 된다.&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;const&lt;/code&gt;의 선언 유무도 &lt;b&gt;함수 오버로딩&lt;/b&gt; 조건에 해당이 된다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;cpp&quot;&gt;&lt;code&gt;class Test
{
public:
    void Func() { }
    void Func() const { }
};&lt;/code&gt;&lt;/pre&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;객체도 상수화 할 수 있다.&lt;/li&gt;
&lt;li&gt;이 객체를 대상으로는 &lt;code&gt;const&lt;/code&gt; 멤버 함수의 호출만 허용한다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;cpp&quot;&gt;&lt;code&gt;class SoSimple
{
private:
    int num;
public;
    SoSimple(int n) : num(n){ } // 생성자
    SoSimple&amp;amp; NotConstFunc(int n)
    {
        num+=n;
    }
    void ConstFunc() const // const 함수
    {
        cout &amp;lt;&amp;lt; &quot; Func() &quot; &amp;lt;&amp;lt; endl;
    }
};

const SoSimple sim(20);
sim.NotConstFunc(); // 불가능
sim.ConstFunc(); // 가능&lt;/code&gt;&lt;/pre&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;friend&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;span style=&quot;color: red;&quot;&gt;A 클래스&lt;/span&gt;가 &lt;span style=&quot;color: blue;&quot;&gt;B 클래스&lt;/span&gt;를 대상으로 &lt;code&gt;friend&lt;/code&gt; 선언을 하면, &lt;br /&gt;&lt;span style=&quot;color: blue;&quot;&gt;B 클래스&lt;/span&gt;는 &lt;span style=&quot;color: red;&quot;&gt;A 클래스&lt;/span&gt;의 &lt;code&gt;private&lt;/code&gt; 멤버에 &lt;b&gt;직접 접근&lt;/b&gt;이 가능하다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;cpp&quot;&gt;&lt;code&gt;class A
{
private:
    int num;
    friend class B;
};

class B
{
public:
    void Func(A &amp;amp;a)
    {
        cout&amp;lt;&amp;lt;&quot; a's num : &quot; &amp;lt;&amp;lt; a.num &amp;lt;&amp;lt; endl; // private 멤버에 접근 가능
    }
};&lt;/code&gt;&lt;/pre&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;code&gt;B&lt;/code&gt;에서 &lt;code&gt;A&lt;/code&gt;의 &lt;code&gt;private&lt;/code&gt;변수에 접근이 가능하다.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;friend&lt;/code&gt; 선언은 &lt;b&gt;정보은닉을 무너뜨리는 문법&lt;/b&gt;이다.
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;필요한 상황에서만 사용해야 한다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;전역 함수, 클래스의 멤버 함수를 대상으로도 &lt;code&gt;friend&lt;/code&gt; 선언이 가능하다.&lt;/li&gt;
&lt;li&gt;함수에 대해 &lt;code&gt;friend&lt;/code&gt; 선언을 해도 &lt;b&gt;기본 함수 원형의 선언&lt;/b&gt;이 포함된다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;static&lt;/h2&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;C에서의 static&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;전역 변수&lt;/b&gt;에 선언
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;선언된 파일 내&lt;/b&gt;에서만 참조를 허용하겠다는 의미&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;함수 내&lt;/b&gt;에 선언
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;한 번만 초기화&lt;/b&gt;된다.&lt;/li&gt;
&lt;li&gt;지역변수와 달리 함수를 빠져나가도 &lt;b&gt;소멸되지 않는다&lt;/b&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;const static 멤버&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;선언과 동시에 초기화&lt;/b&gt;가 가능하다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;cpp&quot;&gt;&lt;code&gt;class First
{
public:
    const static int CNT = 0;
};
cout &amp;lt;&amp;lt; First::CNT &amp;lt;&amp;lt; endl; // 이름을 통해 접근한다.&lt;/code&gt;&lt;/pre&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;static 멤버 변수(클래스 변수)&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;code&gt;static&lt;/code&gt; 변수를 생성자에서 초기화하면 안 된다.
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;객체가 생성&lt;/b&gt;될 때마다 &lt;b&gt;변수가 초기화&lt;/b&gt;되기 때문이다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;전역 변수&lt;/b&gt;로 변수를 선언 시 어디서든 접근할 수 있는데 이걸 &lt;b&gt;제한&lt;/b&gt;할 수 있다.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;static&lt;/code&gt; 멤버에 접근할 때에는 (클래스::변수)로 접근하는 것이 좋다.
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;멤버 변수에 접근하는 것 같은 오해를 없애기 위해서이다.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;ol style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;선언된 클래스의 모든 객체가 공유한다.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;public&lt;/code&gt;으로 선언이 되면, 클래스의 이름을 이용해서 호출이 가능하다.&lt;/li&gt;
&lt;li&gt;객체의 멤버로 존재하는 것이 아니다.&lt;/li&gt;
&lt;/ol&gt;
&lt;pre class=&quot;angelscript&quot;&gt;&lt;code&gt;class First
{
private:
    static int num;
};

int First::num = 0;&lt;/code&gt;&lt;/pre&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;static 멤버 함수&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;code&gt;static&lt;/code&gt; 멤버 함수 내에서는 &lt;code&gt;static&lt;/code&gt; 멤버 변수와 &lt;code&gt;static&lt;/code&gt; 멤버 함수만 호출이 가능하다.&lt;/li&gt;
&lt;li&gt;객체 생성과 아무런 관계가 없다.&lt;/li&gt;
&lt;li&gt;선언된 클래스의 &lt;b&gt;모든 객체가 공유&lt;/b&gt;한다.&lt;/li&gt;
&lt;li&gt;객체의 멤버로 존재하는 것이 아니다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;mutable&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;code&gt;const&lt;/code&gt; 함수 내에서의 값의 변경을 예외적으로 허용한다.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mutable&lt;/code&gt;의 과도한 사용은 &lt;code&gt;const&lt;/code&gt;의 선언을 의미 없게 만들어버린다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;cpp&quot;&gt;&lt;code&gt;mutable int num2;
void CopyToNum2() const
{
    num2 = num1;
}&lt;/code&gt;&lt;/pre&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;explicit&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;&lt;b&gt;암시적 변환&lt;/b&gt;을 막아준다.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;대입 연산자&lt;/b&gt;를 이용한 &lt;b&gt;객체의 생성 및 초기화&lt;/b&gt;는 불가능하다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;gcode&quot;&gt;&lt;code&gt;explicit SoSimple(const SoSimple &amp;amp;copy) : num1(copy.num1), num2(copy.num2){}
SoSimple sim2 = sim1; // 에러! 암시적 변환 불가능
SoSimple sim2(sim1); // 가능&lt;/code&gt;&lt;/pre&gt;</description>
      <category>Programming | 프로그래밍 언어/C++</category>
      <category>const</category>
      <category>explicit</category>
      <category>friend</category>
      <category>mutable</category>
      <category>static</category>
      <author>gyunnnnnn</author>
      <guid isPermaLink="true">https://jevlog.tistory.com/30</guid>
      <comments>https://jevlog.tistory.com/30#entry30comment</comments>
      <pubDate>Sun, 27 Nov 2022 00:13:57 +0900</pubDate>
    </item>
    <item>
      <title>Stack</title>
      <link>https://jevlog.tistory.com/29</link>
      <description>&lt;h2 data-ke-size=&quot;size26&quot;&gt;Stack&lt;/h2&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;stack.png&quot; data-origin-width=&quot;204&quot; data-origin-height=&quot;397&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/bklvqd/btrR8I191fN/wJVsqQggqhkJ2khz85aQv1/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/bklvqd/btrR8I191fN/wJVsqQggqhkJ2khz85aQv1/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/bklvqd/btrR8I191fN/wJVsqQggqhkJ2khz85aQv1/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fbklvqd%2FbtrR8I191fN%2FwJVsqQggqhkJ2khz85aQv1%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;204&quot; height=&quot;397&quot; data-filename=&quot;stack.png&quot; data-origin-width=&quot;204&quot; data-origin-height=&quot;397&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;ol style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;나중에 들어간 데이터가 제일 먼저 나오는 &lt;b&gt;LIFO(Last In First Out)&lt;/b&gt; 구조&lt;/li&gt;
&lt;li&gt;특정 위치(한쪽 끝)에서만 데이터를 넣거나 뺼 수 있다.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;Stack 연산의 시간복잡도&lt;/h3&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;데이터를 추가(push) -&amp;gt; O(1)&lt;/li&gt;
&lt;li&gt;데이터를 제거(pop) -&amp;gt; O(1)&lt;/li&gt;
&lt;li&gt;제일 꼭대기의 데이터를 확인(top) -&amp;gt; O(1)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;Stack 응용 사례&lt;/h3&gt;
&lt;ol style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;수식의 괄호 쌍&lt;/li&gt;
&lt;li&gt;후위 표기법&lt;/li&gt;
&lt;li&gt;DFS&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 data-ke-size=&quot;size23&quot;&gt;Stack 구현&lt;/h3&gt;
&lt;pre class=&quot;arduino&quot;&gt;&lt;code&gt;template&amp;lt;class T&amp;gt;
class Stack
{
private:
    int* stack;
    int size, top;
public:
    Stack&amp;lt;T&amp;gt;(int size) : size(size), top(-1)
    {
        stack = new T[size];
    }
    ~Stack&amp;lt;T&amp;gt;()
    {
        delete stack;
    }
    // 데이터 삽입
    bool Push(T data)
    {
        if (top &amp;lt; size - 1)
        {
            top++;
            stack[top] = data;
            return true;
        }
        else
        {
            return false;
        }
    }
    // 데이터 제거
    T Pop()
    {
        if (top &amp;gt;= 0)
        {
            return stack[top--];
        }
        else
        {
            return -1;
        }
    }
    // 맨 위 데이터 반환
    T Top()
    {
        return stack[top];
    }
    // 스택이 비었는지 확인
    bool IsEmpty()
    {
        return top == -1;
    }
};&lt;/code&gt;&lt;/pre&gt;</description>
      <category>DataStructure | 자료구조</category>
      <category>datastructure</category>
      <category>stack</category>
      <category>스택</category>
      <category>자료구조</category>
      <author>gyunnnnnn</author>
      <guid isPermaLink="true">https://jevlog.tistory.com/29</guid>
      <comments>https://jevlog.tistory.com/29#entry29comment</comments>
      <pubDate>Sat, 26 Nov 2022 21:11:53 +0900</pubDate>
    </item>
    <item>
      <title>위상 정렬</title>
      <link>https://jevlog.tistory.com/28</link>
      <description>&lt;h2&gt;TopologySort(위상정렬)&lt;/h2&gt;
&lt;h3&gt;TopologySort란?&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;순서가 정해져있는 작업을 수행해야 할 때 그 순서를 정하기 위해 사용하는 정렬 알고리즘이다.&lt;/li&gt;
&lt;li&gt;DAG(Directed Acyclic Graph : 사이클이 없는 방향 그래프)에만 적용이 가능하다.&lt;/li&gt;
&lt;li&gt;정렬의 결과는 여러가지가 나올 수 있다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;TopologySort의 시간 복잡도&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;시간 복잡도 : &lt;code&gt;O(V+E)&lt;/code&gt; (정점의 개수 + 간선의 개수)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;TopologySort의 구현&lt;/h3&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;진입차수가 0&lt;/strong&gt;인 정점을 큐에 삽입한다.&lt;/li&gt;
&lt;li&gt;큐에서 원소를 꺼내 연결된 &lt;strong&gt;모든 간선을 제거&lt;/strong&gt;한다.&lt;/li&gt;
&lt;li&gt;간선 제거 후에 &lt;strong&gt;진입차수&lt;/strong&gt;(특정한 노드가 있을때 그 노드로 들어오는 다른 노드의 개수)가 0이 된 정점을 큐에 삽입한다.&lt;/li&gt;
&lt;li&gt;큐가 빌 때까지 2~3의 과정을 반복한다.&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;모든 원소를 방문하기 전에 큐가 비었다면 사이클이 존재하는 것이다.&lt;/li&gt;
&lt;li&gt;모든 원소를 방문하고 큐가 비었다면 큐에서 꺼낸 순서가 정렬의 결과가 된다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;소스코드&lt;/h3&gt;
&lt;pre&gt;&lt;code class=&quot;language-cpp&quot;&gt;#include&amp;lt;vector&amp;gt;
#include&amp;lt;queue&amp;gt;

using namespace std;

vector&amp;lt;int&amp;gt; result;
// 인자 : 간선 모음, 진입차수 배열, 크기
void topologySort(vector&amp;lt;vector&amp;lt;int&amp;gt;&amp;gt;&amp;amp; a, vector&amp;lt;int&amp;gt;&amp;amp; inDegree, int size)
{
    queue&amp;lt;int&amp;gt; q;
    // 진입 차수가 0인 노드를 큐에 삽입
    for (int i = 0; i &amp;lt; size; i++)
    {
        if (inDegree[i] == 0)
        {
            q.push(i);
        }
    }
    // 모든 노드 반복
    for (int i = 0; i &amp;lt; size; i++)
    {
        // n개를 방문하기 전에 큐가 빈다면 사이클 발생
        if (q.empty())
        {
            return;
        }
        int front = q.front();
        q.pop();
        result.push_back(front);
        for (int i = 0; i &amp;lt; a[front].size(); i++)
        {
            int x = a[front][i];
            // 새롭게 진입차수가 0이 된 정점을 큐에 삽입
            if (--inDegree[x] == 0)
            {
                q.push(x);
            }
        }
    }
}&lt;/code&gt;&lt;/pre&gt;</description>
      <category>Algorithm | 알고리즘</category>
      <category>algorithm</category>
      <category>Sort</category>
      <category>topology</category>
      <category>위상정렬</category>
      <author>gyunnnnnn</author>
      <guid isPermaLink="true">https://jevlog.tistory.com/28</guid>
      <comments>https://jevlog.tistory.com/28#entry28comment</comments>
      <pubDate>Fri, 25 Nov 2022 13:05:22 +0900</pubDate>
    </item>
    <item>
      <title>스택으로 큐 구현하기 / 큐로 스택 구현하기</title>
      <link>https://jevlog.tistory.com/27</link>
      <description>&lt;h2 data-ke-size=&quot;size26&quot;&gt;스택과 큐의 상호 구현&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;면접 보면서 정말 많이 받은 질문 중 하나이다.&lt;/li&gt;
&lt;li&gt;처음 질문받았을 때 어버버 하면서 아무것도 대답 못했던 기억이 있고 두 번째 받았을 땐 자신 있게 작성하다 틀린 기억이 있는 부끄러운 기억이 있다.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;스택으로 큐 구현하기&lt;/h2&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;이론은 간단하다. 2개의 스택을 이용해서 구현하면 된다.&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;figure class=&quot;imageblock alignCenter&quot; data-ke-mobileStyle=&quot;widthOrigin&quot; data-filename=&quot;stackQueue.png&quot; data-origin-width=&quot;394&quot; data-origin-height=&quot;469&quot;&gt;&lt;span data-url=&quot;https://blog.kakaocdn.net/dn/cPEibb/btrR6d64AoC/NNdR2V4cD36ovvuyqPWRt0/img.png&quot; data-phocus=&quot;https://blog.kakaocdn.net/dn/cPEibb/btrR6d64AoC/NNdR2V4cD36ovvuyqPWRt0/img.png&quot;&gt;&lt;img src=&quot;https://blog.kakaocdn.net/dn/cPEibb/btrR6d64AoC/NNdR2V4cD36ovvuyqPWRt0/img.png&quot; srcset=&quot;https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&amp;fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcPEibb%2FbtrR6d64AoC%2FNNdR2V4cD36ovvuyqPWRt0%2Fimg.png&quot; onerror=&quot;this.onerror=null; this.src='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png'; this.srcset='//t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png';&quot; loading=&quot;lazy&quot; width=&quot;394&quot; height=&quot;469&quot; data-filename=&quot;stackQueue.png&quot; data-origin-width=&quot;394&quot; data-origin-height=&quot;469&quot;/&gt;&lt;/span&gt;&lt;/figure&gt;
&lt;/p&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;Enqueue(삽입)&lt;/p&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;그냥 첫 번째 스택인 a에 추가만 하면 된다.&lt;/li&gt;
&lt;/ul&gt;
&lt;p data-ke-size=&quot;size16&quot;&gt;Dequeue(제거)&lt;/p&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;두 번째 스택인 b가 비어있지 않다면 b에 있는 값을 제거(반환) 하면 된다.&lt;/li&gt;
&lt;li&gt;두 번째 스택인 b가 비었다면 첫 번째 스택인 a가 빌 때까지 a를 pop(제거)하면서 두 번째 스택인 b에 push(추가) 한다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;cpp&quot;&gt;&lt;code&gt;class StackQueue
{
public:
    stack&amp;lt;int&amp;gt; a;
    stack&amp;lt;int&amp;gt; b;
    void Enqueue(int data)
    {
        a.push(data);
    }
    int Dequeue()
    {
        if (b.empty())
        {
            while (!a.empty())
            {
                b.push(a.top());
                a.pop();
            }
        }
        int data = b.top();
        b.pop();
        return data;
    }
};
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 data-ke-size=&quot;size26&quot;&gt;큐로 스택 구현하기&lt;/h2&gt;
&lt;ul style=&quot;list-style-type: disc;&quot; data-ke-list-type=&quot;disc&quot;&gt;
&lt;li&gt;큐로 스택을 구현하는 경우에도 큐 2개를 이용하면 스택을 구현할 수 있다.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class=&quot;cpp&quot;&gt;&lt;code&gt;#include&amp;lt;queue&amp;gt;
using namespace std;
class QueueStack
{
public:
    queue&amp;lt;int&amp;gt; a;
    queue&amp;lt;int&amp;gt; b;
    void Push(int data)
    {
        if (a.empty())
        {
            a.push(data);
        }
        else
        {
            while (!a.empty())
            {
                b.push(a.front());
                a.pop();
            }
            a.push(data);
            while (!b.empty())
            {
                a.push(b.front());
                b.pop();
            }
        }
    }
    int Pop()
    {
        int data = a.front();
        a.pop();
        return data;
    }
};&lt;/code&gt;&lt;/pre&gt;</description>
      <category>DataStructure | 자료구조</category>
      <category>datastructure</category>
      <category>queue</category>
      <category>stack</category>
      <category>스택</category>
      <category>자료구조</category>
      <category>큐</category>
      <author>gyunnnnnn</author>
      <guid isPermaLink="true">https://jevlog.tistory.com/27</guid>
      <comments>https://jevlog.tistory.com/27#entry27comment</comments>
      <pubDate>Fri, 25 Nov 2022 10:02:48 +0900</pubDate>
    </item>
  </channel>
</rss>