Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8575369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:40:44+00:00 2026-06-11T19:40:44+00:00

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

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T19:40:45+00:00Added an answer on June 11, 2026 at 7:40 pm

    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());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have container in my class like this: protected: std::map<const AAA*, std::set<BBB*> > conn;
I have the following problem: template<class T> void set(std::string path, const T data) {
i have following code #include <iostream> #include <set> #include <string> using namespace std; template<class
I have a std::vector of some class of the form class A{ public: A():i(someNumber){}
I have the following code. #include <set> #include <algorithm> using namespace std; int _tmain(int
Suppose I have the following two data structures: std::vector<int> all_items; std::set<int> bad_items; The all_items
I have a short question about the std::set container. Right now I am feeding
class Foo1: public IFoo { public: template <class T> std::vector<T> foo() { return std::vector<T>();
I have a container class (called Atom) that I want to store objects of
I have a class which accumulates information about a set of objects, and can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.