什么可能在此语句中生成编译器错误以推进迭代器?

Bee*_*and 1 c++ compiler-errors

以下行生成编译器错误:

std::vector<int>::iterator blah = std::advance(instructions.begin(), x );
Run Code Online (Sandbox Code Playgroud)

在哪里宣布:

std::vector<int> instructions;
int x;
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

error C2440: 'initializing' : cannot convert from 'void' to 'std::_Vector_iterator<_Ty,_Alloc>'.

该陈述的哪个元素属于哪种类型void

Jon*_*cto 5

advance不会返回高级迭代器,它会移动作为参数传递的迭代器.所以你的代码应该是:

std::vector<int>::iterator blah = instructions.begin();
advance(blah, x);
Run Code Online (Sandbox Code Playgroud)

  • 如果有人关心的话,其原因在于它适用于输入迭代器,它不能(必然)被复制. (3认同)