如何检测 std::map 循环中的最后一次迭代?
我试图找出最好的方法来确定我是否处于地图上循环的最后一次迭代中,以便执行如下操作:
for (iter = someMap.begin(); iter != someMap.end(); ++iter) {
bool last_iteration;
// do something for all iterations
if (!last_iteration) {
// do something for all but the last iteration
}
}
似乎有几种方法可以做到这一点:随机访问迭代器, distance
函数等。规范方法是什么?
编辑:地图没有随机访问迭代器!
I'm trying to figure out the best way to determine whether I'm in the last iteration of a loop over a map in order to do something like the following:
for (iter = someMap.begin(); iter != someMap.end(); ++iter) {
bool last_iteration;
// do something for all iterations
if (!last_iteration) {
// do something for all but the last iteration
}
}
There seem to be several ways of doing this: random access iterators, the distance
function, etc. What's the canonical method?
Edit: no random access iterators for maps!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
典范? 我不能这么说,但我建议
编辑按照KTC.(谢谢!有时你走得太快,会把最简单的事情搞砸......)
Canonical? I can't claim that, but I'd suggest
Edited to correct as suggested by KTC. (Thanks! Sometimes you go too quick and mess up on the simplest things...)
从 C++11 开始,您还可以使用 std::next()
虽然这个问题是不久前提出的,但我认为值得分享。
Since C++11, you can also use std::next()
Although the question was asked a while ago, I thought it would be worth sharing.
这看起来是最简单的:
This seems like the simplest:
如果您只想使用 ForwardIterator,这应该可以工作:
If you just want to use a ForwardIterator, this should work:
修改了 Mark Ransom 的,使其实际上按预期工作。
Modified Mark Ransom's so it actually work as intended.
令人惊讶的是还没有人提到它,但当然 boost 有一些东西;)
Boost.Next(以及等效的 Boost.Prior)
您的示例如下所示:
Surprised no one mentioned it yet, but of course boost has something ;)
Boost.Next (and the equivalent Boost.Prior)
Your example would look like:
以下代码将由编译器进行优化,以便根据性能和 OOP 规则成为此任务的最佳解决方案:
这是 OOP 规则的最佳代码,因为 std::map 有一个特殊的成员函数 rbegin 用于代码如下:
The following code would be optimized by a compiler so that to be the best solution for this task by performance as well as by OOP rules:
This is the best code by OOP rules because std::map has got a special member function rbegin for the code like:
为什么要努力寻找 EOF,这样你就不会给它一些东西。
简单地,排除它;
KISS(保持简单)
Why to work to find the EOF so that you dont give something to it.
Simply, exclude it;
KISS (KEEP IT SIMPLY SIMPLE)
使用 std::for_each 将确保循环紧密且准确...请注意函数 foo() 的引入,该函数采用单个参数(类型应与 someMap 中包含的内容匹配)。 此方法还增加了 1 行。 当然,如果 Foo 真的很小,您可以使用 lambda 函数并摆脱对 &Foo 的调用。
Using std::for_each will ensure that the loop is tight and accurate... Note the introduction of the function foo() which takes a single argument (the type should match what is contained in someMap). This approach has the added addition of being 1 line. Of course, if Foo is really small, you can use a lambda function and get rid of the call to &Foo.
这个怎么样,没有人提到,但是……
这看起来很简单,嗯……
How about this, no one mentioning but...
this seems simple, mm...
对于喜欢 C++11 基于范围的循环的人:
请注意,此处仅引用类型有效,
自动配对
无效For someone who likes C++11 range-based loop:
Notice only reference type works here, not
auto pair
一个简单而有效的方法:
A simple, yet effective, approach:
完整程序:
该程序产生:
Full program:
This program yields:
这是我的优化方案:
Here's my optimized take:
您可以在迭代之前从地图中取出一个元素,然后在循环之外执行“最后一次迭代”工作,然后将元素放回地图中。 这对于异步代码来说非常糟糕,但考虑到 C++ 的其余部分对于并发性来说有多糟糕,我认为这不会成为问题。 :-)
You can just pull an element out of the map prior to iteration, then perform your "last iteration" work out of the loop and then put the element back into the map. This is horribly bad for asynchronous code, but considering how bad the rest of C++ is for concurrency, I don't think it'll be an issue. :-)