如何限制cin中的输入时间限制? [英] How to restrict input time limit in cin?
本文介绍了如何限制cin中的输入时间限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个代码段,如果您愿意,它会等待一段时间(例如3秒).输入.... fine
但是,如果您在3秒钟内未输入任何内容,则该变量应获取一些默认值(由我们设置).
I want to create a code snippet which waits for some time (say 3 seconds) if you give
input....fine
But if you didn't give input in 3 seconds, the variable should fetch some default value (set by us).
如何制作这样的代码段?
How can I make such a snippet ?
推荐答案
也许此代码有帮助:
#include <iostream>
#include <thread>
#include <chrono>
int getVal(int defaultValue)
{
int val=0;
std::thread t1([&](){
std::cin>>val;
});
std::this_thread::sleep_for(std::chrono::seconds(3));
t1.detach();
if(val==0)
val=defaultValue;
return val;
}
int main()
{
std::cout<<getVal(123);
}
这篇关于如何限制cin中的输入时间限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文