不能移动集合迭代器



我做错了什么:

set<int>::iterator beg = begin( my_set );
++beg;//<<here, no problem, as expected
beg += 3;  //error here no += operator found?!  

知道为什么吗?

推进迭代器的正确方法是使用std::advancestd::next

beg = std::next(beg, 3);
std::advance(beg, 3);

由于指针算术的原因,使用+=对迭代器进行自增的方法只适用于数组(或具有随机访问迭代器的容器)。

最新更新