#include <iostream>
#include <string>
#include <functional>
class Button {
private:
std::function<void()> onClick;
public:
void setCallback(std::function<void()> cb) {
onClick = cb;
}
void press() {
if (onClick) {
onClick();
}
}
};
int main() {
Button btn;
btn.setCallback([]() {
std::cout << "Action Executed!" << std::endl;
});
int N;
if (!(std::cin >> N)) return 0;
for (int i = 0; i < N; ++i) {
std::string cmd;
std::cin >> cmd;
if (cmd == "PRESS") {
btn.press();
}
}
return 0;
}
Explanation & Logic Summary
std::function: Provides a generic way to store and invoke any callable object.Button class triggers the event, while main defines the response.Firmware Relevance & Real-World Context
Input
2 PRESS IDLE
Expected Output
Action Executed!