如何在C ++中为高分辨率时钟声明变量? [英] How to declare a variable for high resolution clock in C++?
问题描述
在此处的示例中: https://en.cppreference.com/w/cpp/chrono/high_resolution_clock/now
他们用auto
声明了时钟时间点.
They declared the clock time point with auto
.
auto start = std::chrono::high_resolution_clock::now();
医生说它返回代表当前时间的时间点".
and the doc says it returns 'A time point representing the current time.'
但是我不确定如何在下面的代码中进行声明,因为我习惯于在函数的开头声明变量,并且不知道将其声明为什么.这里的代码已简化以显示我的意思.我要为???
做什么?
But I am not sure how do declare in my code below because I am used to declaring the variables at the beginning of the function and I don't know what to declare it as. Code has been simplified here to show what I mean. What do I put for the ???
?
我已经在那里尝试了auto
,但是编译器不允许这样做. auto orderRecvedTime;
给我这个错误:
I already tried auto
there but the compiler won't allow it. auto orderRecvedTime;
gives me this error:
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::high_resolution_clock::time_point orderRecvedTime;
在实践中,high_resolution_clock
是system_clock
或steady_clock
的类型别名,所以我的建议是选择其中之一,以提供可移植的体验.
In practice, high_resolution_clock
is a type alias for either system_clock
or steady_clock
, so my advice is to choose either one of those instead for a portable experience.
-
system_clock
就像手表.它可以告诉您现在几点了. -
steady_clock
就像秒表.对时间安排计时确实很不错,但实际上却不知道一天中的时间.
system_clock
is like a watch. It can tell you what time it is.steady_clock
is like a stop watch. It is really good for timing things, but doesn't really know the time of day.
这篇关于如何在C ++中为高分辨率时钟声明变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!