STL迭代器之前std :: map :: begin() [英] STL iterator before std::map::begin()

查看:326
本文介绍了STL迭代器之前std :: map :: begin()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11的 std :: map 中,有一些有效的迭代器 x ,使得++ x 保证等于 map :: begin()?我想检测一个函数,我刚刚调用(我的)已经走过一个迭代器从函数的前面。

In C++11's std::map, is there some valid iterator x such that ++x is guaranteed to equal map::begin()? I would like to detect if a function I just called (mine) has walked an iterator off the front of a function. The function will move the iterator exactly one position backward.

推荐答案

否,在 std 容器之前的迭代器都是UB(除了反向迭代器,这可能不会解决你的问题)。

No, iterators before the beginning in std containers are all UB (except for reverse iterators, which will probably not solve your problem).

您可能需要修复相关函数。没有,包装它,抓住坏的行为,然后再调用它。否则,可以在 map 键类型排序中插入一个负无穷大元素,并添加一个sentinal值。如果没有,你可以编写迭代器适配器,将 map 迭代器与可以在没有UB的情况下一起使用的迭代器。

You probably need to fix the function in question. Failing that, wrap it and catch the bad behavior before you call it. Failing that, you could insert a negative infinity element into the map key type ordering, and add a sentinal value. Failing that, you could write iterator adapters that wrap your map iterators with ones that can go one-before-beginning without UB.

这些是按我的推荐顺序大致排序的。每个都有办法失败,他们得到更多的错误倾向和危险,因为我的建议变得更加遥远。

These are ordered in my order of recommendation, roughly. Each has ways it could fail, and they get more error prone and dangerous as my recommendation gets more remote.

这篇关于STL迭代器之前std :: map :: begin()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆