请教一下:怎样修改vector中的对象?
在vector中放一个对象 我想修改对象中的一个成员变量
或者用它的参数构造一个新的对象 在vector中把它替换掉也行
下面是一部分源码(……这样写自然不对……)
container是一个vector的容器
我想修改其中一个元素(对象)的成员变量(*y)-> vertexcount1
for(Iter y = container.begin();y != container.end();y++){
if(((*y)-> vertex1 == tempvertex1 || (*y)-> vertex1 == tempvertex2) && (*y)-> vertexcount1 ==1){
(*y)-> vertexcount1++;
}else if(((*y)-> vertex2 == tempvertex1 || (*y)-> vertex2 == tempvertex2)&& (*y)-> vertexcount2 ==1){
(*y)-> vertexcount2++;
}
}
[解决办法]
(*y)-> vertexcount1
==>
(*y).vertexcount1 或者是 y-> vertexcount1
如果你的 vector 中的对象可以这么被直接访问成员,
那么这样是可以的。
否则使用成员函数访问即可:y-> memberFunc();