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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:03:23+00:00 2026-05-17T00:03:23+00:00

I have a container like this: // Sort functor struct SortByTime : std::binary_function<const TimeSortableData

  • 0

I have a container like this:

// Sort functor
struct SortByTime :
  std::binary_function<const TimeSortableData &, const TimeSortableData &, bool>
{
    bool operator()(const TimeSortableData & a, const TimeSortableData & b) const
    {
        return a.GetTime() < b.GetTime();
    }
};

// Container that sorts by time
typedef std::multiset<TimeSortableData, SortByTime> TimeSortedData;

Now if I want to get the last data object before time t, I could create a dummy object and call upper_bound():

TimeSortableData dummy(t);
TimeSortedData::const_iterator it = mySortedData.upper_bound(dummy);
--it;
// result = *it;

This gives me logarithmic search complexity.
Aside from looking clumsy, this approach is problematic if such a dummy object is very hard to create (not wrt. run-time performance but coding effort).

I’ve looked at std::multiset::key_comp but I don’t see how I could use it..
Both std::multiset::find() and std::binary_search() want me to give them the container’s key_type, i.e. TimeSortableData objects…

How can I search eficiently without having to create a dummy object?

Update from comments:
There is also find_if(): It would spare me the effort of creating a dummy object but at the price of O(n) complexity.

  • 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-05-17T00:03:24+00:00Added an answer on May 17, 2026 at 12:03 am

    I suppose that if your keys are so expensive to construct that you worry about creating a temporary dummy key, you can always change your code to use an std::multimap instead. Let the key be something cheap to construct, such as an integer or time_t or whatever GetTime() returns, and then the data_type could be the larger data.

    typedef std::multimap<time_t, TimeSortableData> TimeSortedData;
    TimeSortedData mySortedData;
    
    ...
    
    time_t dummy = [whatever];
    TimeSortedData::const_iterator it = mySortedData.upper_bound(dummy);
    if (it != mySortedData.begin()) --it; // Check that iterator isn't at beginning
    result = it->second;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that looks like this: class T {}; class container { const
Suppose I have a struct containing a std::string, like this: struct userdata{ int uid;
I have a templated container class that looks something like this: template <class ItemType>
If I have something like this: var container = Ext.create('Ext.container.Container', { plain:true, width :
I want to have 3 divs aligned inside a container div, something like this:
I have this container div whose width I would like to set according to
I have a table like this below. And there is a div container with
I have two containers, let's say they're defined like this: std::vector<std::unique_ptr<int>> a; std::vector<std::unique_ptr<int>> b;
I have an DIV container with an id=myDiv attribute+value. I'd like to fade this
i have div structure like this : <div id=container> <div id=row1 class=row> <div class=c1></div>

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.