I have a sample program containing 6 timepoints using high_resolution_clock::now()
from standard chrono
header. 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.
I have another variable named durations which is assigned as follows: auto durations = std::make_tuple(duration1,duration2,duration3);
containing previous time-point differences.
I have to push this tuple into an vector, so I have introduced std::vector<std::tuple<std::chrono::microseconds,std::chrono::microseconds,std::chrono::microseconds>> list;
However on using list.push_back(durations);
I get an error as :
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);
I tried to search about std::chrono::microseconds
and other std::chrono::duration
stuff here but wasn't successful in rectifying the problem.
I know this has something to do with my negligence of type system, but I'm unable to locate that error. Any help would be appreciated, & here is 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;
}
You have created a tuple of 3 integers and you're trying to add it to a vector of 3 durations.
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.
Why are you calling count()
on the durations after doing the duration_cast
to convert to microseconds?
Just keep the values as microseconds
objects and you can add them to the 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 );
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments