Re: [問題] stl priority_queue less<MyClass*> ?
簡單的方法是用 boost Pointer Container Library
http://www.boost.org/doc/libs/1_35_0/libs/ptr_container/doc/reference.html
┌────────────────────────────────────┐
|boost::ptr_vector<MyClass> pv ; // declare with non-pointer type │
|pv.push_back(new MyClass(...)) ; // push with pointer │
|pv[0].func() ; // use as reference │
|pv.sort() ; // sort with MyClass's < operator │
└────────────────────────────────────┘
另外一個方案是寫個泛型的 pointer comparing adaptor,以後可以複用。
┌────────────────────────────────────┐
|template < typename T > │
|struct LessByDereference { │
| bool operator < () (const T * const a, const T * const b) const { │
| return *a < *b ; │
| } │
|} ; │
|std::vector<MyClass> v ; │
|std::sort(v.begin(), v.end(), LessByDereference<MyClass>()) ; │
└────────────────────────────────────┘
不過我不知道上面的 code 有沒有錯,所以不要照抄 =,=
--
ㄍㄊㄇㄉ半夜一點半外面還有人大吵大鬧吵的我睡不著只好起來爬文 = =
老天保佑那個男的明天出門給車撞死,算了,半死不活就好,
如果他是程式設計師保佑他遇到海森堡蟲,法客遊。
--
To iterate is human, to recurse, divine.
遞迴只應天上有, 凡人該當用迴圈. L. Peter Deutsch
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.160.115.106
→
11/26 01:55, , 1F
11/26 01:55, 1F
→
11/26 02:08, , 2F
11/26 02:08, 2F
→
11/26 02:08, , 3F
11/26 02:08, 3F
推
11/26 14:12, , 4F
11/26 14:12, 4F
→
11/26 20:32, , 5F
11/26 20:32, 5F
推
11/26 22:03, , 6F
11/26 22:03, 6F
※ yoco315:轉錄至看板 Test 11/26 23:14
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章