Re: [問題] 關於STL的set (已解決)

看板C_and_CPP (C/C++)作者 (Bad_To_The_Bone)時間13年前 (2013/04/18 11:03), 編輯推噓1(1010)
留言11則, 3人參與, 最新討論串1/1
環境: VC 2005 大家好 借標題來問個問題 一樣是使用set {{MYFILE}}.H內容 struct myitem{ std::wstring a; std::wstring b; }; std::set<myitem> set_; {{MYFILE}}.CPP內容 myitem x; x.a = "a"; x.b = "b"; set_.insert(x); 有.insert的操作 會產生下面的錯誤 想請教一下在set內用struct需要修改什麼呢 感謝 ----------- 為了不傷眼 我把 C:\Program Files\Microsoft Visual Studio 8\VC 取代為{{VCPATH}} 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool std::operator <(const std::stack<_Ty,_Container> &,const std::stack<_Ty,_Container> &)' : could not deduce template argument for 'const std::stack<_Ty,_Container> &' from 'const myitem' 1> {{VC_PATH}}\include\stack(90) : see declaration of 'std::operator <' 1> {{VC_PATH}}\include\functional(142) : while compiling class template member function 'bool std::less<_Ty>::operator ()(const _Ty &,const _Ty &) const' 1> with 1> [ 1> _Ty=myitem 1> ] 1> {{VC_PATH}}\include\set(60) : see reference to class template instantiation 'std::less<_Ty>' being compiled 1> with 1> [ 1> _Ty=myitem 1> ] 1> {{VC_PATH}}\include\xtree(26) : see reference to class template instantiation 'std::_Tset_traits<_Kty,_Pr,_Alloc,_Mfl>' being compiled 1> with 1> [ 1> _Kty=myitem, 1> _Pr=std::less<myitem>, 1> _Alloc=std::allocator<myitem>, 1> _Mfl=false 1> ] 1> {{VC_PATH}}\include\xtree(68) : see reference to class template instantiation 'std::_Tree_nod<_Traits>' being compiled 1> with 1> [ 1> _Traits=std::_Tset_traits<myitem,std::less<myitem>,std::allocator<myitem>,false> 1> ] 1> {{VC_PATH}}\include\xtree(94) : see reference to class template instantiation 'std::_Tree_ptr<_Traits>' being compiled 1> with 1> [ 1> _Traits=std::_Tset_traits<myitem,std::less<myitem>,std::allocator<myitem>,false> 1> ] 1> {{VC_PATH}}\include\xtree(112) : see reference to class template instantiation 'std::_Tree_val<_Traits>' being compiled 1> with 1> [ 1> _Traits=std::_Tset_traits<myitem,std::less<myitem>,std::allocator<myitem>,false> 1> ] 1> {{VC_PATH}}\include\set(69) : see reference to class template instantiation 'std::_Tree<_Traits>' being compiled 1> with 1> [ 1> _Traits=std::_Tset_traits<myitem,std::less<myitem>,std::allocator<myitem>,false> 1> ] 1> {{MYFILE}}.h(22) : see reference to class template instantiation 'std::set<_Kty>' being compiled 1> with 1> [ 1> _Kty=myitem 1> ] 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool std::operator <(const std::queue<_Ty,_Container> &,const std::queue<_Ty,_Container> &)' : could not deduce template argument for 'const std::queue<_Ty,_Container> &' from 'const myitem' 1> {{VC_PATH}}\include\queue(103) : see declaration of 'std::operator <' 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool std::operator <(const std::deque<_Ty,_Alloc> &,const std::deque<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::deque<_Ty,_Alloc> &' from 'const myitem' 1> {{VC_PATH}}\include\deque(1327) : see declaration of 'std::operator <' 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'const myitem' 1> {{VC_PATH}}\include\list(1281) : see declaration of 'std::operator <' 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const myitem' 1> {{VC_PATH}}\include\vector(1276) : see declaration of 'std::operator <' 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const myitem' 1> {{VC_PATH}}\include\string(151) : see declaration of 'std::operator <' 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const myitem' 1> {{VC_PATH}}\include\string(141) : see declaration of 'std::operator <' 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const myitem' 1> {{VC_PATH}}\include\string(131) : see declaration of 'std::operator <' 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const myitem' 1> {{VC_PATH}}\include\xtree(1372) : see declaration of 'std::operator <' 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const myitem' 1> {{VC_PATH}}\include\xutility(1880) : see declaration of 'std::operator <' 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const myitem' 1> {{VC_PATH}}\include\utility(76) : see declaration of 'std::operator <' 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool operator <(_Interface *,_com_ptr_t<_InterfacePtr> &)' : could not deduce template argument for '_Interface *' from 'const myitem' 1> {{VC_PATH}}\include\comip.h(985) : see declaration of 'operator <' 1>{{VC_PATH}}\include\functional(143) : error C2784: 'bool operator <(int,_com_ptr_t<_IIID> &)' : could not deduce template argument for '_com_ptr_t<_IIID> &' from 'const myitem' 1> {{VC_PATH}}\include\comip.h(976) : see declaration of 'operator <' 1>{{VC_PATH}}\include\functional(143) : error C2676: binary '<' : 'const myitem' does not define this operator or a conversion to a type acceptable to the predefined operator -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.72.248.50

04/18 11:12, , 1F
set是有序集合,請先定義 myitem 物件怎樣比大小 :)
04/18 11:12, 1F

04/18 11:20, , 2F
bool operator==(const myitem& other) 跟
04/18 11:20, 2F

04/18 11:20, , 3F
bool operator<(const myitem& other)const 嗎?
04/18 11:20, 3F

04/18 11:26, , 4F
operator<就夠了
04/18 11:26, 4F

04/18 11:27, , 5F
或是可以用std::set<myitem,comp>這種形式
04/18 11:27, 5F

04/18 11:28, , 6F
comp是一個有實做
04/18 11:28, 6F

04/18 11:28, , 7F
bool operator()(const myitem &lsh,const myitem &rhs
04/18 11:28, 7F

04/18 11:29, , 8F
的class
04/18 11:29, 8F

04/18 11:43, , 9F
喔喔 那請教如果需要用到.find 應該就要有==了(吧)
04/18 11:43, 9F

04/18 11:47, , 10F
不用 他的相等判斷是 !(a<b) && !(b<a)
04/18 11:47, 10F

04/18 13:09, , 11F
喔喔 了解 感謝幫忙 compile成功了 :D
04/18 13:09, 11F
文章代碼(AID): #1HRsALV8 (C_and_CPP)
文章代碼(AID): #1HRsALV8 (C_and_CPP)