c++ - STL克隆载体

标签 c++ stl vector

!嗨 我很难尝试将指针 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/

相关文章:

c++ - 继承构造函数和虚基类

std::find_if 中的 C++ lambda 表达式?

c++ - std::merge 和 std::inplace_merge 之间的区别?

opencv - 在 OpenCV 中写入向量

c++ - Vector size() 返回看似随机的大整数

c++ - vector 大小 - 在 C++ 中大小为 0 时为 1

c++ - 对数组值的 static_assert

c++ - 如何检查类型是否可显式/隐式构造?

c++ - SIGSEGV 同时尝试读取数组

c++ - 在STL映射中通过引用查找并返回