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

标签 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;
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/

相关文章:

c++ - std::vector 插入错误

python - 用另一个列表切片列表

c - 我如何使用 malloc 在 C 中创建矩阵并避免内存问题?如何使用 C99 语法将矩阵传递给函数?

c++ - 非平凡对象的 ATL 集合

c++ - 定义 typedef 的可变参数模板(使用 C++98)

java - 将一串数字与 int 进行比较

c - 指针和动态内存

c - 使用指针获取数组的长度

c++ - 在 MSSQL 查询中使用 C++ 变量

java - 将字符串列表传递给 Java 方法