std::chrono and cout

后端 未结 4 782
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 10:31

I have a stupid problem. I try to switch to the c++11 headers and one of those is chrono. But my problem is that I cant cout the result of time operations. For example:

4条回答
  •  心在旅途
    2020-12-10 10:59

    As others have noted, you can call the count() member function to get the internal count.

    I wanted to add that I am attempting to add a new header: to this library. It is documented here. The main advantage of over just using count() is that the compile-time units are printed out for you. This information is of course obtainable manually, but it is much easier to just have the library to it for you.

    For me, your example:

    #include 
    #include 
    
    int main()
    {
        auto t = std::chrono::high_resolution_clock::now();
        std::cout << t.time_since_epoch() << '\n';
    }
    

    Outputs:

    147901305796958 nanoseconds
    

    The source code to do this is open source and available at the link above. It consists of two headers: and , and 1 source: chrono_io.cpp.

    This code should be considered experimental. It is not standard, and almost certainly will not be standardized as is. Indeed preliminary comments from the LWG indicate that they would prefer the default output to be what this software calls the "short form". This alternative output can be obtained with:

    std::cout << std::chrono::duration_fmt(std::chrono::symbol)
              << t.time_since_epoch() << '\n';
    

    And outputs:

    147901305796958 ns
    

提交回复
热议问题