C++ C++;第二个计数器

C++ C++;第二个计数器,c++,timer,counter,C++,Timer,Counter,我创建了一个函数,它在用户选择后计算秒数。这一切都是可行的,但能做得更聪明、更有效率吗?因为它看起来很沉很慢。有解决这个问题的库吗?或者我们怎样才能避开它 这是我的密码: #include <ctime> #include <iomanip> #include <iostream> using namespace std; int main() { double a,c, x,b; int nutid=0; cout<&l

我创建了一个函数,它在用户选择后计算秒数。这一切都是可行的,但能做得更聪明、更有效率吗?因为它看起来很沉很慢。有解决这个问题的库吗?或者我们怎样才能避开它

这是我的密码:

#include <ctime>
#include <iomanip>
#include <iostream>

using namespace std;

int main() {
    double a,c, x,b;

    int nutid=0;

    cout<<"Please enter a number: ";
    cin>>a;
    x = time(0);
    c = a-1;

    while (true) {
        if (!cin) {
            cout<<"... Error";
            break;
        }
        else {
            b=time(0)-x;

            if(b>nutid){
                cout<<setprecision(11)<<b<<endl;
                nutid = b+c;
            }
        }
    }

    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
双a,c,x,b;
int nutid=0;
库塔;
x=时间(0);
c=a-1;
while(true){
如果(!cin){

cout您可以使用库
测量时间(因为
c++11

例如:

#include <iostream>
#include <chrono>
using namespace std;
using namespace chrono;

int main() {
    auto start = high_resolution_clock::now();

    // your code here

    auto end = high_resolution_clock::now();
    // you can also use 'chrono::microseconds' etc.
    cout << duration_cast<seconds>(end - start).count() << '\n';
}
#包括
#包括
使用名称空间std;
使用名称空间计时;
int main(){
自动启动=高分辨率时钟::现在();
//你的代码在这里
自动结束=高分辨率时钟::现在();
//您还可以使用'chrono::microseconds'等。

不能使用
std::chrono
查看这里的参考资料在循环的每次迭代中,你不使用
sleep(1)
吗?哦!谢谢-没有想到只使用sleep af计数方法:D非常感谢