Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
“理解困难”;列表<;int>;::迭代器i&引用; 我一直在研究如何使列表在C++中工作。尽管第12行不起作用,但我对我在标题中提到的那行更感兴趣,因为我不明白这是怎么回事_C++_List_Iterator - Fatal编程技术网

“理解困难”;列表<;int>;::迭代器i&引用; 我一直在研究如何使列表在C++中工作。尽管第12行不起作用,但我对我在标题中提到的那行更感兴趣,因为我不明白这是怎么回事

“理解困难”;列表<;int>;::迭代器i&引用; 我一直在研究如何使列表在C++中工作。尽管第12行不起作用,但我对我在标题中提到的那行更感兴趣,因为我不明白这是怎么回事,c++,list,iterator,C++,List,Iterator,因此,for循环中有一个错误,但我认为这是因为我对list::iterator I缺乏理解,如果有人能详细解释一下这句话对我意味着什么,那就太棒了 #include <iostream> #include <list> using namespace std; int main(){ list<int> integer_list; integer_list.push_back(0); //Adds a new element to th

因此,
for
循环中有一个错误,但我认为这是因为我对
list::iterator I缺乏理解,如果有人能详细解释一下这句话对我意味着什么,那就太棒了

#include <iostream>
#include <list>

using namespace std;

int main(){

    list<int> integer_list;

    integer_list.push_back(0); //Adds a new element to the end of the list.
    integer_list.push_front(0); //Adds a new elements to the front of the list.
    integer_list (++integer_list.begin(),2); // Insert '2' before the position of first argument.

    integer_list.push_back(5);
    integer_list.push_back(6);

    list <int>::iterator i;

    for (i = integer_list; i != integer_list.end(); ++i)
    {
        cout << *i << " ";
    }


    return 0;

}
#包括
#包括
使用名称空间std;
int main(){
整数列表;
整数列表。向后推(0);//在列表末尾添加新元素。
整数_list.push_front(0);//将新元素添加到列表的前面。
integer_list(++integer_list.begin(),2);//在第一个参数的位置前插入'2'。
整数列表。向后推(5);
整数列表。向后推(6);
列表::迭代器i;
对于(i=integer_list;i!=integer_list.end();++i)
{
coutThe
list::iterator
类型是模板类
list
的迭代器类型。迭代器允许您一次查看列表中的每个元素。修复代码并尝试解释,这是正确的语法:

for (i = integer_list.begin(); i != integer_list.end(); ++i)
{
    // 'i' will equal each element in the list in turn
}
方法
list.begin()
list.end()
list::iterator的每个返回实例,分别指向列表的开头和结尾。for循环中的第一个项初始化您的
list::iterator
,使用复制构造函数指向列表的开头。第二个项检查迭代器是否指向与o相同的位置ne设置为指向末尾(换句话说,您是否已到达列表的末尾),第三项是运算符重载的示例。类
list::iterator
重载了
++
运算符,使其行为类似于指针:指向列表中的下一项

您还可以使用一些语法糖分并使用:

for (auto& i : integer_list)
{

}

对于相同的结果。希望这能为您清除一点迭代器。

初始化
i=integer\u list.begin();
在代码直接取自的教程中,for循环看起来像
for(i=L.begin();i!=L.end();++i)
。你的for循环是什么样子的?@Barry很抱歉我已经修改了,我只是为了自己的方便重新命名了列表如果它解决了你的问题,请不要忘记接受答案;)谢谢!非常有用的信息!