如何在 C++ 中添加定时器?

How to add timer in c++?

我正在制作一个带有登录程序的 C++ 项目。我想添加计时器,如果用户输入登录详细信息错误 3 次,则用户必须等待 10 分钟。谁能推荐一个方法?

您不需要计时器,但您应该在以用户名为键的地图中记录第三次尝试时的时间。

每次用户尝试输入密码时,您应该检查他是否已被地图列入黑名单,并向他发出警告消息。

通过这种方法,您允许不同的用户在同一会话中获得访问权限。

您可以使用 std::chrono::steady_clock.

#include <chrono>

// ...

std::chrono::steady_clock::time_point timer_start;

// start the timer when you want
timer_start = std::chrono::steady_clock::now();

    
// then check if the time has passed later:
std::chrono::duration time_passed;
    
time_passed = std::chrono::steady_clock::now() - timer_start;

if(time_passed < std::chrono::minutes(10)) {
    // not allowed in yet        
}

您不需要计时器。你可以延迟一下。
这是它在 windows

上的工作方式
#include "windows.h"
Sleep(5000);

以及如何 linux

#include <unistd.h>
sleep(5);