我究竟做错了什么:

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);


由于指针算法的原因,使用+=递增迭代器的方法仅适用于数组(或具有random access iterators的容器)。

关于c++ - 无法移动集合迭代器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32425936/

10-12 20:42