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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:02:23+00:00 2026-06-06T04:02:23+00:00

My map is defined as such: map<string, LocationStruct> myLocations; where the key is a

  • 0

My map is defined as such:
map<string, LocationStruct> myLocations; where the key is a time string

I am only keeping 40 items in this map, and would like to drop off the last item in the map when i reach 40 items. I know that i can’t do myLocations.erase(myLocations.end()), so how do i go about this?

I do intend for the last item in the map to be the oldest, and therefore FIFO. The data will be coming in rather quick (about 20Hz), so i’m hoping that the map can keep up with it. I do need to look up the data based on time, so i really do need it to be the key, but i am open to alternate methods of accomplishing this.

The format of the string is a very verbose “Thursday June 21 18:44:21:281”, though i can pare that down to be the seconds since epoch for simplicity. It was my first go at it, and didn’t think too much about the format yet.

  • 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-06T04:02:26+00:00Added an answer on June 6, 2026 at 4:02 am

    I assume when you say “erase last element”, you mean “erase oldest element”.

    I wouldn’t use a string for times, use a date/time type instead (like unix timestamp). Then they’ll be sorted by time, instead of lexicographically, and you can myLocations.erase(myLocations.begin()), since the oldest would always be at the beginning.

    Even better, use a boost::circular_buffer<std::pair<timetype, LocationStruct>>, and use std::lower_bound to find elements by time. This will automatically remove the oldest for you, and has the same logorithmic complexity on finding an element by time. It’s also faster when adding data. It’s pretty much win all around for your situation. If you really want to avoid boost, then a std::deque fits your needs best, and gives great performance, but if you already have a working map, then staying with a std::map is probably best.

    Here’s how to do the find in a deque:

    typedef ???? timetype;
    typedef std::pair<Timetype, LocationStruct> TimeLocPair
    typedef std::deque<TimeLocPair> LocationContainer;
    typedef LocationContainer::const_iterator LocationIterator;
    
    bool compareTimeLocPair(const TimeLocPair& lhs, const TimeLocPair& rhs)
    {return lhs.first < rhs.first;}
    
    LocationIterator find(const LocationContainer& cont, timetype time) {
        TimeLocPair finder(time, LocationStruct());
        LocationIterator it = std::lower_bound(cont.begin(), cont.end(), finder, compareTimeLocPair);
        if (it == cont.end() || it->first != time)
            return cont.end();
        return it;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to derive from Scala's immutable Map. It is defined as such:
I want to define a std::map with key as std::pair<std::string, std::string> something like follow
I have a dispatch map defined as such: private Dictionary<string, Func<DynamicEntity, DynamicEntity, IEnumerable<DynamicEntity>, string>>
I would like to achieve something similar to how scala defines Map as both
I've got this snippet of code: (define-key lisp-interaction-mode-map (kbd C-c C-e) (lambda () (let
Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic This
I would like to define a type such as: type blah = AThing |
This is in the context of Automatic Differentiation - what would such a system
I doubt this is possible but what I would like to do is have
I am looking for a robust Map in Java, where the key lookup would

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.