How to sort a std::set with const getters

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

I have a std::set container whose elements are objects of the following class:

class LaneConnector { public:      const Lane* getLaneFrom() const {         return From;     }     const Lane* getLaneTo() const {         return To;     }  private:      Lane* From;     Lane* To; } 

and my comparator function is as follows:

struct MyLaneConectorSorter {   bool operator() (LaneConnector * c, LaneConnector * d)   {       Lane* a = const_cast<Lane*>(c->getLaneFrom());       Lane* b = const_cast<Lane*>(d->getLaneFrom());       return (a->getLaneID() < b->getLaneID());   } } myLaneConnectorSorter; 

Now when I try to sort the elements in the set with:

//dont panic, the container just came through a const_iterator of a std::map :) const std::set<LaneConnector*> & tempLC = (*it_cnn).second; std::sort(tempLC.begin(), tempLC.end(), myLaneConnectorSorter); 

I get a frenzy of errors starting with the following lines, Appreciate if you help me solve this problem. Thanks:

/usr/include/c++/4.6/bits/stl_algo.h: In function ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = std::_Rb_tree_const_iterator<LaneConnector*>, _Compare = {anonymous}::MyLaneConectorSorter]’: /home/.../dev/Basic/shared/conf/simpleconf.cpp:1104:65:   instantiated from here /usr/include/c++/4.6/bits/stl_algo.h:5368:4: error: no match for ‘operator-’ in ‘__last - __first’ /usr/include/c++/4.6/bits/stl_algo.h:5368:4: note: candidates are: /usr/include/c++/4.6/bits/stl_iterator.h:321:5: note: template<class _Iterator> typename std::reverse_iterator::difference_type std::operator-(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&) /usr/include/c++/4.6/bits/stl_iterator.h:378:5: note: template<class _IteratorL, class _IteratorR> typename std::reverse_iterator<_IteratorL>::difference_type std::operator-(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&) /usr/include/c++/4.6/bits/stl_bvector.h:181:3: note: std::ptrdiff_t std::operator-(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&) /usr/include/c++/4.6/bits/stl_bvector.h:181:3: note:   no known conversion for argument 1 from ‘std::_Rb_tree_const_iterator<LaneConnector*>’ to ‘const std::_Bit_iterator_base&’ 

回答1:

First, you cannot sort an std::set. It is a sorted structure, sorting happens upon construction or insertion.

Second, you can construct an std::set with your own sorting functor, and you can avoid unnecessary const_casts by making it take const pointers:

struct MyLaneConectorSorter {   bool operator() (const LaneConnector* lhs, const LaneConnector* rhs) const   {     // you may want to put some null pointer checks in here     const Lane* a = lhs->getLaneFrom();     const Lane* b = rhs->getLaneFrom();     return a->getLaneID() < b->getLaneID();   } }; 

and instantiate the set like this:

std::set<LaneConnector*, MyLaneConectorSorter> s(MyLaneConectorSorter()); 

or, if you want to construct it from a different set, with a different ordering,

std::set<LaneConnector*> orig = ..... ; .... std::set<LaneConnector*, MyLaneConectorSorter> s(orig.begin(), orig.end(), MyLaneConectorSorter()); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!