我有迭代器,我不想更改,但我想将另一个迭代器分配给该迭代器 -1 的值,所以
it2 = --it1;
是不可能的。
问题:
it2 = it1-1;
不起作用。
error: no match for 'operator-' in 'bigger - 1' c++/4.1.1/bits/stl_bvector.h:182: note: candidates are: ptrdiff_t std::operator-(const std::_Bit_iterator_base&, const std::_Bit_iterator_base&)
我能做到
it2 = --it1;
it1++;
但这太可怕了。
最佳答案
在 C++11 中,使用 std::prev
来自标题 <iterator>
:
it2 = std::prev(it1);
在此之前,您必须自己制作拷贝并将拷贝推进 -1:
it2 = it1;
std::advance(it2, -1);
或:
it2 = it1;
it2--;
关于c++ - 如何从双向迭代器中减去 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20571144/