Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在std::copy中推进列表迭代器时出错_C++ - Fatal编程技术网

C++ 在std::copy中推进列表迭代器时出错

C++ 在std::copy中推进列表迭代器时出错,c++,C++,怎么了?我很困惑。我想问题出在复制功能上 错误: 不能通过使用多个值对列表进行递增而使其非随机前进。换句话说,双向迭代器只定义了运算符++和运算符-。改用 std::next将指向正确的内部迭代器函数,该函数实际上将逐个重复递增迭代器。list\u int和list\u int2属于std::list类型。std::list的迭代器不支持随机访问。因此,不能通过添加类似list_int.crbegin+3这样的整数来提升它们 您可以制作列表迭代器的副本,并使用std::advance对其进行升级

怎么了?我很困惑。我想问题出在复制功能上

错误:

不能通过使用多个值对列表进行递增而使其非随机前进。换句话说,双向迭代器只定义了运算符++和运算符-。改用

std::next将指向正确的内部迭代器函数,该函数实际上将逐个重复递增迭代器。

list\u int和list\u int2属于std::list类型。std::list的迭代器不支持随机访问。因此,不能通过添加类似list_int.crbegin+3这样的整数来提升它们


您可以制作列表迭代器的副本,并使用std::advance对其进行升级。请参阅std::list的迭代器不是双向的。它们是双向的,不是随机迭代器。请用你的标题描述这个问题。你可以看到其他的问题不仅仅是为什么我的代码错了
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using std::list;
using std::endl;
using std::cout;
using std::iterator;
int main()
{
    list<int> list_int{ 0,1,2,3,4,5,6,7,8,9 };
    list<int> list_int2;

    copy(list_int.crbegin()+3 , list_int.crbegin()+8,back_inserter(list_int2));
    for (auto &ele : list_int2)
        cout << ele << endl;

    return 0;
}
copy(std::next(list_int.crbegin(), 3), 
     std::next(list_int.crbegin(), 8),
     back_inserter(list_int2));