看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: Win10, Linux, ...) 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) 不好意思,我是在刷題的時候遇到問題 可能關鍵字下錯所以一直找不太到答案,所以想在這裡求助大神 一般來說 定義 priority_queue<>時,第三個參數放的是compare 在刷題時,模板是這樣子 class solution{ // 裡面會有題目自訂的funciton // 若是我在題目內宣告priority_queue,且要用到符合需求的compare時, // 我會在該class外面自行定義 vector<int> mergeTwoSortedArray(vector<int> a, vector<int> b) { // 隨便假設 priority_queue<int, vector<int>, compare> pq; } }; // ex class compare{ bool operator()(const int &a, const int &b) { return a > b; } }; 可是我現在需要在class solution題目給的function內去更改compare, 因為我需要使用到題目給的input,不單單只是整數,說不定要用到a的size之類的(假設) 補充說明(Supplement): 抱歉,第一次發文,若有什麼不妥或不符合規定請告知,會改進 謝謝各位的幫忙。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.42.134.69 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1555582666.A.B38.html
Feis: 在建構時傳進去就好,範例有: http://bit.ly/2VRHqvR fifth 04/18 18:39
a29831968: Thank you so much! 等等下班試試04/18 18:54
https://i.imgur.com/iP93jph.jpg
有點奇怪,我不覺得有寫的不一樣 ※ 編輯: a29831968 (42.73.37.77), 04/18/2019 22:51:16
loveme00835: 當然有不一樣, template 接收的只有 type/non-type 04/18 23:28
loveme00835: argument, 你給一個 runtime 才能建立的 object 當然 04/18 23:29
loveme00835: 編不過 04/18 23:29
mmmmei: Ctor的第三個參數傳class的名字 不是method 04/19 03:51
LPH66: 應該說你不能傳成員函式進去 (因為成員函式有隱藏的 this) 04/19 07:29
wawi2: Comparator 04/19 11:22
xavier13540: std::priority_queue的第三個模板參數是定義小於的 04/19 16:09
xavier13540: typename 要先生出這個typename的instance才能比較 04/19 16:11
xavier13540: 做法就是把這個typename的ctor的參數作為std:: 04/19 16:12
xavier13540: priority_queue的ctor的參數 04/19 16:12
xavier13540: 以你的例子來說就是 priority_queue<pair<int, int>, 04/19 16:16
xavier13540: vector<pair<int, int>>, compare> pq(intervals); 04/19 16:17
xavier13540: 抱歉 我查了一下沒有這種用法 看來還是用lambda好了 04/19 16:24
xavier13540: 把上面的pq(intervals)改成pq(compare(intervals)) 04/19 16:26
adrianshum: 要傳成員函式請愛用bind 04/21 19:00