我正在做這個問題,我們需要對單個陣列中的 k 個排序陣列進行排序,編譯器拋出一個錯誤:
“prog.cpp:20:23: 錯誤:預期)在 & token struct mycomp(triplet &t1, triplet &t2){ 之前”
我是初學者,有人可以幫助我了解問題所在。
``代碼```
struct triplet{
int val; int apos; int valpos;
};
struct mycomp(triplet &t1 , triplet &t2){
bool operator(triplet &t1 , triplet &t2){
return t1.val<t2.val;
}
};
class Solution
{
public:
//Function to merge k sorted arrays.
vector<int> mergeKArrays(vector<vector<int>> arr, int K)
{
//code here
vector<int> v;
priority_queue<triplet , vector<triplet> ,mycomp) pq;
for(int i=0; i<k; i ){
triplet t = {arr[i][0] , i ,0}
pq.push_back(t);
}
while(pq.empty()==false){
triplet t = pq.top(); pq.top();
v.push_back(t.val);
int ap == curr.apos; int vp = curr.valpos;
if(vp 1<arr[ap].size()){
triplet t = (arr[ap][vp 1] , ap , vp 1);
pq.push(t);
}
}
return v;
}
};
uj5u.com熱心網友回復:
您給定的程式中還有許多其他錯誤/錯誤。你提到的那個是因為mycomp是一個結構,所以在撰寫它的定義時你不能將引數傳遞給它(因為它不是一個函式定義)。可以通過將mycomp定義更改為以下內容來修復此特定錯誤:
//mycomp is a struct and not a function so don't pass parameters
struct mycomp{
bool operator()(triplet &t1 , triplet &t2)//note the parenthesis () i have added to overload operator()
{
return t1.val<t2.val;
}
};
另外關于如何std::priority_queue正確使用你可以參考這個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/340389.html
下一篇:Ksum的演算法總結
