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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:28:42+00:00 2026-05-10T18:28:42+00:00

Problem I have timestamped data, which I need to search based on the timestamp

  • 0

Problem

I have timestamped data, which I need to search based on the timestamp in order to get the one existing timestamp which matches my input timestamp the closest.
Preferably this should be solved with the STL. boost::* or stl::tr1::* (from VS9 with Featurepack) are also possible.
Example of timestamped data:

struct STimestampedData {  time_t m_timestamp; // Sorting criterion  CData m_data;       // Payload } 

Approach with stl::vector, sort() and equal_range()

Since a map or set only allows me to find exact matches, I don’t get any further using one of these. So now I have a vector to which I append data as it is coming in. Before searching I use <algorithm>‘s sort() and supply it with a custom comparison function.
After that I use <algorithm>‘s equal_range() to find the two neighbors of a specified value x. From these two values I check which one is closest to x and then I have my best match.

While this is not too complex, I wonder if there are more elegant solutions to this.
Maybe the STL already has an algorithm which does exactly that so I’m not re-inventing something here?

Update: Linear vs. binary search

I forgot to mention that I have quite a lot of data to handle so I don’t want to have to search linearly.
The reason I am sorting a vector with sort() is because it has random access iterators which is not the case with a map. Using a map would not allow equal_range() to do a search with twice logarithmic complexity.
Am I correct?

  • 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. 2026-05-10T18:28:42+00:00Added an answer on May 10, 2026 at 6:28 pm

    I would use set::lower_bound to find the matching or greater value, then decrement the iterator to check the next lower value. You should use std::set rather than std::map since your key is embedded in the object – you’ll need to provide a functor that compares the timestamp members.

    struct TimestampCompare {     bool operator()(const STimestampedData & left, const STimestampedData & right) const     {         return left.m_timestamp < right.m_timestamp;     } }; typedef std::set<STimestampedData,TimestampCompare> TimestampedDataSet;  TimestampedDataSet::iterator FindClosest(TimestampedDataSet & data, STimestampedData & searchkey) {     if (data.empty())         return data.end();     TimestampedDataSet::iterator upper = data.lower_bound(searchkey);     if (upper == data.end())         return --upper;     if (upper == data.begin() || upper->m_timestamp == searchkey.m_timestamp)         return upper;     TimestampedDataSet::iterator lower = upper;     --lower;     if ((searchkey.m_timestamp - lower->m_timestamp) < (upper->m_timestamp - searchkey.m_timestamp))         return lower;     return upper; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a problem. I have closing data to display, app is get it
Problem: I have an address field from an Access database which has been converted
Problem: I have two spreadsheets that each serve different purposes but contain one particular
Problem: I have to support users who need to edit web pages. Some of
My main problem is storing the data which is not supported by Core Data.
I have a file of data Dump, in with different timestamped data available, I
I essentially have a bunch of data objects which map timestamps in milliseconds to
I have a Gridview that has a timestamp as one of the rows. When
I have a problem in converting the Unix timestamp to sql server timestamp. I
This is a complex one. But I have a table which has a DATETIME

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.