如何在 C++ 中为高分辨率时钟声明变量?

How to declare a variable for high resolution clock in C++?

在此处的示例中:https://en.cppreference.com/w/cpp/chrono/high_resolution_clock/now

他们用auto声明了时钟时间点。

auto start = std::chrono::high_resolution_clock::now();

医生说的 returns 'A time point representing the current time.'

但我不确定如何在下面的代码中声明,因为我习惯于在函数开头声明变量,而我不知道将其声明为什么。此处的代码已被简化以表明我的意思。我为 ??? 添加了什么?

我已经在那里尝试 auto 但编译器不允许。 auto orderRecvedTime; 给我这个错误:

error: non-static data member declared with placeholder 'auto'
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <string.h>
//#include "load_symbol.h"
//#include "check_symbol.h"
#include "windows.h"
#include <vector>
#include <chrono>
using namespace std;

 

class order {
  private:
    string orderID;
    ???    orderRecvedTime;
    char   buysell;
    string symbol;
    double price;
    int    qty;

  public:
    void newOrder(string &_orderID, char &_buysell, string &_symbol, double &_price, int &_qty){
        orderID = _orderID;
        buysell = _buysell;
        symbol = _symbol;
        price = _price;
        qty = _qty;
        orderRecvedTime = std::chrono::high_resolution_clock::now();
    }
  
};



int main() {
    cout << "!!!Hello once more" << endl; // prints !!!Hello once more

    vector<order> thebook;
    string user_order = "";

    string done = "done trading";
    string orderID;
    string orderaction;
    string orderRecvedTime;
    char buysell;
    string symbol;
    double price;
    int qty;

    while (user_order.compare(done) != 0) {
        cout << "enter order"<< endl;
        getline(cin, user_order);

        stringstream lineStream(user_order);
        lineStream >>orderaction>>orderID>> buysell >> symbol >> price>> qty;
 
        order user_order;
        if (orderaction.compare("D") == 0) {
            cout << "you are making a new order."<< endl;
            user_order.newOrder(orderID, buysell,symbol,price,qty);
            thebook.push_back(user_order);
        }
    }
}

从调试器消息中提取它,但如果我这样声明它编译。

std::chrono::_V2::system_clock::time_point orderRecvedTime;

std::chrono::high_resolution_clock::time_point orderRecvedTime;

实际上,high_resolution_clocksystem_clocksteady_clock 的类型别名,因此我的建议是选择其中任何一个来获得便携体验。

  • system_clock就像一只手表。它可以告诉你现在几点了。
  • steady_clock 就像一个秒表。它非常适合计时,但不知道一天中的时间。