使用“序列<>”你类似于“的std ::名单”语义索引看到提振codumentation和code example。改变你的“类型定义”到:
typedef boost::multi_index_container<
boost::shared_ptr<Temp>,
boost::multi_index::indexed_by<
boost::multi_index::sequenced<>,
boost::multi_index::ordered_non_unique<
boost::multi_index::const_mem_fun<Temp, unsigned int, &Temp::getParam>
>
>
> RequestsContainer;
,然后有“的std ::名单”的额外的语义,你会得到一个双向迭代结束和减少它作为每this question,这样的:
RequestsContainer r;
/* ... fill r ... */
assert(!r.empty);
auto iter = r.end(); // from sequenced<>
--iter; // iter now points to the last element
r.erase(iter); // pop()
- 编辑 -
如果什么“最后”的语义是不是插入的顺序,但你ordered_non_unique索引的顺序,你可以用“reverse_iterator的::基地()”,这给了一个向前的'迭代器'到下一个元素:
RequestsContainer r;
/* ... fill r ... */
auto index = r.get<1>(); // or get<0> if no sequenced<>
auto riter = index.rbegin(); // reverse_iterator
++riter; // points previous to last element
auto iter = riter.base(); // points to the last element
r.erase(iter); // pop()
另请参阅this有关将逆向迭代器转换为向前迭代器的答案。
谢谢,但我需要使用ordered_non_unique索引删除最后一个项目。在你的解决方案中,我会删除最后一项,保持插入顺序。我对吗? – user2301299 2013-04-25 02:02:10
是的,你是对的,我不明白你的问题。我正在使用reverse_iterator :: base()更新我的答案,这正是你想要的。 – Antoine 2013-04-25 08:38:23