gpt4 book ai didi

c++ - 复制指针列表没有循环

转载 作者:行者123 更新时间:2023-11-28 06:52:27 25 4
gpt4 key购买 nike

<分区>

我有一个列表,其中包含指向某个对象的指针。我想复制列表,但要使用新指针。

我的意思是列表中的每个元素,对对象执行新操作并将新地址插入新列表。

我尝试使用:

 std::list<Course*> tmp;

tmp = courses; // courses is list<Course*>.

std::for_each(tmp.begin(), tmp.end(), ReplaceAllCells());



class ReplaceAllCells {
public:
ReplaceAllCells(){}
void operator( )( Course* course) {
course = new Course(*course);
}
};

调用函数后,我得到了具有相同指针的新列表……tmp 的值没有改变……我怎样才能在不使用任何循环的情况下解决这个问题?谢谢。

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