STL克隆载体

Ber*_*ial 5 c++ stl vector

嗨我很难尝试将指针向量复制到Point.我有一个

vector<Point*> oldVector
Run Code Online (Sandbox Code Playgroud)

我想将此向量复制到其他向量中.所以我使用了复制构造函数.我这样做了

vector<Point*> newVector = vector<Point*>(oldVector.begin(),oldVector.end());
Run Code Online (Sandbox Code Playgroud)

不幸的是,如果我运行此函数,我会收到异常/错误.

矢量交互者不兼容

可能是什么问题?

编辑迭代器必须有一个更大的问题,似乎我根本不能使用迭代器.我想在彼此中添加两个stl向量,所以我使用了写这样的东西

 vector<int> a, b;
    b.insert(b.end(), a.begin(), a.end());
Run Code Online (Sandbox Code Playgroud)

并且在执行此行期间我收到sama异常/错误

在此输入图像描述

Mac*_*Mac 18

那将是

vector<Point*> *newVector = new vector<Point*>(oldVector.begin(),oldVector.end());
Run Code Online (Sandbox Code Playgroud)

要么

vector<Point*> newVector(oldVector.begin(),oldVector.end());
Run Code Online (Sandbox Code Playgroud)

创建对象时,只在从堆分配时使用赋值.否则,只需将构造函数参数放在新变量名后面的括号内.

或者,以下内容更简单:

vector<Point*> newVector(oldVector);
Run Code Online (Sandbox Code Playgroud)

  • vector <Point*> newVector(oldVector)这个工作正常:) (3认同)