150. Basic Lambda Callback

In embedded drivers, notifying the application about hardware events (such as a button press) typically requires a callback mechanism. Legacy C implementations often rely on function pointers, which can be cumbersome. Modern C++ allows the use of std::function and lambda expressions to define callbacks inline, improving readability and maintainability.

Your task is to implement a class named Button that supports a callback mechanism using modern C++ features.

The Button class must:

  • Contain a private member of type std::function<void()> to store a callback.
  • Provide a method void setCallback(std::function<void()> cb) to register the callback.
  • Provide a method void press() that executes the stored callback only if it is valid.

In the main function, you must register a lambda expression as the callback.
When invoked, this lambda must print:

Action Executed!

followed by a newline.

Program Flow

  1. Instantiate a Button object.
  2. Define a lambda expression that prints "Action Executed!" followed by a newline.
  3. Pass the lambda to button.setCallback(...).
  4. Read an integer N representing the number of commands.
  5. Loop N times:
    • Read a string command.
    • If the command is "PRESS", call button.press().
    • Ignore all other commands.

Input Format

  • First line: Integer N (1 ≤ N ≤ 20)
  • Next N lines: A string command ("PRESS" or any other string)

Input is provided via standard input (stdin).

Output Format

  • For each "PRESS" command, output:

    Action Executed!
    

    followed by a newline.

  • No output is produced for other commands.

Example

Input

2
PRESS
IDLE

Output

Action Executed!

Constraints

  • Must include the <functional> header.
  • Must use a lambda expression in main (no free functions).
  • Callback signature must be void().

 

 

 

Loading...

Input

2 PRESS IDLE

Expected Output

Action Executed!