[問題] unordered_set包unordered_set一問

看板C_and_CPP作者 (蘇打)時間14年前 (2010/05/25 15:57), 編輯推噓0(003)
留言3則, 1人參與, 最新討論串1/1
遇到的問題: (題意請描述清楚) 使用unordered_set遇到的問題,想要製造多層時會發生compile錯誤 (使用boost而非tr1) 希望得到的正確結果: compile通過 程式跑出來的錯誤結果: instantiated from here (compile錯誤) /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_set.h:321: instantiated from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<Key, Compare, Allocator>::insert(const _Key&) [with _Key = boost::unordered_set<long int, boost::hash<long int>, std::equal_to<long int>, std::allocator<long int> >, _Compare = std::less<boost::unordered_set<long int, boost::hash<long int>, std::equal_to<long int>, std::allocator<long int> > >, _Alloc = std::allocator<boost::unordered_set<long int, boost::hash<long int>, std::equal_to<long int>, std::allocator<long int> > >]' 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) g++ 4.1.1 Linux (SL 5.3) 使用boost的unordered_set (不使用tr1是因為他不包含!= 以及==這兩個operator) 有問題的code: (請善用置底文標色功能) 原始的程式碼長相如下 set< set<long> > hello; set<long> hello2; hello2.insert( 1 ); hello.insert( hello2 ); 這樣子是ok 但如果全部改成unordered_set boost::unordered_set< boost::unordered_set<long> > hello; boost::unordered_set<long> hello2; hello2.insert( 1 ); hello.insert( hello2 ); 在紅色那行會發生編譯錯誤 錯誤訊息如上所示 如果hello宣告改成 set< boost::unordered_set<long> > hello; 一樣會錯誤,只是錯誤訊息不同 似乎是因為他不認得boost::unordered_set 補充說明: tr1我嘗試過會發生一樣的錯誤 而且tr1沒有我需要的== 及!=兩個operator... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.138.145.170

05/25 16:00, , 1F
set 要求內部元素是 strict weak ordering 吧。
05/25 16:00, 1F

05/25 16:05, , 2F
幫它做一個 operator< 就可以了。
05/25 16:05, 2F

05/25 16:07, , 3F
不過最好的方法還是去自訂 template 第二個引數。
05/25 16:07, 3F
文章代碼(AID): #1B-uBso2 (C_and_CPP)