My question is in regard to the following piece of code:
pairVectors.push_back(new vector<CompactPair>());
for (int i = 0; i < generationVectors[0].size(); ++(i))
{
//Find the new indices of the two symbols in this pair
long leftIndex = ((terminalIndices)[(generationVectors[0])[i].leftSymbol]);
long rightIndex = ((terminalIndices)[(generationVectors[0])[i].rightSymbol]);
//Make a pair out of the indices we found, then push it to the vector
CompactPair p(leftIndex, rightIndex);
pairVectors[0]->push_back(p);
//Record the index of this symbol
if (indices[(generationVectors[0])[i].leftSymbol].empty())
{
indices[(generationVectors[0])[i].leftSymbol].set_empty_key(-1);
indices[(generationVectors[0])[i].leftSymbol].set_deleted_key(-2);
}
((indices)[(generationVectors[0])[i].leftSymbol])[(generationVectors[0])[i].rightSymbol] = i + terminals.size();
}
The CompactPair p is created using the following constructor:
CompactPair::CompactPair(long left, long right)
{
leftSymbol = left;
rightSymbol = right;
}
Whether or not it is being pushed onto the vector does not seem to matter, leftIndex, rightIndex, p and i all remain visible outside the scope of the loop. Can anybody explain this?
I'm using the Intel c++ 15.0 compiler with optimizations disabled.
You are inserting fully constructed object in your vector so when calling push_back
, you are effectively copying(or moving if enabled) the object to a new one which sits in the vector. The scope of this new object is then tied to the one of the vector.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.