使用 new 创建的向量的操作
谁能帮忙解决这个问题...
vector<unsigned int> *vVec = new vector<unsigned int>;
vVec .reserve(frankReservedSpace);
start = std::clock();
for(int f=0; f<sizeOfvec; f++)
{ //Populate the newly created vector on the heap
vVec .push_back(pArray[f]);
}
我得到: 错误 C2228:'.reserve' 的左侧必须具有类/结构/联合
我正在使用 new 运算符创建一个向量,以便它比创建它的函数寿命更长。 因此,这给了我一个指向堆上该向量的指针,而不是一个实际的向量对象本身。 因此它不会让我执行任何push_backs的.reserve()。 我看不到解决办法,有人可以帮忙吗?
Can anyone help with this...
vector<unsigned int> *vVec = new vector<unsigned int>;
vVec .reserve(frankReservedSpace);
start = std::clock();
for(int f=0; f<sizeOfvec; f++)
{ //Populate the newly created vector on the heap
vVec .push_back(pArray[f]);
}
I'm getting:
error C2228: left of '.reserve' must have class/struct/union
I'm creating a vector using the new operator so that it outlives the function where it is created. This therefore gives me back a pointer to that vector on the heap rather than an actual vector object itself. therefore it won't let me carry out any .reserve() of push_backs.
I can't see a way around it, can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
vVec 是指向向量的指针。 因此,您应该使用间接 (->) 运算符而不是点 (.)
vVec is a pointer to a vector. Therefore you should be using the indirection (->) operator rather than the dot (.)
使用“->” 代替 ”。”
vVec
是指针类型,因此需要使用operator ->
来访问它所指向的对象的成员。Use "->" instead of "."
vVec
is of pointer type, so you need to useoperator ->
to access members of the object it points to.相反,
你想要:
Insterad of
you want:
就我个人而言,我不关心您在变量名称中内置的匈牙利表示法。 我宁愿看到比“vVec”更特定领域和自我记录的东西。 如果您决定更改为链接列表,是否必须更改变量名称才能反映这一点? 显然答案是否定的。
Personally, I don't care for the Hungarian notation you've build into your variable name. I would rather see something more domain specific and self-documenting than 'vVec'. If you decided to change to a linked list, would the variable name have to change to reflect that? Obviously the answer is no.