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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 65k
  • Answers 65k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer URLConnection is fine for simple cases. When there are things… May 11, 2026 at 11:05 am
  • added an answer The HTML Agility Pack is highly rated. It will certainly… May 11, 2026 at 11:05 am
  • added an answer The best way of seeing whether or not anything was… May 11, 2026 at 11:05 am

Related Questions

Problem I have timestamped data, which I need to search based on the timestamp
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
The problem: I have a class which contains a template method execute which calls
One problem I have with open-source is not often the product or documentation, but
The problem I have a transparent NSView on a transparent NSWindow . The view's
I have the following problem: I have an HTML textbox ( <input type=text> )
I have an odd problem...I'm using a documentation generator which generates a lot of
I have a problem I'm trying to solve involving interfaceing a C++ program with

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.