【问题标题】:How to declare a variable for high resolution clock in C++?如何在 C++ 中为高分辨率时钟声明一个变量?
【发布时间】:2020-06-04 02:37:55
【问题描述】:

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

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

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

文档说它返回“代表当前时间的时间点”。

但是我不确定如何在下面的代码中声明,因为我习惯于在函数的开头声明变量并且我不知道将其声明为什么。代码已在此处简化以说明我的意思。我要为??? 写什么?

我已经在那里尝试过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);
        }
    }
}

【问题讨论】:

  • Google for high_resolution_clock::now,您会看到带有类型的描述(单词 static 不是类型的一部分)。
  • 你的意思是:std::chrono::time_point?
  • 我尝试使用std::chrono::time_point orderRecvedTime,但编译器出错:``` 错误:在没有参数列表的情况下无效使用模板名称'std::chrono::time_point' ```
  • @D.Zou 您链接的页面顶部有now() 的声明。 staticnow() 之间的所有内容都是返回值的类型:std::chrono::time_point&lt;std::chrono::high_resolution_clock&gt;
  • auto 通常很有用。

标签: c++ chrono high-resolution-clock


【解决方案1】:
std::chrono::high_resolution_clock::time_point orderRecvedTime;

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      相关资源
      最近更新 更多