gpt4 book ai didi

c++ - 将迭代器中的数据插入列表的问题

转载 作者:行者123 更新时间:2023-11-30 01:42:40 24 4
gpt4 key购买 nike

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

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;
j = 5;
bob.push_front(j);
std::list<int>::const_iterator goop = bob.begin();
bob.push_front(*goop); //inserts goop just fine

第一个是指向整数的指针列表,第二个只是整数。第一个问题是我在尝试插入时取消引用迭代器,提示打字,特别是想要“int * const &

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

最佳答案

改变这个:

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

为此:

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

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

关于c++ - 将迭代器中的数据插入列表的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39299704/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com