Std :: map - delete the last element

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

I save only 40 elements on this map and would like to delete the last element on the map when I reach 40 elements. I know that I can’t do it myLocations.erase(myLocations.end()), so how can I do it?

I assume that the last element on the map will be the oldest, which means FIFO. The data will arrive pretty quickly (about 20 Hz), so I hope that the card can keep up with it. I need to search for data based on time, so I really need it to be the key, but I am open to alternative methods of doing this.

The line format is a very verbose Thursday, June 21, 18:44:21:281, although I can evaluate it to be seconds from the era for simplicity. This was my first look at him, and he did not think too much about the format.

+5
source share
6 answers

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

I would not use a string for times, instead use a date / time type (e.g. unix timestamp). Then they will be sorted by time, not lexicographically, and you can myLocations.erase(myLocations.begin()), since the oldest will always be in the beginning.

, boost::circular_buffer <std::pair<timetype, LocationStruct>> std::lower_bound, . ​​ . . . boost, std::deque , map, std::map.

, 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;
}
+6

:

myLocations.erase( std::prev( myLocations.end() ) );

++ 11, .

+11

, :

map<string, LocationStruct>::iterator it = myLocations.end();
it--;
myLocations.erase(it);
+6

, g++ 4.4 , :

myLocations.erase(myLocations.rbegin()->first);

, , .

+1

. ( , 00:00 24:00) , , , ,

     `map<string, LocationStruct>::iterator it;`
      it=myLocations.lower_bound ('00:00');
      myLocations.erase ( it, it+1);

if it belongs to different dates, . data is coming quick enough, . The safe way here would be take the entire date in terms of second and remove the lowest one as mentioned above. , .

0

map::erase TS, :

myLocations.erase ((--myLocations.end()));
0

All Articles