我有一个包含 6 个时间点的示例程序,使用 high_resolution_clock::now()
来自标准chrono
header 。我将它们中的每一个差异化为 3 个差异并将它们归类为 auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
到微秒。
我有另一个名为 durations 的变量,其分配如下:auto durations = std::make_tuple(duration1,duration2,duration3);
包含之前的时间点差异。
我必须将这个元组插入一个 vector ,所以我引入了 std::vector<std::tuple<std::chrono::microseconds,std::chrono::microseconds,std::chrono::microseconds>> list;
但是在使用 list.push_back(durations);
我得到一个错误:
prog.cpp: In function 'int main()':
prog.cpp:36:29: error: no matching function for call to 'std::vector<std::tuple<std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> >, std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> >, std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> > > >::push_back(std::tuple<long long int, long long int, long long int>&)'
list.push_back(durations);
我试图搜索关于 std::chrono::microseconds
的内容及其他std::chrono::duration
东西here但未能成功解决问题。
我知道这与我对类型系统的疏忽有关,但我无法找到该错误。任何帮助将不胜感激,这里是 ideone link .
#include <iostream>
#include <chrono>
#include <vector>
#include <tuple>
using namespace std;
using namespace std::chrono;
void function()
{
long long number = 0;
for( long long i = 0; i != 2000000; ++i )
{
number += 5;
}
}
int main()
{
high_resolution_clock::time_point t1 = high_resolution_clock::now();
high_resolution_clock::time_point t3 = high_resolution_clock::now();
high_resolution_clock::time_point t5 = high_resolution_clock::now();
function();
high_resolution_clock::time_point t2 = high_resolution_clock::now();
high_resolution_clock::time_point t4 = high_resolution_clock::now();
high_resolution_clock::time_point t6 = high_resolution_clock::now();
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>( t4 - t3 ).count();
auto duration3 = std::chrono::duration_cast<std::chrono::microseconds>( t6 - t5 ).count();
auto durations = std::make_tuple(duration1,duration2,duration3);
std::vector<std::tuple<std::chrono::microseconds,std::chrono::microseconds,std::chrono::microseconds>> list;
list.push_back(durations);
cout << duration1 << " -- "<< duration2 << " -- "<< duration3 << " -- ";
return 0;
}
最佳答案
您已经创建了一个包含 3 个整数的元组,并且您正在尝试将它添加到一个包含 3 个持续时间的 vector 。
I take differences b/w each of them resulting in 3 differences and caste them as
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
to microseconds.
你为什么调用 count()
关于执行 duration_cast
后的持续时间转换为微秒?
只需将值保持为 microseconds
对象,您可以将它们添加到 vector 中:
auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 );
auto duration2 = std::chrono::duration_cast<std::chrono::microseconds>( t4 - t3 );
auto duration3 = std::chrono::duration_cast<std::chrono::microseconds>( t6 - t5 );
关于c++ - 没有用于调用 std::vector<std::tuple> push_back 的匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31408460/