gpt4 book ai didi

c++ - 你如何通过 STL 列表向后迭代?

转载 作者:IT老高 更新时间:2023-10-28 12:35:05 24 4
gpt4 key购买 nike

我正在编写一些 Windows 和 Mac 之间的跨平台代码。

如果 list::end() “返回一个迭代器,该迭代器指向列表中最后一个元素之后的位置”并且可以在向前遍历列表时检查,那么向后遍历的最佳方法是什么?

此代码适用于 Mac,但不适用于 Windows(不能递减超过第一个元素):

list<DVFGfxObj*>::iterator iter = m_Objs.end();
for (iter--; iter!=m_Objs.end(); iter--)// By accident discovered that the iterator is circular ?
{
}

这适用于 Windows:

list<DVFGfxObj*>::iterator iter = m_Objs.end();
do{
iter--;
} while (*iter != *m_Objs.begin());

是否有另一种可以在 for 循环中实现的向后遍历方式?

最佳答案

使用 reverse_iterator 而不是 iterator。使用 rbegin() & rend() 而不是 begin() & end()

另一种可能性,如果您喜欢使用 BOOST_FOREACH宏是使用Boost 1.36.0中引入的BOOST_REVERSE_FOREACH宏。

关于c++ - 你如何通过 STL 列表向后迭代?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/188942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com