簡體   English   中英

沒有用於調用std :: vector的匹配函數 <std::tuple> 推回

[英]no matching function for call to std::vector<std::tuple> push_back

我有一個包含6個時間點的示例程序,使用來自標准chrono標頭的high_resolution_clock::now() 我對它們各自進行差異b / w導致3個差異,並將它們作為auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count(); 到微秒。

我有另一個名為durations的變量,其分配如下: 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); 我得到一個錯誤:

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::microsecondsstd::chrono::duration的東西在這里 ,但沒有成功地糾正問題。

我知道這與我對類型系統的疏忽有關,但我無法找到該錯誤。 任何幫助將不勝感激,這里是ideone 鏈接

#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個持續時間的向量中。

我對它們各自進行差異b / w導致3個差異,並將它們作為auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count(); 到微秒。

在執行duration_cast轉換為微秒后,為什么要在持續時間內調用count()

只需將值保存為microseconds對象,就可以將它們添加到向量中:

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 );

std::chrono::microseconds::count()類型不是std::chrono::microseconds ,它是一些帶符號的整數類型。

auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
//decltype(duration1) is not std::chrono::microseconds

因此,您不能將duration*n*變量用於期望microsecond值的向量。

修復很簡單,只需推遲您的count調用,直到您嘗試打印內容為止。

原因很簡單:不調用count

std::chrono::microseconds是(在你的情況下) std::chrono::duration<long long int, std::ratio<1ll, 1000000ll> >的類型的typedef。 這也是你從std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 )

但是,這不是您分配給duration1 您正在為該類型分配調用count函數的結果。 並且返回刻度數作為數字(標准庫中的long long int ),而不是duration

你得到的類型不匹配。

auto duration1 = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count()

實際上在你的情況下給你很long long而不是std::chrono::microseconds 你可以通過使用decltype()和更改來解決這個問題

std::vector<std::tuple<std::chrono::microseconds,std::chrono::microseconds,std::chrono::microseconds>> list;

std::vector<decltype(durations)> list;

實例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM