Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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++ 将迭代器中的数据插入列表时出现问题_C++_List_Pointers_Compiler Errors_Iterator - Fatal编程技术网

C++ 将迭代器中的数据插入列表时出现问题

C++ 将迭代器中的数据插入列表时出现问题,c++,list,pointers,compiler-errors,iterator,C++,List,Pointers,Compiler Errors,Iterator,我有两套非常相似的代码。我想强调的是,它们不是按顺序排列的,甚至不是在同一个程序中。为了便于说明,我将它们并排放置: std::list<int*> bob; int * j = new int; *j = 5; bob.push_front(j); std::list<int>::const_iterator goop = bob.begin(); bob.push_front(*goop); //takes issue with inserting goop 第一个

我有两套非常相似的代码。我想强调的是,它们不是按顺序排列的,甚至不是在同一个程序中。为了便于说明,我将它们并排放置:

std::list<int*> bob;
int * j = new int;
*j = 5;
bob.push_front(j);
std::list<int>::const_iterator goop = bob.begin();
bob.push_front(*goop); //takes issue with inserting goop
第一个是指向整数的指针列表,第二个只是整数。 第一个问题是我在尝试插入时取消了对迭代器的引用,抱怨类型化,特别是想要int*const&

这是怎么回事?关于迭代器如何引用其基础数据,以及我必须如何执行第二种情况才能从列表的迭代器插入到列表中,我误解了什么?

更改以下内容:

std::list<int>::const_iterator goop = bob.begin();
为此:

std::list<int*>::const_iterator goop = bob.begin();
由于您需要指向整数的指针,在第一个示例中。

更改此选项:

std::list<int>::const_iterator goop = bob.begin();
为此:

std::list<int*>::const_iterator goop = bob.begin();

因为您需要指向整数的指针,在第一个示例中。

您使用的迭代器是list::const_迭代器,但您使用它来迭代列表


您需要使用list::const_迭代器来迭代列表

您正在使用的迭代器是list::const_迭代器,但您正在使用它对列表进行迭代


您需要使用list::const_迭代器来迭代列表

我不理解你的困惑。列表部分完全不相关。您在这里试图做的是:

int* j = new int;
int i = j;  // illegal, pointer vs integer
j = i;  // illegal, integer vs pointer
就这么简单

std::list<int*> bob;
j是指向整数的指针

*j = 5;
这句话与问题完全无关

bob.push_front(j);
这会将最近分配的地址推到列表的前面。不是值5,内存的地址

std::list<int>::const_iterator goop = bob.begin();
如果前一行已经编译,这将是非法的,因为*goop的类型是int,而bob的值类型是int*而不是int

bob.push_front接受int**goop将是一个int

<>也许你应该考虑利用C++ 11的自动关键字
std::list<int*> bob;
bob.push_front(new int);
auto it = bob.begin();  // std::list<int*>::iterator it = bob.begin();
// or auto it = bob.cbegin();   if you want a const_iterator
pop.push_front(*it);
现在,当你犯下这个错误时,情况也变得更加清楚了:

std::list<unique_ptr<int>> bob;
std::list<int> sally;  // clearly not compatible with bob.

我不理解你的困惑。列表部分完全不相关。您在这里试图做的是:

int* j = new int;
int i = j;  // illegal, pointer vs integer
j = i;  // illegal, integer vs pointer
就这么简单

std::list<int*> bob;
j是指向整数的指针

*j = 5;
这句话与问题完全无关

bob.push_front(j);
这会将最近分配的地址推到列表的前面。不是值5,内存的地址

std::list<int>::const_iterator goop = bob.begin();
如果前一行已经编译,这将是非法的,因为*goop的类型是int,而bob的值类型是int*而不是int

bob.push_front接受int**goop将是一个int

<>也许你应该考虑利用C++ 11的自动关键字
std::list<int*> bob;
bob.push_front(new int);
auto it = bob.begin();  // std::list<int*>::iterator it = bob.begin();
// or auto it = bob.cbegin();   if you want a const_iterator
pop.push_front(*it);
现在,当你犯下这个错误时,情况也变得更加清楚了:

std::list<unique_ptr<int>> bob;
std::list<int> sally;  // clearly not compatible with bob.

保罗,看来我比你快了一倍但我们对解决方案达成一致,不是吗+1.保罗,看来我比你快了一倍但我们对解决方案达成一致,不是吗+1.