[問題] 用boost lambda 代替stl set的functor

看板C_and_CPP (C/C++)作者時間14年前 (2011/10/14 01:25), 編輯推噓0(0011)
留言11則, 2人參與, 最新討論串1/1
假設我有如下Class 內部存放一組bitset template <unsigned int T> class Unit { public: Unit(int n):this->code_(n){} int value(){ return (int)this->code_->to_ulong();} private: bitset<T> code_; }; 而在main裡我先用shared_ptr將Unit包起來, 再用stl::set來存放他,確保Unit會依照其直排序, 並另外自己寫一個functor傳進去當比較函數。 //comparator of Unit class template <unsigned int T> struct compare { bool operator()(const shared_ptr<Unit<T> >& a, const shared_ptr<Unit<T> >& b) { return a->value() < b->value(); } }; int main() { set<shared_ptr<Unit<5> >, compare<5> > myset; } 現在想請問一下,若是我不想自己額外再去寫一個compare的functor 而想直接利用boost lambda來替換掉compare<5>,該怎麼下手才好-_- 試過用bost::bind(&shared_ptr<Unit<5> >::get,_1)這樣的方向去組裝 但仍然會報錯說 main.cpp: error: Semantic Issue: Template argument for template type parameter must be a type 試一個晚上試不出來, 謝謝! -- ※ 發信站 :批踢踢實業坊(ptt.cc) ◆ From: 111.250.64.140 ※ 編輯: GreatShot 來自: 111.250.64.140 (10/14 01:48)

10/14 03:18, , 1F
很想推 Boost.Compose, 不過既然它被移掉了, 還是建議
10/14 03:18, 1F

10/14 03:18, , 2F
你用 C++11 的 lambda + decltype + MACRO 可讀性還是
10/14 03:18, 2F

10/14 03:19, , 3F
高一些
10/14 03:19, 3F
因為LLVM還不支援(冏)

10/14 03:20, , 4F
再來就是你確定要用 shared_ptr 而不是 weak_ptr ?
10/14 03:20, 4F
這裡我不太明白 您的意思是指functor的部分改用weak_ptr 還是說set內存放weak_ptr呢 不知優點為何?

10/14 03:23, , 5F
個人覺得用私有繼承+using會好些, 你在建構子多做了賦
10/14 03:23, 5F

10/14 03:23, , 6F
值的動作
10/14 03:23, 6F

10/14 03:29, , 7F
再來就是推 Boost Dynamic Bitset
10/14 03:29, 7F
Useful information!Thx!

10/14 21:30, , 8F
後來是用怎樣的方法?
10/14 21:30, 8F

10/14 21:37, , 9F
我看錯了-_-
10/14 21:37, 9F
我放棄了,乖乖自己寫comparator-_- ※ 編輯: GreatShot 來自: 111.250.64.140 (10/15 02:43)

10/15 03:04, , 10F
內存改weak_ptr, 可以少很多counter的判斷
10/15 03:04, 10F

10/15 03:04, , 11F
其實這種智慧型指標不適合放在STL容器內
10/15 03:04, 11F
文章代碼(AID): #1EbnxxTM (C_and_CPP)
文章代碼(AID): #1EbnxxTM (C_and_CPP)