簡體   English   中英

錯誤C2893:無法專門化功能模板&#39;unknown-type std :: less <void> :: operator()(_ Ty1 &amp;&amp;,_ Ty2 &amp;&amp;)const&#39;

[英]error C2893: Failed to specialize function template 'unknown-type std::less<void>::operator ()(_Ty1 &&,_Ty2 &&) const'

我編寫了一個函數,該函數將采用點的向量,點是一個結構,並將使用穩定排序對其進行排序,但出現以下錯誤:

    Error   1   error C2893: Failed to specialize function template 'unknown-type std::less<void>::operator ()(_Ty1 &&,_Ty2 &&) const'

那是我的程序應該做的:

    in:
    5 8
    3 6
    10 9
    8 11
    7 4

並顯示:

    out:
    3 6
    5 8
    7 4
    8 11 
    10 9

這是我的代碼:

    #include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

struct points
{
    int a, b;

};

int main()
{

    int nbrDeLignes = 0;
    cin >> nbrDeLignes;

    vector<points> tab(nbrDeLignes);
    for (int i = 0; i < nbrDeLignes; i++)
    {
        points p;

        cin >> p.a >> p.b;
        tab.push_back(p);
    }

    //stable_sort(tab.begin(), tab.end());

    for (const points &point : tab)
    {
        cout << point.a << " " << point.b << endl;
    }

    return 0;
}

請任何幫助 ;

stable_sort不知道如何對struct進行排序,因為沒有為它定義比較運算符。 您要么需要將一個points設為一個class並覆蓋<運算符,要么需要為stable_sort提供比較功能。

bool compare_points(point p1, point p2)
{
    return p1.a < p2.a;
}

stable_sort(tab.begin(), tab.end(), compare_points);

那么,你怎么想訂購你的觀點? 您如何知道一個點在排序時是否應該先於另一個點? 您是通過x坐標訂購的還是什么? 您需要告訴編譯器!

換句話說,您需要提供一個operator <

暫無
暫無

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

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