Leetcode

LeetCode 1114. Print in Order [C++/ promise]

치킨먹고싶어요 2022. 6. 3. 15:04

https://leetcode.com/problems/print-in-order/

 

Print in Order - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

코드:

class Foo {
    promise<void> one, two;
public:
    Foo() {
    }
    void first(function<void()> printFirst) {
        printFirst();
        one.set_value();
    }
 
    void second(function<void()> printSecond) {
        one.get_future().get();
        printSecond();
        two.set_value();
    }
 
    void third(function<void()> printThird) {
        two.get_future().get();
        printThird();
    }
};
cs

풀이 :

 

class Foo {
    promise<void> one, two; // 나중에 실행 시키기로 약속합니다
public:
    Foo() {
    }
    void first(function<void()> printFirst) {
        printFirst();
        one.set_value(); // 약속을 이행하여 실행 시킵니다
    }
 
    void second(function<void()> printSecond) {
        one.get_future().get(); // 약속을 실행 시킨 것을 받습니다
        printSecond();
        two.set_value(); // 나중에 실행 시키기로 약속합니다
    }
 
    void third(function<void()> printThird) {
        two.get_future().get();  // 약속을 실행 시킨 것을 받습니다
        printThird();
    }
};
cs