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 8524649
In Process

The Archive Base Latest Questions

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

I am using boost::multi_index_container to provide multiple views and sorting orders for a set

  • 0

I am using boost::multi_index_container to provide multiple views and sorting orders for a set of objects. Recently, I wanted to sort the container using a custom sorting predicate that (in essence) pre-calculates attribute values for all objects and then uses these values to sort them (see below for example code).

The container gets sorted correctly, but I noted that sorting with this predicate takes much longer that sorting with a predicate whose operator() simply accesses an internal property of my objects.

Further investigation showed that the (implicitly defined) copy-constructor of my predicate was called very often. Since each copy of the predicate contains a copy of the complete attribute map, this took a long time.

I have since solved this by adding an internal attribute to my objects, but I am still not convinced that this is the best of course of action. So, I would like to know:

  • Why is the copy constructor called this often?
  • Am I defining my predicate correctly? Are predicate not supposed to contain this much internal data?
  • What would be a better solution than defining yet another internal object attribute?

Here’s the relevant portion of my code. I did not describe the Object class in much detail because its attributes do not contribute to the problem.

class Predicate
{
public:
  Predicate()
  {
    // fill _attributes map with attribute values for all objects
  }

  bool operator()(const Object& a, const Object& b) const
  {
    std::map<Object, double>::const_iterator firstPos = _attributes.find( a );
    std::map<Object, double>::const_iterator secondPos = _attributes.find( b );

    // throw error if one of the objects could not be found

    return( firstPos->second < secondPos->second );
  }

private:
  std::map<Object, double> _attributes;
};

// Later, in order to sort the container
_container.sort( Predicate() );
  • 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-11T07:44:54+00:00Added an answer on June 11, 2026 at 7:44 am

    One solution would be to construct your attributes map once, outside of the predicate, and have the predicate hold a const reference to the map. Another option would be to pass an std::ref or boost::ref to the predicate to your sorting function. This will avoid unnecessary copies of the Predicate‘s std::map data member.

    Predicate p; // builds map internally
    _container.sort( boost::ref(p) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a Boost multi_index container to store objects according to 2 integer keys
I'm using a boost::multi_index_container to provide a random access and a hash-based access to
I'm using boost::multi_index_container and I'm trying to refer to a member of a member
I have a boost multi index container thus. using namespace boost::multi_index; template < typename
I'm thinking of using boost::weak_ptr to implement a pool of objects such that they
So I have a boost::multi_index_container with multiple non-unique indexes. I would like to find
I am using the boost::multi_index_container (as follows) and I am running into a compile
Background Using boost and other similar libraries is the easiest way to find compiler
I'm using boost uuid to generate session ids. std::string SessionGenerator::generate() { boost::uuids::uuid id =
I am not using boost libraries. How can i do this using STL? class

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.