「最後の要素を消去する」とは、「最も古い要素を消去する」という意味だと思います。
時間に文字列を使用せず、代わりに日付/時刻タイプを使用します(UNIXタイムスタンプなど)。次に、辞書式ではなく時間で並べ替えられますmyLocations.erase(myLocations.begin())
。最も古いものが常に先頭にあるため、これを行うことができます。
さらに良いことに、を使用し、を使用して時間で要素を検索します。これにより、最も古いものが自動的に削除され、時間ごとに要素を見つける際の論理的な複雑さが同じになります。データを追加するときも高速です。それはあなたの状況のためにほとんどすべての周りに勝ちます。本当に避けたい場合は、ニーズに最もよく適合し、優れたパフォーマンスを提供しますが、すでに作業を行っている場合は、aを使用するのがおそらく最善です。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;
}