gpt4 book ai didi

c++ - 错误:尚未声明 'high_resolution_clock'

转载 作者:行者123 更新时间:2023-12-02 09:59:16 25 4
gpt4 key购买 nike

我在Windows 10上使用g++版本8.1.0,但仍在尝试编译时

auto start=high_resolution_clock::now();
rd(n);
auto stop=high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop-start);
cout<<duration.count()<<endl;
我得到的错误
error: 'high_resolution_clock' has not been declared
auto start=high_resolution_clock::now();
^~~~~~~~~~~~~~~~~~~~~
我已经包括了chrono和time.h

最佳答案

您需要在std::chrono::high_resolution_clockmicroseconds前面指定duration_cast命名空间限定符,例如:

#include <chrono>

auto start = std::chrono::high_resolution_clock::now();
rd(n);
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop-start);
std::cout << duration.count() << std::endl;
否则,您可以改用 using语句,例如:
#include <chrono>
using namespace std::chrono;

auto start = high_resolution_clock::now();
rd(n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop-start);
std::cout << duration.count() << std::endl;
要么:
#include <chrono>
using std::chrono::high_resolution_clock;
using std::chrono::microseconds;
using std::chrono::duration_cast;

auto start = high_resolution_clock::now();
rd(n);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop-start);
std::cout << duration.count() << std::endl;

关于c++ - 错误:尚未声明 'high_resolution_clock',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63533235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com