修改c ++ vector

时间:2016-10-03 13:34:37

标签: c++ pointers vector reference

我想跟踪矢量中的一个项目并按如下方式修改它:

vector<myObj> v;

myObj obj = myObj(params)
v.push_back(myObj);

obj = myObj(params)
v.push_back(myObj);

obj = myObj(params)
v.push_back(myObj);

myObj* modThis = &v[2];
//modify modThis

但是当我在实际代码中执行类似的操作时,它只修改对象的副本而不是修改向量内的实际对象。我打印了modThis和&amp; v [2]的地址,它们不一样!我也尝试过以下内容:

myObj modThis = v[2]; 
//modiy myObj

也似乎只创建了对象的副本并继续修改副本而不是存储在向量中的原始对象。

如何在不复制项目的情况下访问向量内部对象的访问权限?我错过了什么?

注意:myObj目前有编译器生成的拷贝构造函数。

1 个答案:

答案 0 :(得分:0)

您应该使用矢量引用运算符来修改数组中的对象。
正确的方法:

Sub ShowSeries() Dim mySrs As Series Dim myPts As Points Dim chtType As Long Dim colors As String With ActiveSheet For Each mySrs In ActiveChart.SeriesCollection 'Add label Set myPts = mySrs.Points myPts(myPts.Count).ApplyDataLabels ShowSeriesName:=True, ShowValue:=False 'Color text label same as line color 'if line has default color If mySrs.Border.ColorIndex = -4105 Then chtType = mySrs.ChartType 'Temporarily turn this in to a column chart: mySrs.ChartType = 51 mySrs.DataLabels.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = _ mySrs.Format.Fill.ForeColor.RGB 'reset the chart type to its original state: mySrs.ChartType = chtType 'if line has a color manually changed by user Else mySrs.DataLabels.Font.ColorIndex = mySrs.Border.ColorIndex End If Next End With

如果你的矢量持有指针,那么:

SELECT * FROM WorldFlowers_table WHERE (device_id, score) IN (SELECT device_id, max(score) FROM WorldFlowers_table GROUP BY device_id ) ORDER BY score DESC LIMIT 100

希望有所帮助