gpt4 book ai didi

c++ - STL克隆载体

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:56 26 4
gpt4 key购买 nike

!嗨我很难尝试将指针 vector 复制到 Point。我有一个

vector<Point*> oldVector

我想将这个 vector 复制到其他 vector 中。所以我使用了一个复制构造函数。我是这样做的

vector<Point*> newVector = vector<Point*>(oldVector.begin(),oldVector.end());

不幸的是,如果我运行这个函数,我会得到一个异常/错误。

vector interators incompatible

可能是什么问题??

编辑迭代器肯定有更大的问题,看来我根本就不会使用迭代器。我想将两个 STL vector 相加,所以我用这样写的东西

 vector<int> a, b;
b.insert(b.end(), a.begin(), a.end());

在执行这一行的过程中我得到了 sama 异常/错误

enter image description here

最佳答案

要么是

vector<Point*> *newVector = new vector<Point*>(oldVector.begin(),oldVector.end());

vector<Point*> newVector(oldVector.begin(),oldVector.end());

创建对象时,只有在从堆分配时才使用赋值。否则,您只需将构造函数参数放在新变量名称后的括号内即可。

或者,下面的方法更简单:

vector<Point*> newVector(oldVector);

关于c++ - STL克隆载体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5109737/

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