I have the following code:
std::set< std::vector<int> > testSet;
vector<int> v0 = vector<int>(3);
vector<int> v11 = vector<int>(3);
v0[0] = 0;
v0[1] = 10;
v0[2] = 20;
std::cout << v0[0] << endl;
testSet.insert(v0);
v0[0] = 1;
v0[1] = 11;
v0[2] = 22;
testSet.insert(v0);
std::set< std::vector<int> >::iterator it;
for (it = testSet.begin(); it != testSet.end(); it++) {
const std::vector<int>& i = (*it);
std::cout << i[0] << endl;
}
When I change:
const std::vector<int>& i = (*it)
to:
std::vector<int>& i = (*it)
it stops working. Apparently (*it)
returns a const vector<int>&
, but why is it the case? The set contains vectors, not const
vectors.