ペアとベクトルで構成されるベクトルを定義しました。読みやすくするために、私はこれを行います
#include <iostream>
#include <vector>
using namespace std;
class foo {
public:
vector< pair<int, vector<int> > > v;
vector< pair<int, vector<int> > >::iterator it;
void bar()
{
for( it = v.begin(); it != v.end(); it++ ) {
if ( 10 == (*it).first ) {
vector<int> a = (*it).second; // NOTE
a[0] = a[0]*2; // NOTE
}
}
}
};
int main()
{
foo f;
f.bar();
return 0;
}
ご覧のとおり、簡単に操作できるように(*it).second
変数を割り当てています。a
問題は、の値を変更してa
も元のベクトルv
が変更されないことです。私はその変化a
を(*it).second
ループのあらゆる場所に解決することができます。ただし、これによりコードが読みにくくなります。
a
変更をオリジナルに反映する方法はありますv
か?私はこのように参照によって呼び出す必要があります
vector<int> a = &(*it).second;
動作しません