I have a question regarding vector [] operator in C++.
vector<stack<T>> myStackVec;
ops...
...
...
This code does not modify myStackVec:
stack<T> temp = myStackVec.at(stackPos);
cout << "removing " << temp.top() << endl;
This code does modify myStackVec through modifying temp:
stack<T> *temp = &myStackVec[stackPos];
temp->push(item);
[] operator returns a reference, why code snippet 1 does not work? Is temp in code1 a copy?
will make a copy of stack, then
temp.push_backwill operate on the copied stack, you need reference instead:or simply: