VC++向量迭代器初始化 [英] VC++ vector iterator initialization
问题描述
我的头文件中有以下减速
I have the following deceleration in my header file
// 3D Vector
typedef struct tagV3D /*: V2D*/ {
union {
struct {
double x;
double y;
double z;
};
struct {
struct tagV2D v2d_;
};
};
} V3D, TVec3D, *PVec3D;
现在我的 cpp 文件中有一个方法
now i have a method inside my cpp file
bool InsertSelfIntersectionVertexes(vector<PVec3D> &avtxlst) {
PVec3D vtx;
int iI;
...
vtx = new TVec3D;
*vtx = v;
PVec3D* p= avtxlst.begin() + iI + 1;
avtxlst.insert(p, vtx);
...
}
我在尝试编译代码时遇到以下错误
I get following errors trying to compile the code
error C2440:正在初始化":无法从std::_Vector_iterator<_Ty,_Alloc>"转换到'PVec3D *'
和error C2664: 'std::_Vector_iterator<_Ty,_Alloc>std::vector<_Ty>::insert(std::_Vector_const_iterator<_Ty,_Alloc>,const _Ty &)':无法将参数 1 从 'PVec3D' 转换为 'std::_Vector_const_iterator<_Ty,
我该如何解决这个问题?
How do I Fix this?
以下代码在 vc6 上运行良好,并且在迁移到 VS 2008 时出现错误.
这是为什么?
感谢任何答案
The following code worked fine with vc6 and the errors appeared when migrated to VS 2008.
Why is that ?
Appreciate any answers
推荐答案
vector
本身就是一种类型,与指向结构的指针不兼容.您应该创建一个 vector
.
A vector<T>::iterator
is a type by itself and is not compatible with your pointer to your struct. You should create a vector<PVec3D>::iterator my_iter = avtxlst.begin()
.
现在您可以对迭代器执行相同的操作,就像对您拥有的指针一样.如增量、my_iter++
或取消引用*my_iter
.
Now you can do the same operations on your iterator, as you can with the pointer you had. Such as increment, my_iter++
or dereference *my_iter
.
然后您可以使用 my_iter
并通过 Ii
以及您需要执行的任何其他操作来增加它.
You can then use my_iter
and increment it by Ii
and whatever else you need to do.
这篇关于VC++向量迭代器初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!